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

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
Publicado el día 15/05/09 22:52.
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.
Publicado el día 16/05/09 5:49 en respuesta a 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.
Publicado el día 16/06/09 12:47 en respuesta a Jeffrey Handa.
Hi,
Thanks for this good wiki.

And what about existing attributes? For example, the street, the country, ...
Publicado el día 17/06/09 6:39.
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.
Publicado el día 17/06/09 11:48 en respuesta a 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.
Publicado el día 23/06/09 13:32 en respuesta a diba M.
To access existing attributes you would make use of Liferay's service layer.
Publicado el día 23/06/09 13:35 en respuesta a 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?
Publicado el día 1/07/09 7:38 en respuesta a Jeffrey Handa.
Nothing??? No one can help editing the create account page?????
Publicado el día 6/07/09 7:30 en respuesta a 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
Publicado el día 9/07/09 6:35 en respuesta a 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
Publicado el día 9/07/09 6:37 en respuesta a 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!!
Publicado el día 9/07/09 7:04 en respuesta a 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.
Publicado el día 6/08/09 11:46.
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?
Publicado el día 9/10/09 8:04 en respuesta a 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!
Publicado el día 3/11/09 8:22.
Flavel, are you still having problems? If so, can you post a message in the forums with the war file?
Publicado el día 13/11/09 17:52 en respuesta a 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.
Publicado el día 13/11/09 17:57 en respuesta a 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.
Publicado el día 13/11/09 18:00 en respuesta a 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.
Publicado el día 13/11/09 18:29 en respuesta a 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
Publicado el día 16/11/09 8:42 en respuesta a 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
Publicado el día 6/05/10 4:15.
Managed to solve this.. Just override the file html/taglib/ui/custom_attribute/page.jsp
Publicado el día 12/05/10 3:45 en respuesta a 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
Publicado el día 12/05/10 3:49.
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.
Publicado el día 8/06/10 13:16.
Hi,
Was able to configure as required, looking forward to add more fields emoticon

Thank you for the article.
Publicado el día 16/08/10 23:19.
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 !
Publicado el día 15/02/11 22:49.
good work .i have added one field if i want to display this field in another jsp than what should be done for it
Publicado el día 3/03/11 10:17.
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 */;
Publicado el día 18/05/11 7:50 en respuesta a 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
Publicado el día 21/02/12 14:52 en respuesta a 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.
Publicado el día 1/03/12 23:19 en respuesta a 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
Publicado el día 27/02/14 21:10.
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.
Publicado el día 28/02/14 6:19 en respuesta a Pradip A Bhatt.