How to use Custom Attributes to enhance Users

Etiquetas: custom attributes

Howto use Custom Attributes automatically and programatically. #

Out-Of-The-Box Features #

Liferay provides a UI and API to allow for the creation of customized attributes associated with any entity generated by Service Builder. These attributes provide for extensibility without the need for extending the Liferay DB schema.

Out of the box Liferay provides Custom Attributes UI for Users and Organizations.

Creating Custom Attributes for Users #

  • Login as an Administrator
  • Navigate to the 'Control Panel' from the 'Dock'
  • Select 'Custom Fields' from the left hand side navigation bar
  • Select 'User'
  • Click 'Add Custom Attribute'
  • Specify the 'Key' for the Custom Attribute. The help icon for this field reads; "The custom attribute key is used to access the attribute programatically through the <liferay-ui:custom-attribute /> tag." More on this later.
  • The Key must not contain spaces or special characters. Use
    • underscores (my_key)
    • dashes (my-key)
    • or camel case (myKey)
  • Specify the type of the attribute. The help icon for this field reads; "Choose the attribute type carefully as once it is defined it cannot be changed."

Note: Only "java.lang.String" type fields can be made searchable. Consider this at time of creation.

  • Specify if the field is hidden. The help icon for this field reads: "Setting a custom attribute to hidden means that the attribute's value is never shown in any user interface besides this one. This allows the attribute to be used for some more obscure and advanced purposes such as acting as a placeholder for custom permissions." This setting can be changed at any time
  • Click "Save"

Updating Custom Attribute settings #

  • Once the attribute is saved, you are returned to the list. Click the new attribute's link to return to the edit view
  • You will notice that the Name is automatically rendered based on a formatting of the 'key', but also note that this rendering does not grant automatic localization. In order to create true localization, define those in the Language.properties files based on the attribute's 'key'
  • There is now an option to specify a default value
  • If the attribute is of type 'java.lang.String', you have the option to make the custom attribute 'Searchable' (this only currently works for User as the underlying service must support integrated custom attribute indexing). The help icon for this field reads; "Setting a custom attribute to searchable means that the value of the attribute will be indexed when the entity (such as User) is modified. Only java.lang.String attributes can be made searchable. Note that when an attribute is newly made searchable, the indexes must be updated before the data is available to search."
  • Click "Save"

Updating Custom Attribute permissions #

  • Once the attribute is again saved, you are returned to the list. Click the 'Actions' button at the right end of the row for a given attribute
  • Click 'Permissions'
  • Assign the actions to the Roles as desired.
  • Note if the field is 'hidden' the attribute is still only visible in this UI.

Viewing and setting Custom Attributes per User instance #

  • Select any user from the user list or visit the My Account view
  • Choose the 'Custom Attributes' link from the right hand side navigation bar
  • Depending on permissions and settings you are presented with the current list of editable and/or not-editable attributes.
  • If the field is editable, enter a value and click 'Save'

Using Custom Attribute tags to display editable or non-editable values #

Using the <liferay-ui:custom-attribute-list /> jsp tag #

  • This tag generates a list of all the (non-hidden, and VIEWable) tags for a given entity type and instance
  • The available parameters are:
    • className, the fully qualified name of the entity (required = true) e.g. com.liferay.portal.model.User
    • classPK, the primaryKey of the entity instance (or zero (0) if there is not currently an instance) (required = true)
    • editable, is this invocation to check UPDATE permission and show as a input field if check returns true (required = false, default = false)
    • label, is the label of the field rendered, if not simply show the raw output of the attribute value (required = false, default = false)
  • A sample invocation looks like:
		<liferay-ui:custom-attribute-list
			className="<%= User.class.getName() %>"
			classPK="<%= selUser != null ? selUser.getUserId() : 0 %>"
			editable="<%= true %>"
		/>

