« Back to Custom fields

Custom Fields and JSP Hooks

Introduction #

Our goal is to take advantage of several new Liferay Portal features to add a field to the Create Account portlet, and persist submitted values to the database. In the past (prior to Liferay 5.2), this would have most likely been accomplished using the EXT environment or a combination of Portal Hook Plugins. In this example, the following Liferay Portal features will be used to accomplish this:

  • Custom User Fields
  • Plugins SDK Hooks

Note - The steps in this article rely on several fixes that were added after Liferay 5.2.2 were released. You will need to use 5.2.3 when it becomes available , or build from source.

Creating Custom User Fields #

-Login to the portal as an administrator -Select Control Panel from the Dock -Select Users' from the Portal section of the left hand nav bar -Click Custom Fields from the top of the panel -Click the Add Custom Fields button -Enter home-town-newspaper in the Key field -Select Text Field - Indexed in the Type field -Select Save

You have now created a Custom Field that will be persisted to the database. By default, the field won't be available for all users, so we'll need to modify the permissions.

-Click the Actions button that corresponds to the home-town-newspaper custom field and select Permissions. -Add a check for Update and View on the Guest role (You may also want to add Update and View permissions for the User role as well if you want users to be able to update their choice through the My Accounts screen) -Click Submit.

Your custom field is now ready to be used.

Note - By using the hyphenated name for the Key field, Liferay provides some very nice features. Adding the key to the language files allows you to localize the term. Also, if the key is a term that's already been localized then you don't have to add it, it will just work. Lastly, Liferay will apply a parser that will pretty print the key for you, so it will become Home Town Newspaper when displayed as a label in the JSP.

Modifying the Create Accounts page #

Create Portal Hook Plugin #

This section assumes that you have already configured the Liferay Plugins SDK environment and have access to the Liferay Portal source code. If not, please refer to this article http://www.liferay.com/web/guest/community/wiki/-/wiki/Main/Plugins+SDK for information on setting up the Plugins SDK. The portal source code can be downloaded from the downloads http://www.liferay.com/web/guest/downloads/portal section of Liferay.com.

-Create a new folder in your plugins/hooks folder called customjsp-hook -Create a file called build.xml with the following contents:

<?xml version="1.0"?>
<project name="hook" basedir="." default="deploy">
      <import file="../build-common-hook.xml" />
</project>

-Create a new folder inside of customjsp-hook named docroot -Create a new folder inside of docroot named WEB-INF -Create a new file inside of WEB-INF named liferay-hook.xml with the following contents: Note - there should no square brackets in the 2nd line of the xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 5.2.0//EN"  "http://www.liferay.com/dtd/liferay-hook_5_2_0.dtd" >
<hook>
	<custom-jsp-dir>/WEB-INF/jsps</custom-jsp-dir>
</hook>

-Create a new file inside of WEB-INF named liferay-plugin-package.properties with the following contents:

name=customjsp
module-group-id=liferay
module-incremental-version=1
tags=
short-description=This hook contains a custom Create Accounts JSP
change-log=
page-url=http://www.liferay.com
author=Liferay, Inc.
licenses=MIT
required-deployment-contexts=

-Create the following folder structure inside of the WEB-INF folder:

jsps/html/portlet/login

-Copy the file, create_account.jsp from liferay-portal-src-5.x.x/portal-web/docroot/html/portlet/login to customjsp-hook/docroot/WEB-INF/jsps/html/portlet/login

Add Custom Fields to Custom Create Account page #

-Open the create_account.jsp file and find the email address portion of the form (Search for key="email-address") -Insert the following code between the ending </div> tag of the mail section and the ending </fieldset> tag

<div class="exp-ctrl-holder">
	<liferay-ui:custom-attribute
		className="<%= User.class.getName() %>"
		classPK="<%= 0 %>"
		editable="<%= true %>"
		label="<%= true %>"
		name="home-town-newspaper"
	/>
</div>

- Save the file -From a command prompt, navigate to the plugins/hooks/customjsp-hook folder and deploy your custom hook using the following command:

ant deploy

-Watch the command window and the console to ensure the hook is deployed and registered successfully and available for use. -Log in to Liferay and from the SIgn In portlet, select Create Account to see the updated JSP with your custom field.

1 Attachment
85552 Views
Average (0 Votes)
The average rating is 0.0 stars out of 5.
Comments
Threaded Replies Author Date
Hi, First of all, thanks for the customization... Anand Abhyankar May 15, 2009 10:52 PM
Hi Anand, What version are you using? This... Jeffrey Handa May 16, 2009 5:49 AM
I am using Liferay standard edition bundled... diba M June 16, 2009 12:47 PM
Hey Jeffrey, Its kind of urgent help I need in... diba M June 17, 2009 11:48 AM
Hi Diba, The user_ table isn't modified when... Jeffrey Handa June 23, 2009 1:32 PM
Hi, Thanks for this good wiki. And what about... Emilie R June 17, 2009 6:39 AM
To access existing attributes you would make... Jeffrey Handa June 23, 2009 1:35 PM
Hey guys, I use v5.2.3. I believe I did... Secret Developer July 1, 2009 7:38 AM
Nothing??? No one can help editing the create... Secret Developer July 6, 2009 7:30 AM
I don't want to be mean, don't get me wrong but... Secret Developer July 9, 2009 6:35 AM
I don't want to be mean, don't get me wrong but... Secret Developer July 9, 2009 6:37 AM
Found the problem. In liferay-hook.xml file... Secret Developer July 9, 2009 7:04 AM
Hey Baris, Thanks for pointing out the... Jeffrey Handa November 13, 2009 6:29 PM
Hi Jeffrey, Is it possible to have more... Emilie R October 9, 2009 8:04 AM
Emilie, Did you get this figured out? If not,... Jeffrey Handa November 13, 2009 5:57 PM
Have tried to create a jsp hook but it does not... Flavel --------- August 6, 2009 11:46 AM
Flavel, are you still having problems? If so,... Jeffrey Handa November 13, 2009 5:52 PM
I attempted this, and it all worked fine on my... Krista M Leopold November 3, 2009 8:22 AM
Hi Krista, Can you also post this question in... Jeffrey Handa November 13, 2009 6:00 PM
I did, Jeffrey, and have since resolved my... Krista M Leopold November 16, 2009 8:42 AM
Hi Jeff, Very nicely written article. Thanks! ... Gaurav Talwar May 6, 2010 4:15 AM
Managed to solve this.. Just override the file... Gaurav Talwar May 12, 2010 3:45 AM
Hi Jeff, I have added a custom attribute... Gaurav Talwar May 12, 2010 3:49 AM
Hi - thanks for the article. All works well... David Hunter June 8, 2010 1:16 PM
Hi, Was able to configure as required, looking... Swapnil Gholap August 16, 2010 11:19 PM
Hey Jeffrey ! Thanks for the article .. I'd... Abhay Doshi February 15, 2011 10:49 PM
good work .i have added one field if i want to... bhavishya goyal March 3, 2011 10:17 AM
I was able to get this working on LF6 after I... Leonardo Santos-Macias May 18, 2011 7:50 AM
You can easily change the rendering of a... Liferay Blogger February 21, 2012 2:52 PM
hi guys , can any one tell me how can i add... Tejas patel March 1, 2012 11:19 PM
Hello All, I am using Liferay 6.1 CE version. I... Pradip A Bhatt February 27, 2014 9:10 PM
When defining the custom field from the Control... Jeffrey Handa February 28, 2014 6:19 AM

Hi,
First of all, thanks for the customization of the Create Account portlet.
I have followed all the above steps, and able to get the label for Home Town Newspaper, but not getting the text box to enter the data on the create account portlet.
Any ideas.....?

Thanks in advance
>>> Anand
Posted on 5/15/09 10:52 PM.
Hi Anand,