Using the <liferay-ui:custom-attribute /> jsp tag #

  • This tag generates a view of a specific attribute for a given entity type and instance.
  • The available parameters are:
    • className, the fully qualified name of the entity (required = true) e.g. com.liferay.portal.model.User
    • classPK, the primaryKey of the entity instance (or zero (0) if there is not currently an instance) (required = true)
    • editable, is this invocation to check UPDATE permission and show as a input field if check returns true (required = false, default = false)
    • label, is the label of the field rendered, if not simply show the raw output of the attribute value (required = false, default = false)
    • name, the name of the specific attribute to render (required = true)
  • A sample invocation looks like:
	
		<liferay-ui:custom-attribute
			className="<%= User.class.getName() %>"
			classPK="<%= selUser != null ? selUser.getUserId() : 0 %>"
			editable="<%= true %>"
			name="company-name"
		/>

Performing a custom User search using specific custom attributes #

  • The short method for performing an index search for Users is:
	public Hits search(
			long companyId, String keywords, Boolean active,
			LinkedHashMap<String, Object> params, int start, int end, Sort sort)
		throws SystemException;

e.g. searching for Users by 'company-name' attribute

		LinkedHashMap userParams = new LinkedHashMap();
	
		userParams.put("usersOrgs", new Long(organizationId));
		userParams.put("company-name", "My Company");

		boolean asc = true;
		Sort sort = new Sort("lastName", Sort.STRING_TYPE, asc);

		Hits hits = UserLocalServiceUtil.search(
			companyId, null, true, userParams, 0, 20, sort);
	
		List<User> users = new ArrayList<User>();

		List<Document> hitsList = hits.toList();

		for (Document doc : hitsList) {
			long userId = GetterUtil.getLong(doc.get(Field.USER_ID));

			users.add(UserLocalServiceUtil.getUserById(userId));
		}

e.g. searching for users where a value can be found in any indexed field, as well as in custom attributes

		LinkedHashMap userParams = new LinkedHashMap();


		userParams.put("usersOrgs", new Long(organizationId));


		boolean asc = true;
		Sort sort = new Sort("lastName", Sort.STRING_TYPE, asc);


		Hits hits = UserLocalServiceUtil.search(
			companyId, "My Company", true, userParams, 0, 20, sort);


		List<User> users = new ArrayList<User>();


		List<Document> hitsList = hits.toList();


		for (Document doc : hitsList) {
			long userId = GetterUtil.getLong(doc.get(Field.USER_ID));


			users.add(UserLocalServiceUtil.getUserById(userId));
		}

Setting the value for a specific user programatically. #

	user.getExpandoBridge().setAttribute("company-name", "My Company");