What version are you using? This won't work with 5.2.2 because you aren't able to give the 'update' permission to the 'guest' role (See http://issues.liferay.com/browse/LPS-3160). You'll need to build from the latest source in the SVN, or wait for the next bundle.
Posted on 5/16/09 5:49 AM in reply to anand abhyankar.
I am using Liferay standard edition bundled tomcat 5.2.3. I followed the above steps and the Create account page do not show the field label or the text box.

And again does it really create the fields to the user_ table in lportal schema? I could not locate the new field in lportal schema, so wonder how it persist.
Posted on 6/16/09 12:47 PM in reply to Jeffrey Handa.
Hi,
Thanks for this good wiki.

And what about existing attributes? For example, the street, the country, ...
Posted on 6/17/09 6:39 AM.
Hey Jeffrey,

Its kind of urgent help I need in this issue.

I am using Liferay standard edition bundled tomcat 5.2.3. I followed the above steps and the Create account page do not show the field label or the text box.

And again does it really create the fields to the user_ table in lportal schema? I could not locate the new field in lportal schema, so wonder how it persist.
Posted on 6/17/09 11:48 AM in reply to Jeffrey Handa.
Hi Diba,

The user_ table isn't modified when using Custom Attributes. The attributes are stored using Liferay's ExpandoService.

I am not sure why you're not seeing the field label or the text box, but I would start by making sure that the hook is working correctly. Are any changes to the JSP showing up after deploying? If the hook is working, then I would double check the permissions on the custom attribute.
Posted on 6/23/09 1:32 PM in reply to diba M.
To access existing attributes you would make use of Liferay's service layer.
Posted on 6/23/09 1:35 PM in reply to Emilie R.
Hey guys,

I use v5.2.3. I believe I did everything perfectly but the text box is not showing up in create account page. Any one has a solution for this?
Posted on 7/1/09 7:38 AM in reply to Jeffrey Handa.
Nothing??? No one can help editing the create account page?????
Posted on 7/6/09 7:30 AM in reply to Baris Sener.
I don't want to be mean, don't get me wrong but can someone fix this issue???

I keep getting this error, that's why it doesn't show up on create account page.

12:56:14,195 INFO [HookHotDeployListener:442] Hook for customjsp-hook was unregistered
12:56:14,250 INFO [HookHotDeployListener:184] Registering hook for customjsp-hook
12:56:14,252 ERROR [HotDeployUtil:111] com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering hook for customjsp-hook
com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering hook for customjsp-hook
at com.liferay.portal.deploy.hot.BaseHotDeployListener.throwHotDeployException(Base­HotDeployListener.java:58)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployLi­stener.java:101)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._doFireDeployEvent(HotDeployU­til.java:108)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._fireDeployEvent(HotDeployUti­l.java:153)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil­.java:43)
at com.liferay.portal.kernel.servlet.HookContextListener.contextInitialized(HookCon­textListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843­)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1116)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1214)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.ja­va:117)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337­)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(Containe­rBase.java:1590)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.liferay.portal.kernel.xml.DocumentException: Error on line 2 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:377)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:388)
at com.liferay.portal.kernel.xml.SAXReaderUtil.read(SAXReaderUtil.java:156)
at com.liferay.portal.deploy.hot.HookHotDeployListener.doInvokeDeploy(HookHotDeploy­Listener.java:191)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployLi­stener.java:98)
... 15 more
Posted on 7/9/09 6:35 AM in reply to Baris Sener.
I don't want to be mean, don't get me wrong but can someone fix this issue???

I keep getting this error, that's why it doesn't show up on create account page.

12:56:14,195 INFO [HookHotDeployListener:442] Hook for customjsp-hook was unregistered
12:56:14,250 INFO [HookHotDeployListener:184] Registering hook for customjsp-hook
12:56:14,252 ERROR [HotDeployUtil:111] com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering hook for customjsp-hook
com.liferay.portal.kernel.deploy.hot.HotDeployException: Error registering hook for customjsp-hook
at com.liferay.portal.deploy.hot.BaseHotDeployListener.throwHotDeployException(Base­HotDeployListener.java:58)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployLi­stener.java:101)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._doFireDeployEvent(HotDeployU­til.java:108)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil._fireDeployEvent(HotDeployUti­l.java:153)
at com.liferay.portal.kernel.deploy.hot.HotDeployUtil.fireDeployEvent(HotDeployUtil­.java:43)
at com.liferay.portal.kernel.servlet.HookContextListener.contextInitialized(HookCon­textListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3843­)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4342)
at org.apache.catalina.startup.HostConfig.checkResources(HostConfig.java:1116)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1214)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:293)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.ja­va:117)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1337­)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1601)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChild­ren(ContainerBase.java:1610)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(Containe­rBase.java:1590)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.liferay.portal.kernel.xml.DocumentException: Error on line 2 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:377)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:388)
at com.liferay.portal.kernel.xml.SAXReaderUtil.read(SAXReaderUtil.java:156)
at com.liferay.portal.deploy.hot.HookHotDeployListener.doInvokeDeploy(HookHotDeploy­Listener.java:191)
at com.liferay.portal.deploy.hot.HookHotDeployListener.invokeDeploy(HookHotDeployLi­stener.java:98)
... 15 more
Posted on 7/9/09 6:37 AM in reply to Baris Sener.
Found the problem. In liferay-hook.xml file remove the " [ ] " around the link.

After doing this, if the text box is still not showing up in the create account page, add this statement to your liferay-hook.xml file.

<custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir>

Cheers!!
Posted on 7/9/09 7:04 AM in reply to Baris Sener.
Have tried to create a jsp hook but it does not seem to override the jsp. Have the war file, if anybody is interested in helping.
Posted on 8/6/09 11:46 AM.
Hi Jeffrey,

Is it possible to have more details, please?

What I exactly want to do is adding a zip field in the create account page. Do I have to use the custom attribute, or is it a way to add the existing zip field of the address?

At the moment, I can add the field "graphically", but the value isn't stored anyway...

Any idea?
Posted on 10/9/09 8:04 AM in reply to Jeffrey Handa.
I attempted this, and it all worked fine on my workstation, but when I tried to deploy on my test server, which is running in a non-root context, it bombed. Does it have something to do with the war file randomly adding a version to the name of the war file, as in customjsp-hook-5.2.3.1.war ? Please help!
Posted on 11/3/09 8:22 AM.
Flavel, are you still having problems? If so, can you post a message in the forums with the war file?
Posted on 11/13/09 5:52 PM in reply to Flavel ---------.
Emilie,

Did you get this figured out? If not, can you post this question in the forums? You can put a link to your forum post here on this wiki. By keeping all the questions in the forums we will minimize the number of places that community members will need to look for answers. You'll also get a larger pool of possible responders.

BTW - You don't need a custom attribute to store zip, but there would be some steps to persist the zip to the db.
Posted on 11/13/09 5:57 PM in reply to Emilie R.
Hi Krista,

Can you also post this question in the forum? Off the top of my head, I am not sure, but by posting this in the forum you'll get more people looking at the question that might be able to help. If you're already posted in the forum, I apologize, I've been heads-down on some projects and must have missed it.
Posted on 11/13/09 6:00 PM in reply to Krista M Leopold.
Hey Baris,

Thanks for pointing out the mistake. I am not sure why, but creole keeps adding the brackets. I've added a note so hopefully others wont' run into this.
Posted on 11/13/09 6:29 PM in reply to Baris Sener.
I did, Jeffrey, and have since resolved my problem, thanks!!
http://www.liferay.com/web/guest/community/forums/-/message_boards/message/42473­84
Posted on 11/16/09 8:42 AM in reply to Jeffrey Handa.
Hi Jeff,

Very nicely written article. Thanks!

I want to display a checkbox on the create account page. I have managed to get a select with boolean values.

<liferay-ui:custom-attribute
className="<%= User.class.getName() %>"
classPK="<%= 0 %>"
editable="<%= true %>"
label="<%= true %>"
name="email-subscription"
/>

I want to convert the above select to checkbox in JSP file. Any ideas to do it? Thanks
Posted on 5/6/10 4:15 AM.
Managed to solve this.. Just override the file html/taglib/ui/custom_attribute/page.jsp
Posted on 5/12/10 3:45 AM in reply to Gaurav Talwar.
Hi Jeff,

I have added a custom attribute 'postcode' which i have added to enterprise_admin/user/details.jsp. It gets saved correctly but its not populating the field on page load.. Any ideas whats missing?