0 archivos adjuntos
54279 Accesos
Promedio (6 Votos)
La valoración media es de 3.16666666666667 estrellas de 5.
Comentarios
Respuestas anidadas Autor Fecha
Hi ray! Thank you for another very good... Ivano Carrara 4 de noviembre de 2008 1:17
Hi Ray! This is a good feature to use tables:... Jonas Yuan 29 de diciembre de 2008 17:32
One more question: do the attribute name and... Jonas Yuan 29 de diciembre de 2008 17:51
Name: yes (use the name "key" of the attribute... Ray Augé 2 de octubre de 2009 9:55
i am using liferay 6.i have created some custom... ankit yakkundi 7 de septiembre de 2010 23:01
It is available to any entity. ... Ray Augé 2 de octubre de 2009 9:58
this article show how to display custom... François LE QUEMENER 7 de octubre de 2009 7:33
Hi Ray! Great article. Question - how would... Jim Klo 29 de octubre de 2009 17:53
Hi All, Instead of setting the value... ilke Muhtaroglu 17 de noviembre de 2009 4:59
You solve your problem? Lucas Roberto Thomaz 1 de diciembre de 2009 11:45
Did you try casting it to "big" Integer? Maciej Nowicki 8 de junio de 2010 2:16
What is the meaning of setting the type(Text... pooja shah 5 de agosto de 2010 6:11
Hi all, can any body say is this... Path Finder LifeRay 15 de diciembre de 2009 6:31
We have added custom attributes, and are doing... whyBish valid 15 de diciembre de 2009 22:01
Hi Ray, I want to know, if I have to use... vikash kumar chaurasia 24 de febrero de 2010 2:16
Somebody knows how to retrieve custom... Pablo Krause 7 de abril de 2010 11:19
You can use ... Murat ÇOLAK 27 de enero de 2011 3:35
Is there an equivalent tutorial for use in... Abhay Doshi 15 de febrero de 2011 22:52
I create 1 custom attribute, and I put this at... Moisés Belda 2 de junio de 2011 1:24
Great Explenation Maybe there should be added... Jakob Fahrner 3 de agosto de 2011 7:07
How to remove the hours and minutes of such... Pablo Antonio Zamora Ortiz 29 de enero de 2012 13:15
I have select list with three values as custom... Shantanu Puranik 5 de febrero de 2012 22:36
Check how I changed the render type of... Liferay Blogger 21 de febrero de 2012 14:45
How to fetch the value of custom field which is... Praveen P 20 de abril de 2012 22:56
Did anybody find the answer to this? I have a... Johny black 15 de mayo de 2012 14:33
Anybody know how to get the selected value in... me liferay 8 de enero de 2013 2:57

Hi ray! Thank you for another very good article!

One only thing... I think it is a good thing to evidentiate in tags or in the text or in everyplace, the Version of Liperay Portal where is applicable the argument of the Article...

Thank you!

Ivano Carrara
Publicado el día 4/11/08 1:17.
Hi Ray! This is a good feature to use tables: expando-table (and -column, -row, -value, also). The portlet 139 (Expando) is used for custom attributes of users, as shown in above example.

On 4/24/08 6:28, you have shown another usage: expandotable (-column, -row, -value) by article templates. It is really cool!

One question: can it be used for other ClassNameId? such as group_, organization_, user group?

Maybe, just add portlet 139 in control panel as Admin function. Make sense?

Thanks

Jonas Yuan
Publicado el día 29/12/08 17:32.
One more question: do the attribute name and value support locale?

Thanks

Jonas Yuan
Publicado el día 29/12/08 17:51 en respuesta a Jonas Yuan.
Name: yes (use the name "key" of the attribute in the Language-ext files)
Value: no
Publicado el día 2/10/09 9:55 en respuesta a Jonas Yuan.
It is available to any entity.

<liferay-portlet:renderURL windowState="<%= WindowState.MAXIMIZED.toString() %>" var="expandoURL" portletName="<%= PortletKeys.EXPANDO %>">
<portlet:param name="struts_action" value="/expando/view_attributes" />
<portlet:param name="redirect" value="<%= currentURL %>" />
<portlet:param name="modelResource" value="<%= User.class.getName() %>" />
</liferay-portlet:renderURL>

<a href="<%= expandoURL %>"><liferay-ui:message key="custom-attributes" /></a>

Change the "modelResource" attribute and replace it with your entity's className.
Publicado el día 2/10/09 9:58 en respuesta a Jonas Yuan.
this article show how to display custom attribute, but not how to use them programatically in a form.
Publicado el día 7/10/09 7:33.
Hi Ray!

Great article. Question - how would search both regular attributes and custom attributes. In the example "usersOrgs" i'm assuming that maps to the User.getOrganizations(), but how do I find out what the other fields are named?

Thanks,

- Jim
Publicado el día 29/10/09 17:53.
Hi All,

Instead of setting the value programatically, I want to get it !

instead of user.getExpandoBridge().setAttribute("company-name", "My Company");

user.getExpandoBridge().getAttribute("sessionNo");

but getAttribute returns the result as Serializable !!! But my attribute is int !
how can i change this value? When i just cast it to int the result is "null" !?

any idea is welcome !

ilke
Publicado el día 17/11/09 4:59.
You solve your problem?
Publicado el día 1/12/09 11:45 en respuesta a ilke Muhtaroglu.
Hi all,
can any body say is this custom attribute used for themes also.
If yes, just rep me the link which gives the resolution of that. I m seriously stuck at themes concept.

Thanx in advance,

Regards,
Path finder Liferay.
Publicado el día 15/12/09 6:31.
We have added custom attributes, and are doing custom attribute search. It all works OK except that if the search criteria value has a space in it, the results that have the value are not found. E.G. if I create attribute country and create a user with country=New Zealand then I can search for "new" and find the user, "zealand" and find the user, but "new zealand" returns no results at all!!
Publicado el día 15/12/09 22:01.
Hi Ray,

I want to know, if I have to use Custom Attributes in my portlet code, do I need to switch to Ext envt or is it possible in plugins envt? Currently, I have done the development in plugin envt.

Thanks in advance.
Publicado el día 24/02/10 2:16 en respuesta a whyBish valid.
Somebody knows how to retrieve custom properties from a user that was retrieved using the Portal_UserService web service?
When you get hold of a user using the local API, you can get the custom attributes using
1user.getExpandoBridge().getAttribute("myAttribute");
but there is no such method in the UserSoap object that is returned by the Portal_UserService web service.

Any help greatly appreciated.
Publicado el día 7/04/10 11:19.
Did you try casting it to "big" Integer?
Publicado el día 8/06/10 2:16 en respuesta a ilke Muhtaroglu.
What is the meaning of setting the type(Text Box,TextBox indexed,Textfield secret etc.) of custom attribute in ORGANISATION or USER portlet(Liferay 5.0).....will v get texboxes when we assign these attributes,values????
n hw can v access these custom attributes,coz they are not attached in predefined fields?
Publicado el día 5/08/10 6:11 en respuesta a Maciej Nowicki.
i am using liferay 6.i have created some custom attributes for webcontent,blog,user,etc portlets.how can i search using these attibutes..i am not able to search..
Publicado el día 7/09/10 23:01 en respuesta a Jonas Yuan.
You can use

ExpandoValueLocalServiceUtil.addValue(companyId,User.class.getName(),ExpandoT­ableConstants.DEFAULT_TABLE_NAME,"parametername",user.getUserId(),"data");

this one too, emoticon
Publicado el día 27/01/11 3:35.
Is there an equivalent tutorial for use in Liferay 6?
Publicado el día 15/02/11 22:52.
I create 1 custom attribute, and I put this at create_account.jsp (with EXT plugin).

I need that this field will be REQUIRED... I think the way is checking value returned (jsp) at CreateAccountAction.java , and if equals("") launch exception at CreateAcctountAction.addUser method....

But I don't know how access to the value of liferay-ui:custom-attribute, ParamUtil. returns always "", empty value.
Publicado el día 2/06/11 1:24 en respuesta a Abhay Doshi.
Great Explenation
Maybe there should be added some approach from the forum (http://www.liferay.com/de/community/forums/-/message_boards/message/4339723) of getting the Attribute user.getExpandoBridge().getAttribute("myAttribute"); That you have to cast it to the right type and set the right permissions (view permissions for guest)
Publicado el día 3/08/11 7:07 en respuesta a Rick Dangerous.
How to remove the hours and minutes of such date in a custom field?

not show the hours and minutes in the input
Publicado el día 29/01/12 13:15.
I have select list with three values as custom attribute on document of Document library portlet. Need is to display two values in select list to noraml user and all three values to power user. Any possibility to achive this?
Publicado el día 5/02/12 22:36.
Check how I changed the render type of True/False custom field from a drop-down box to a checkbox - http://liferay.bdedov.eu/2012/02/render-truefalse-custom-field-type-as.html
Publicado el día 21/02/12 14:45 en respuesta a Shantanu Puranik.
How to fetch the value of custom field which is of type selection of text value???

(String)user.getExpandoBridge.getAttribute("Name of custom field") is giving the output like [ljava.string.....
Publicado el día 20/04/12 22:56 en respuesta a Liferay Blogger.
Did anybody find the answer to this?

I have a property called "companyAddress" but when I do

(String)user.getExpandoBridge().getAttribute("companyAddress");

I don't get anything I get a blank.
Publicado el día 15/05/12 14:33 en respuesta a Praveen P.
Anybody know how to get the selected value in String arrays(DropDown)?
Publicado el día 8/01/13 2:57 en respuesta a Johny black.