My code is:

<liferay-ui:custom-attribute
className="<%= User.class.getName() %>"
classPK="<%= 0 %>"
editable="<%= true %>"
label="<%= true %>"
name="postcode"
/>
</div>

Thanks,
Gaurav Talwar
Posted on 5/12/10 3:49 AM.
Hi - thanks for the article.

All works well except the localization.

I have added Language-ext_xx.properties files under :
/hooks/docroot/WEB-INF/classes/content/
however it is not being recognised using the message key method in the Create Account jsp :
<liferay-ui:message key="CustomAttribute" />

Any ideas ? Thanks.
Posted on 6/8/10 1:16 PM.
Hi,
Was able to configure as required, looking forward to add more fields emoticon

Thank you for the article.
Posted on 8/16/10 11:19 PM.
Hey Jeffrey ! Thanks for the article .. I'd like to know how to display the custom attribute in Liferay 6 - as this uses the AUI tags for generating the form etc.

Thanks !
Posted on 2/15/11 10:49 PM.
good work .i have added one field if i want to display this field in another jsp than what should be done for it
Posted on 3/3/11 10:17 AM.
I was able to get this working on LF6 after I followed EXACTLY all the steps in the wiki.

I tried a "teacher" field and activated all permissions and didn't work. It worked only with the hyphens in "home-town-newspaper" and only the permissions in guest and user.

As per the location of the saved data, this table is created and the data saved there

CREATE TABLE `expandocolumn` (
`columnId` bigint(20) NOT NULL,
`companyId` bigint(20) DEFAULT NULL,
`tableId` bigint(20) DEFAULT NULL,
`name` varchar(75) DEFAULT NULL,
`type_` int(11) DEFAULT NULL,
`defaultData` longtext,
`typeSettings` longtext,
PRIMARY KEY (`columnId`),
UNIQUE KEY `IX_FEFC8DA7` (`tableId`,`name`),
KEY `IX_A8C0CBE8` (`tableId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `expandocolumn` VALUES (10407,10132,10406,'aboutMe',15,'',''),(11202,10132,11201,'supervisor\'s name',15,'','indexable=true\n'),(11402,10132,11018,'home-town-newspaper',15,'','­indexable=true\n');
/*!40000 ALTER TABLE `expandocolumn` ENABLE KEYS */;
Posted on 5/18/11 7:50 AM in reply to bhavishya goyal.
You can easily change the rendering of a true/false custom field to a check box with a hook - I described how I did it in this article - http://liferay.bdedov.eu/2012/02/render-truefalse-custom-field-type-as.html
Posted on 2/21/12 2:52 PM in reply to Leonardo Santos-Macias.
hi guys ,
can any one tell me how can i add custom fields on my account page
where user photo ,and other details are shown.
thanks,
Tejas patel.
Posted on 3/1/12 11:19 PM in reply to Liferay Blogger.
Hello All,
I am using Liferay 6.1 CE version. I added my custom attribute in User entity from control panel. I want to add this my custom field in create_account.jsp hook page. I added like this way.

<div class="exp-ctrl-holder">
<liferay-ui:custom-attribute className="<%=User.class.getName()%>"
classPK="<%=0%>" editable="<%=true%>" label="<%=true%>"
name="user-default-group-name" />

After that i am trying to getting this value in UserLocalService hook class which extends UserLocalServiceWrapper class - (hook). I find each value from this but can not able to get my custom field value.

I am trying to get custom field value like this.

String defaultGroup = (String) User.getExpandoBridge().getAttribute("defaultGroupName");
log.info("Default Group Name :" + defaultGroup);

But I am getting null value or default value which I set at the time of adding custom attribute. I can not find my entered value from jsp page.

Please help me.

Regards :
Pradip Bhatt
pradip.bhatt@aspiresoftware.in
Posted on 2/27/14 9:10 PM.
When defining the custom field from the Control Panel, what Key did you define? In your jsp snippet you're using user-default-group-name but in your service wrapper hook you're using defaultGroupName.
Posted on 2/28/14 6:19 AM in reply to Pradip A Bhatt.