« Back

[HOWTO] Personalization - Getting current user attributes

Company Blogs May 16, 2008 By Ray Augé Staff

Portals are all about personalization, and Liferay is no different.

I mean, there are many, many other things involved in portals other than personalization. But none of them really make sense without it.

Personalization is the dressing that makes your portal salad bearable. So, it has to be pretty good dressing.

Beyond the portal and personalization, we have the reason the portal exists at all: Portlets. Portlets are the ingredients needed to make a portal salad worth eating.

  1. You have your greens (the hard working portlets: E-Commerce, BI, BPM, CMS, etc.).
  2. Then you have your non-green veggies (Email, Forums, Blogs, Wiki, etc.).
  3. Finally you have your crutons and bacon bits, the stuff we know we can live without but just can't bring ourselves to leave out (social networking and other generally superfluous gadgets, widgetry, and such).

Yet it doesn't end there, it's gotta be MY salad, not someone else's... and it's gotta have MY name on it too. Who wants to eat someone else's food anyway...

That said, personalization at the portlet level is very important. There are two types of portlet personalization mechanisms available in Liferay.

  1. Personalization defined by the Portlet spec(s) (low fat, low cal. dressing)
  2. Liferay personalization (fatty, rich, tasty dressing)

Let's go through putting both to use in your Portlet application and then you can decide for yourself which best suites your diet.

Personalizaton via Portlet spec(s)

Both the 1.0 version and 2.0 (upcomming) versions of the spec define a mechanism called "user-attributes" defined as such:

"The deployment descriptor of a portlet application must define the user attribute names the portlets use. The following example shows a section of a deployment descriptor defining a few user attributes:

<portlet-app>
	...
	<user-attribute>
		<description>User Given Name</description>
		<name>user.name.given</name>
	</user-attribute>
	<user-attribute>
		<description>User Last Name</description>
		<name>user.name.family</name>
	</user-attribute>
	<user-attribute>
		<description>User eMail</description>
		<name>user.home-info.online.email</name>
	</user-attribute>
	<user-attribute>
		<description>Company Organization</description>
		<name>user.business-info.postal.organization</name>
	</user-attribute>
	...
<portlet-app>
A deployer must map the portlet application’s logical user attributes to the corresponding user attributes offered by the runtime environment. At runtime, the portlet container uses this mapping to expose user attributes to the portlets of the portlet application. User attributes of the runtime environment not mapped as part of the deployment process must not be exposed to portlets."

[JavaTM Portlet Specification, version 1.0, PLT.17.1]

This means that without having to do anything other than configuration, the portlet is requesting access to the defined user attributes. The list of available attributes can be found in the spec docs.

The next part is of course getting access to those user attributes at runtime.

Here is a complete JSP which handles getting the user.name.given attribute.

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

<%@ page import="javax.portlet.PortletRequest"%>
<%@ page import="java.util.Map"%>

<portlet:defineObjects />

<%
Map userInfo = (Map)renderRequest.getAttribute(PortletRequest.USER_INFO);
String givenName = (userInfo != null) ? (String)userInfo.get("user.name.given") : "";
%>

Given Name: <%= givenName %>

The result looks something like the following:

[Image 1]

That's 100% spec complient and includes the following list of attributes:

package com.liferay.portlet;

public class UserAttributes {

	// Mandatory Liferay attributes

	public static final String LIFERAY_COMPANY_ID = "liferay.company.id";

	public static final String LIFERAY_USER_ID = "liferay.user.id";

	public static final String USER_NAME_FULL = "user.name.full";

	// See page 119 of the JSR 168 spec

	public static final String USER_BDATE = "user.bdate";

	public static final String USER_GENDER = "user.gender";

	public static final String USER_EMPLOYER = "user.employer";

	public static final String USER_DEPARTMENT = "user.department";

	public static final String USER_JOBTITLE = "user.jobtitle";

	public static final String USER_NAME_PREFIX = "user.name.prefix";

	public static final String USER_NAME_GIVEN = "user.name.given";

	public static final String USER_NAME_FAMILY = "user.name.family";

	public static final String USER_NAME_MIDDLE = "user.name.middle";

	public static final String USER_NAME_SUFFIX = "user.name.suffix";

	public static final String USER_NAME_NICKNAME = "user.name.nickName";

	public static final String USER_HOME_INFO_POSTAL_NAME = "user.home-info.postal.name";

	public static final String USER_HOME_INFO_POSTAL_STREET = "user.home-info.postal.street";

	public static final String USER_HOME_INFO_POSTAL_CITY = "user.home-info.postal.city";

	public static final String USER_HOME_INFO_POSTAL_STATEPROV = "user.home-info.postal.stateprov";

	public static final String USER_HOME_INFO_POSTAL_POSTALCODE = "user.home-info.postal.postalcode";

	public static final String USER_HOME_INFO_POSTAL_COUNTRY = "user.home-info.postal.country";

	public static final String USER_HOME_INFO_POSTAL_ORGANIZATION = "user.home-info.postal.organization";

	public static final String USER_HOME_INFO_TELECOM_TELEPHONE_INTCODE = "user.home-info.telecom.telephone.intcode";

	public static final String USER_HOME_INFO_TELECOM_TELEPHONE_LOCCODE = "user.home-info.telecom.telephone.loccode";

	public static final String USER_HOME_INFO_TELECOM_TELEPHONE_NUMBER = "user.home-info.telecom.telephone.number";

	public static final String USER_HOME_INFO_TELECOM_TELEPHONE_EXT = "user.home-info.telecom.telephone.ext";

	public static final String USER_HOME_INFO_TELECOM_TELEPHONE_COMMENT = "user.home-info.telecom.telephone.comment";

	public static final String USER_HOME_INFO_TELECOM_FAX_INTCODE = "user.home-info.telecom.fax.intcode";

	public static final String USER_HOME_INFO_TELECOM_FAX_LOCCODE = "user.home-info.telecom.fax.loccode";

	public static final String USER_HOME_INFO_TELECOM_FAX_NUMBER = "user.home-info.telecom.fax.number";

	public static final String USER_HOME_INFO_TELECOM_FAX_EXT = "user.home-info.telecom.fax.ext";

	public static final String USER_HOME_INFO_TELECOM_FAX_COMMENT = "user.home-info.telecom.fax.comment";

	public static final String USER_HOME_INFO_TELECOM_MOBILE_INTCODE = "user.home-info.telecom.mobile.intcode";

	public static final String USER_HOME_INFO_TELECOM_MOBILE_LOCCODE = "user.home-info.telecom.mobile.loccode";

	public static final String USER_HOME_INFO_TELECOM_MOBILE_NUMBER = "user.home-info.telecom.mobile.number";

	public static final String USER_HOME_INFO_TELECOM_MOBILE_EXT = "user.home-info.telecom.mobile.ext";

	public static final String USER_HOME_INFO_TELECOM_MOBILE_COMMENT = "user.home-info.telecom.mobile.comment";

	public static final String USER_HOME_INFO_TELECOM_PAGER_INTCODE = "user.home-info.telecom.pager.intcode";

	public static final String USER_HOME_INFO_TELECOM_PAGER_LOCCODE = "user.home-info.telecom.pager.loccode";

	public static final String USER_HOME_INFO_TELECOM_PAGER_NUMBER = "user.home-info.telecom.pager.number";

	public static final String USER_HOME_INFO_TELECOM_PAGER_EXT = "user.home-info.telecom.pager.ext";

	public static final String USER_HOME_INFO_TELECOM_PAGER_COMMENT = "user.home-info.telecom.pager.comment";

	public static final String USER_HOME_INFO_ONLINE_EMAIL = "user.home-info.online.email";

	public static final String USER_HOME_INFO_ONLINE_URI = "user.home-info.online.uri";

	public static final String USER_BUSINESS_INFO_POSTAL_NAME = "user.business-info.postal.name";

	public static final String USER_BUSINESS_INFO_POSTAL_STREET = "user.business-info.postal.street";

	public static final String USER_BUSINESS_INFO_POSTAL_CITY = "user.business-info.postal.city";

	public static final String USER_BUSINESS_INFO_POSTAL_STATEPROV = "user.business-info.postal.stateprov";

	public static final String USER_BUSINESS_INFO_POSTAL_POSTALCODE = "user.business-info.postal.postalcode";

	public static final String USER_BUSINESS_INFO_POSTAL_COUNTRY = "user.business-info.postal.country";

	public static final String USER_BUSINESS_INFO_POSTAL_ORGANIZATION = "user.business-info.postal.organization";

	public static final String USER_BUSINESS_INFO_TELECOM_TELEPHONE_INTCODE = "user.business-info.telecom.telephone.intcode";

	public static final String USER_BUSINESS_INFO_TELECOM_TELEPHONE_LOCCODE = "user.business-info.telecom.telephone.loccode";

	public static final String USER_BUSINESS_INFO_TELECOM_TELEPHONE_NUMBER = "user.business-info.telecom.telephone.number";

	public static final String USER_BUSINESS_INFO_TELECOM_TELEPHONE_EXT = "user.business-info.telecom.telephone.ext";

	public static final String USER_BUSINESS_INFO_TELECOM_TELEPHONE_COMMENT = "user.business-info.telecom.telephone.comment";

	public static final String USER_BUSINESS_INFO_TELECOM_FAX_INTCODE = "user.business-info.telecom.fax.intcode";

	public static final String USER_BUSINESS_INFO_TELECOM_FAX_LOCCODE = "user.business-info.telecom.fax.loccode";

	public static final String USER_BUSINESS_INFO_TELECOM_FAX_NUMBER = "user.business-info.telecom.fax.number";

	public static final String USER_BUSINESS_INFO_TELECOM_FAX_EXT = "user.business-info.telecom.fax.ext";

	public static final String USER_BUSINESS_INFO_TELECOM_FAX_COMMENT = "user.business-info.telecom.fax.comment";

	public static final String USER_BUSINESS_INFO_TELECOM_MOBILE_INTCODE = "user.business-info.telecom.mobile.intcode";

	public static final String USER_BUSINESS_INFO_TELECOM_MOBILE_LOCCODE = "user.business-info.telecom.mobile.loccode";

	public static final String USER_BUSINESS_INFO_TELECOM_MOBILE_NUMBER = "user.business-info.telecom.mobile.number";

	public static final String USER_BUSINESS_INFO_TELECOM_MOBILE_EXT = "user.business-info.telecom.mobile.ext";

	public static final String USER_BUSINESS_INFO_TELECOM_MOBILE_COMMENT = "user.business-info.telecom.mobile.comment";

	public static final String USER_BUSINESS_INFO_TELECOM_PAGER_INTCODE = "user.business-info.telecom.pager.intcode";

	public static final String USER_BUSINESS_INFO_TELECOM_PAGER_LOCCODE = "user.business-info.telecom.pager.loccode";

	public static final String USER_BUSINESS_INFO_TELECOM_PAGER_NUMBER = "user.business-info.telecom.pager.number";

	public static final String USER_BUSINESS_INFO_TELECOM_PAGER_EXT = "user.business-info.telecom.pager.ext";

	public static final String USER_BUSINESS_INFO_TELECOM_PAGER_COMMENT = "user.business-info.telecom.pager.comment";

	public static final String USER_BUSINESS_INFO_ONLINE_EMAIL = "user.business-info.online.email";

	public static final String USER_BUSINESS_INFO_ONLINE_URI = "user.business-info.online.uri";

}

It should be noted that as of right now, we haven't implemented the additonal attributes defined in javax.portlet.PortletRequest.P3PUserInfos, of the current version 2.0 recommendation.



Liferay Personalizaton

Liferay personalization provides access to pojos which can be interacted with in a much richer fashion than above. It also ties your application to Liferay so, if you plan to target more than just Liferay as plaform, don't use it.

The easiest way to take avantage of Liferay personalization is to use a couple of Liferay's taglibs; more precisely, the liferay-theme and liferay-ui taglibs.

Since these are not included in a deployed portlet by default, add the following property to your liferay-plugin-package.properties file:

portal.dependency.tlds=\
    liferay-ui.tld,\
    liferay-theme.tld

and the following taglib definitions to your web.xml:

	<taglib>
		<taglib-uri>http://liferay.com/tld/theme</taglib-uri>
		<taglib-location>/WEB-INF/tld/liferay-theme.tld</taglib-location>
	</taglib>
	<taglib>
		<taglib-uri>http://liferay.com/tld/ui</taglib-uri>
		<taglib-location>/WEB-INF/tld/liferay-ui.tld</taglib-location>
	</taglib>

Now that we have the right tools available, let's start with the very simplest example that gives us the most bang.

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>

<liferay-theme:defineObjects />
<portlet:defineObjects />

<liferay-ui:user-display userId="<%= user.getUserId() %>" />

The result of the above JSP code is something like the following:

[Image 2]

Now you might be asking yourself "Where did that user object suddenly come from?"

The answer is simple! It comes from the <liferay-theme:defineObjects /> tag we used. This tag puts a whole bunch of context sensitive objects into our JSP page context, including the com.liferay.portal.model.User pojo associated with the current user..

The objects that are injected into the pageContext by the <liferay-theme:defineObjects /> tag are:

  • themeDisplay - com.liferay.portal.theme.ThemeDisplay
  • company - com.liferay.portal.model.Company
  • account - com.liferay.portal.model.Account (deprecated)
  • user - com.liferay.portal.model.User
  • realUser - com.liferay.portal.model.User
  • contact - com.liferay.portal.model.Contact
  • ?layout - com.liferay.portal.model.Layout
  • ?layouts - List<com.liferay.portal.model.Layout>
  • plid - java.lang.Long
  • ?layoutTypePortlet - com.liferay.portal.model.LayoutTypePortlet
  • portletGroupId - java.lang.Long
  • permissionChecker - com.liferay.portal.security.permission.PermissionChecker
  • locale - java.util.Locale
  • timeZone - java.util.TimeZone
  • theme - com.liferay.portal.model.Theme
  • colorScheme - com.liferay.portal.model.ColorScheme
  • portletDisplay - com.liferay.portal.theme.PortletDisplay

Wow, now that's lots of bacon!!! I like bacon!

I won't try to explain the use of all of these objects here on this post. Suffice it to say that after exploring all of these objects and the remaining tags available to you in the Liferay taglibs, you shouldn't run out of personalization options any time soon.

You can't use JSP taglibs?

Never fear! All these objects are actually obtained by various getter methods on the com.liferay.portal.theme.ThemeDisplay object. We just put them right into the page context to cut down on code.

So, all you need is the themeDisplay object, and you can get it from the portletRequest like so:

ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);

themeDisplay.getCompany();
themeDisplay.getAccount();
themeDisplay.getUser();
themeDisplay.getRealUser();
themeDisplay.getContact();

if (themeDisplay.getLayout() != null) {
	themeDisplay.getLayout();
}

if (themeDisplay.getLayouts() != null) {
	themeDisplay.getLayouts();
}

themeDisplay.getPlid());

if (themeDisplay.getLayoutTypePortlet() != null) {
	themeDisplay.getLayoutTypePortlet();
}

new Long(themeDisplay.getPortletGroupId());
themeDisplay.getPermissionChecker();
themeDisplay.getLocale();
themeDisplay.getTimeZone();
themeDisplay.getTheme();
themeDisplay.getColorScheme();
themeDisplay.getPortletDisplay();

Well, feel to ask questions about the ones for which the meanings are less than obvious. The Message Boards are also full of answers regarding various personalization topics. And remember to have a look at the API docs on the site for the methods provided from each of these objects.

Threaded Replies Author Date
Really nice Ray! A great starting point to... Alvaro Del Castillo May 19, 2008 9:03 AM
Thanks! :) Ray Augé May 19, 2008 9:08 AM
I really like your [HOWTO]'s. Thank you very much! Thomas Ballerstedt May 19, 2008 1:27 PM
Much appreciated! :) Ray Augé May 19, 2008 1:34 PM
I like this HOWTO very much, but I still have a... Hans-Georg Menz June 23, 2008 9:02 AM
Thank you very much! I'm not a *Faces export... Ray Augé June 23, 2008 9:24 AM
Insert the following in web.xml ... Ruben Gerad Mathew May 28, 2010 1:51 PM
<b>No tag "user-display" defined in tag library... aligator fred August 26, 2008 3:14 PM
as im the only unhappy one here im hoping its... aligator fred August 26, 2008 3:16 PM
What environment are you working with a portlet... Ray Augé August 26, 2008 5:50 PM
Hi Ray, I did all the settings, but I always... M Murali Krishna Reddy September 1, 2008 9:29 AM
hi murali, i have followed... Auditya manikanta Vadrevu September 1, 2008 8:25 PM
Hi Ray, I am trying to get this working, but I... Jane Eriksson April 27, 2009 2:29 AM
The elements under "portlet-app" go in the... Ray Augé April 27, 2009 11:28 AM
I did try to put it there but I still get the... Jane Eriksson April 29, 2009 7:30 AM
Thanks for this post. I have a question on a... Pierre Denis October 30, 2008 4:45 AM
Hi Ray, Is it possible to achieve rules based... Raju A K November 4, 2008 2:39 AM
Do you have some references I can read on both... Ray Augé November 4, 2008 5:24 AM
Sorry for delay. Rules based personalization:... Raju A K November 8, 2008 6:20 AM
hi i got good idea about personalization, but i... nidhi singh December 1, 2008 3:51 AM
How do you display any of the themeDisplay... Michael Poznecki March 23, 2009 10:44 AM
I'm not sure what you're asking! Can you... Ray Augé March 23, 2009 11:02 AM
I'll make a quick blog post about this since I... Ray Augé March 23, 2009 11:03 AM
Hi Ray thank you for the HOWTO but i still have... touka touka April 7, 2011 7:26 AM
How are you using that field? I don't know if... Ray Augé April 7, 2011 7:28 AM
Thanks for the post Ray. I would like to fetch... Aravind Kumar April 2, 2009 10:35 PM
List<UserGroup>... Ray Augé April 27, 2009 11:38 AM
I was trying to figure out how to get the phone... Jeff M. Tackett May 13, 2009 6:09 AM
Nevermind, I have found a way to get the phone... Jeff M. Tackett May 13, 2009 2:33 PM
Hi Ray, Very helpful post! But I am having... Garry Seto May 19, 2009 6:13 AM
Hi, I have a question with regard to... Ruben Gerad Mathew May 28, 2010 1:30 PM
Not sure why you have to get the RenderRequest... Ray Augé May 28, 2010 2:04 PM
Thank you Ray, I really appreciate your... Ruben Gerad Mathew May 29, 2010 12:57 PM
Hello, Can anyone tell me how to get user... Kevin Gebel June 10, 2010 2:01 AM
There should be no difference with Vaadin,... Ray Augé September 13, 2010 3:52 PM
Very nice article. Do you know how to also... Mark Ricard September 13, 2010 12:53 PM
Mark, I responded to the post directly. :) Ray Augé September 13, 2010 1:06 PM
Thanks, but I am having trouble getting your... Philipp Bouillon January 4, 2011 6:44 AM
Arrgh! Ok, sorry: portlet-app. I see. I forgot... Philipp Bouillon January 4, 2011 6:55 AM
I'm using liferay 6. I would like to get... Its Me January 25, 2011 7:45 AM
how can i do this here, to get some name of... Its Me January 25, 2011 7:47 AM
Okay, not very compicated. Described as aboth.... Its Me January 25, 2011 8:24 AM
Ray: I think I've followed your instructions to... Abhay Doshi January 31, 2011 1:24 AM
It took me a few minutes to see the problem... Ray Augé January 31, 2011 6:49 AM
Sir, you are awesome ! Thank you :) Abhay Doshi January 31, 2011 6:59 AM
Thank you! I'm glad to help. Ray Augé January 31, 2011 7:10 AM
Quick question: How can I access the user's... Abhay Doshi February 15, 2011 9:39 AM
hello ray... i got dis error while runnig my... prabhakar m February 25, 2011 7:36 AM
You aren't accessing the jsp through a portlet,... Ray Augé February 25, 2011 8:48 AM
yes sir im accessing through the portal only.. prabhakar m February 25, 2011 9:42 PM
im getting error in the portlet itself. prabhakar m February 25, 2011 9:44 PM
And the code snippet I am using to read the... prabhakar m February 25, 2011 9:58 PM
i want to dispaly the current user id who is... prabhakar m February 25, 2011 10:07 PM
mr.ray thanks for such a useful blog.. i hope i... prabhakar m February 25, 2011 10:12 PM
yes mr. ray i got user id and email... itz only... prabhakar m February 28, 2011 11:56 PM
[...] On top of that if you include... Anonymous May 12, 2011 3:08 AM
[...] On top of that if you include... Anonymous May 12, 2011 3:18 AM
Super helpful! Suraj Bihari November 15, 2011 1:43 PM
hi all.i have different issue regarding... krishna kumar March 5, 2012 3:40 AM
i need portlet to be personalized based on... krishna kumar March 5, 2012 3:42 AM
Who would be setting this value; the user, the... Ray Augé March 5, 2012 11:38 AM
[...] Para obtener el nombre del usuario o... Anonymous June 21, 2012 6:23 AM
[...] Para obtener el nombre del usuario o... Anonymous June 21, 2012 6:23 AM
Thank you for the [HOWTO] Best regards. Mihail Kirilov August 26, 2012 1:55 PM
Useful stuff ever..!! Thanks -Vishal Vishal Panchal August 27, 2012 11:10 PM
[...]... Anonymous September 8, 2012 11:25 AM
[...]... Anonymous December 26, 2013 4:36 AM

Really nice Ray! A great starting point to understand personalization. I hope some of these contents reach the developer guide.
Posted on 5/19/08 9:03 AM.
Posted on 5/19/08 9:08 AM in reply to Alvaro Del Castillo.
I really like your 's. Thank you very much!
Posted on 5/19/08 1:27 PM.
Much appreciated! emoticon
Posted on 5/19/08 1:34 PM in reply to Thomas Ballerstedt.
I like this HOWTO very much, but I still have a question. Maybe you can help me with that. Do you know how to get the req in

ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);

in an ICEFaces portlet?
Posted on 6/23/08 9:02 AM.
Thank you very much!

I'm not a *Faces export by any means, BUT I think that in general you can do something like this:

FacesContext fCtx = FacesContext.getCurrentInstance();

PortletRequest req = (PortletRequest)fCtx.getExternalContext().getRequest();

_themeDisplay = (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);

Please note that a common mistake with *Faces is that "some" requests aren't actually handled by the portal. Those will never have portal attributes, like "ThemeDisplay" in them, unless you add some sort of special handling.
Posted on 6/23/08 9:24 AM in reply to Hans-Georg Menz.
<b>No tag "user-display" defined in tag library imported with prefix "liferay-ui"</b> error..

there is no file liferay-plugin-package.properties either ? ...
Posted on 8/26/08 3:14 PM.
as im the only unhappy one here im hoping its just me being a liferay anoying noob emoticon
Posted on 8/26/08 3:16 PM in reply to aligator fred.
What environment are you working with a portlet or a theme? In ext or SDK?
Posted on 8/26/08 5:50 PM in reply to aligator fred.
Hi Ray,

I did all the settings, but I always get null for all the objects like themeDisplay and user etc.
Posted on 9/1/08 9:29 AM in reply to Ray Augé.
hi murali,

i have followed the steps and i have got the name and userid. specify the changes what u have done
Posted on 9/1/08 8:25 PM in reply to M Murali Krishna Reddy.
Thanks for this post. I have a question on a topic related to personalization.

When a logged user adds, positions or emove a portlet on a page of the portal, the page configuration is persisted somewhere; so the user, for his next session, will retrieve the his set of portlets, in his specific layout. For example,
- user Joe has a News portlet on the left and Wheather portlet on the right;
- user Sarah has a Weather portlet on top and a Map Viewer portlet on the bottom.
The same applies for the specific list of pages, with chosen layout templates, configured by each user.

What is exactly the name for this kind of settings (in Liferay or elsewhere)?
Is this standardized (JSR-168 or JSR-286)?

To be clear, my question is at the level of portal page. I DON'T speak here about
- preferences/configuration of one specific portlet
- Liferay themes
- user profiles (name, address, etc)

Thanks
Posted on 10/30/08 4:45 AM.
Hi Ray,

Is it possible to achieve rules based personalization or say unified user profile attributes like one available with weblogic ?
Thanks
Raju
Posted on 11/4/08 2:39 AM.
Do you have some references I can read on both these topics? I've never used the terminology, so I can't really comment on whether we can or can't do it.

Thanks
Posted on 11/4/08 5:24 AM in reply to Raju A K.
Sorry for delay.
Rules based personalization: based on embedded business rules like say if A then B defined say using a drools rules engine or like web sphere which has a rules engine as part of personalization .
Useful link:
http://www.atgsolutionsstrategy.com/2008/09/personalization.html

Unified user Profile is more Weblogic Portal specific which allows to create and manage unified user profiles for personalization.
Heres a useful bea link:
http://edocs.bea.com/wlp/docs92/users/uup.html

Does liferay support use of say drools rules engine just like it supports jBPM for process management .

Thanks
Raju
Posted on 11/8/08 6:20 AM in reply to Ray Augé.
hi
i got good idea about personalization, but i have 1 question that, if i want to change the theme users to user how will i do?
Posted on 12/1/08 3:51 AM.
How do you display any of the themeDisplay values?

Michael
Posted on 3/23/09 10:44 AM in reply to nidhi singh.
I'm not sure what you're asking! Can you explain what you are trying to do?
Posted on 3/23/09 11:02 AM in reply to Michael Poznecki.
I'll make a quick blog post about this since I now have 2 inquires of how to do this.

Coming soon!
Posted on 3/23/09 11:03 AM in reply to nidhi singh.
Thanks for the post Ray. I would like to fetch the Liferay logged User's UserGroup Names. How can I do that?
Posted on 4/2/09 10:35 PM.
Hi Ray,
I am trying to get this working, but I get Given Name : null.
First of all I am working with a portlet and I am using SDK 5.2.2.
I am not sure where to put the <portlet-app> configurations. Is there something that is already written somewhere or shall I write it in some file, in that case, which one. If it is already written, what else can be wrong?
Thanks!
Posted on 4/27/09 2:29 AM in reply to Ray Augé.
The elements under "portlet-app" go in the portlet.xml file of your plugin.

See the JSP spec for relevant details about that (or the examples above).
Posted on 4/27/09 11:28 AM in reply to Jane Eriksson.
List<UserGroup> com.liferay.portal.service.UserGroupLocalServiceUtil.getUserUserGroups(long userId) throws SystemException;
Posted on 4/27/09 11:38 AM in reply to Aravind Kumar.
I did try to put it there but I still get the same answer, Given name: null.
Does it matter where in the file I put it? I have tried both in the top between the <portlet-app>-tags and in the end, still between them, but nothing works.
Posted on 4/29/09 7:30 AM in reply to Ray Augé.
I was trying to figure out how to get the phone number of a user. I am using the themeDisplay object and I just can't find the phone numbers.
Posted on 5/13/09 6:09 AM.
Nevermind, I have found a way to get the phone number.
Posted on 5/13/09 2:33 PM in reply to Jeff M. Tackett.
Hi Ray,

Very helpful post! But I am having trouble with one specific attribute, Job Title. I've followed the code exactly and all the other attributes are retrieved correctly but Job Title keeps on coming back NULL. I am using LifeRay 5.2.2.

Please let me know if you've or anyone has encountered this before or if this is a known issue.

Thanks,
Garry
Posted on 5/19/09 6:13 AM.
Hi,
I have a question with regard to personalization, since portlets reside on the same JVM can I use the static Util class which are also available through spring to create and remove content like shown below using my Icefaces portlets Managed Bean class:

public class KeyAccess {

public void invoke() {
try {
javax.portlet.PortletRequest req = (javax.portlet.PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
RenderRequest renderRequest = (RenderRequest) req.getAttribute("javax.portlet.request");
ThemeDisplay _theme = (ThemeDisplay) req.getAttribute(WebKeys.THEME_DISPLAY);

System.err.println(_theme.getCompany() + " : " + _theme.getCompanyId());
System.out.println(_theme.getUser().getFullName() + " : " + _theme.getUserId());

com.liferay.portlet.announcements.service.AnnouncementsEntryLocalServiceUtil ann = new AnnouncementsEntryLocalServiceUtil();

com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil msg = new MBMessageLocalServiceUtil();
MBMessage message = msg.createMBMessage(1001);
message.setBody("My Simple Message");
message.setSubject("TEST Message");
message.setCompanyId(_theme.getCompanyId());
message.setUserId(_theme.getUserId());
message.setUserName(_theme.getUser().getFullName());
message.setCategoryId(1);
msg.updateMBMessage(message);

// AnnouncementsEntry entry = ann.createAnnouncementsEntry(109);
// Calendar cal = Calendar.getInstance();
// entry.setCompanyId(_theme.getCompanyId());
// entry.setUserId(_theme.getUserId());
// entry.setAlert(true);
// entry.setContent("adfads asdfasdf asdfsadf asdfasdf");
// entry.setCreateDate(cal.getTime());
// entry.setDisplayDate(cal.getTime());
// ann.updateAnnouncementsEntry(entry);
} catch (SystemException ex) {
Logger.getLogger(KeyAccess.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Posted on 5/28/10 1:30 PM.
Insert the following in web.xml
<context-param>
<param-name>com.icesoft.faces.hiddenPortletAttributes</param-name>
<param-value>THEME_DISPLAY</param-value>
</context-param>

Then you can access Theme Display by adding the code in your Managed Bean:
avax.portlet.PortletRequest req = (javax.portlet.PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
RenderReques­t renderRequest = (RenderRequest) req.getAttribute("javax.portlet.request");
ThemeDisplay _theme = (ThemeDisplay) req.getAttribute(WebKeys.THEME_DISPLAY);

System.err.println(_theme.getCompany() + " : " + _theme.getCompanyId());
System.out.println(_theme.getUser().getFullName() + " : " + _theme.getUserId());
Posted on 5/28/10 1:51 PM in reply to Hans-Georg Menz.
Not sure why you have to get the RenderRequest from the already obtained PortletRequest (should both the be the same objects), but perhaps this is a quirck with faces I'm not aware of...

Otherwise try this:
----------------------------------------
try{
RenderRequest renderRequest = ...

ThemeDisplay _theme = (ThemeDisplay)renderRequest.getAttribute(
WebKeys.THEME_DISPLAY);

List<ObjectV­aluePair<String, byte[]>> files = Collections.EMPTY_LIST;

ServiceContext serviceContext = ServiceContextFactory.getInstance(
MBMessage.class.getName(), renderRequest);

MBMessageLocalServiceUtil.addMessage(
_theme.getUserId(), _theme.getUser().getFullName(),
_theme.getScopeGroupId(),
MBCategoryConstant­s.DEFAULT_PARENT_CATEGORY_ID, "TEST Message",
"My Simple Message", files, false,
MBThreadConstants.PRIORITY_NOT_GIVEN, true, serviceContext);
}
catch (SystemException ex) {
...
}
----------------------------------------

Furthermore, there is no need to initialize the services as these are singletons that are ready for use. Just invoke their static methods.

Also, use the factory add/delete/update methods from core services unless you are ABSOLUTELY sure what you are doing.
Posted on 5/28/10 2:04 PM in reply to Ruben Gerad Mathew.
Thank you Ray, I really appreciate your immediate assistance. I hope I can document my full development approach soon.
Posted on 5/29/10 12:57 PM in reply to Ray Augé.
Hello,
Can anyone tell me how to get user attributes from a vaadin portlet please ?
Posted on 6/10/10 2:01 AM in reply to Ruben Gerad Mathew.
Very nice article. Do you know how to also get at the "custom fields" values for a particular object? I added a custom field for the Organization type but cannot seem to get the values that are at a particular object. I posted the question in the forum. Not sure if you knew. (http://www.liferay.com/community/forums/-/message_boards/message/5857914?_19_pr­eview=false)
Posted on 9/13/10 12:53 PM.
Mark, I responded to the post directly. emoticon
Posted on 9/13/10 1:06 PM in reply to Mark Ricard.
There should be no difference with Vaadin, provided you can get a handle on the underlying portletRequest/Response objects so that you can interact with the JSR-168/286 API.
Posted on 9/13/10 3:52 PM in reply to Kevin Jebel.
Thanks, but I am having trouble getting your example to work. Since I am completely new to Liferay, I am sure, my problem is an easy one, but googling didn't help.

Here's what I did:

Using the Eclipse Liferay IDE, I created a new Portlet. In the view.jsp, I have this code:

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>

<%@ page import="javax.portlet.PortletRequest"%>
<%@ page import="java.util.Map"%>

<portlet:defineObjects />

<%
Map userInfo = (Map)renderRequest.getAttribute(PortletRequest.USER_INFO);
String givenName = (userInfo != null) ? (String)userInfo.get("user.name.given") : "<empty>";
String loginId = (userInfo != null) ? (String) userInfo.get("user.login.id") : "<empty>";
String userPassword = PortalUtil.getUserPassword(request);
%>

Given Name: <%= givenName %>
Login ID: <%= loginId %>
Password: <%= userPassword %>


After deployment, the page shows "Given Name: null, Login ID: null, Password: null", whether I am currently logged in or not. Is there anything I can do to solve this issue? What have I missed? Thanks!
Posted on 1/4/11 6:44 AM.
Arrgh! Ok, sorry: portlet-app. I see. I forgot to include them. Sorry!!

However, the password still is not shown, because I couldn't add that one to the portlet-app tags. How do I make the password bit available? Thanks!
Posted on 1/4/11 6:55 AM in reply to Philipp Bouillon.
I'm using liferay 6. I would like to get current user attributes in the controller.

my methods looks like this, and i'm not using annotation (instead of renderingRequests?)

@RequestMapping(params = "action=myMethod")
public String myMethod(ModelMap model) throws Exception {

// Here I want to get current name and save something with his/her name to the database (over a service)

model.put("done", isSuccessfull);
return "finished";
}
Posted on 1/25/11 7:45 AM.
how can i do this here, to get some name of current logged in person?
Posted on 1/25/11 7:47 AM in reply to Its Me.
Okay, not very compicated. Described as aboth. But i will give some more explicite example

go to YourController.java

I) Portlet.xml
Do as described above in main post under "Personalizaton via Portlet spec(s)"

II) Controller.java
1) Define
----------------------------------------------------------------
public class YourController.java
[...]
@Inject // I'm not sure if i have to inject this, but i did.
protected PortletRequest portRequest;

protected Map mapReq;

[...]

2) Code request in your method
----------------------------
@RequestMapping(params = "action=yourMethod")
public String pushIt(ModelMap model) throws Exception {
Map userInfo =(Map)portRequest.getAttribute(PortletRequest.USER_INFO);
String givenName = (userInfo != null) ? (String)userInfo.get("user.name.given") : "";
model.put("getQuickMessages", countNewMessages());
return "thanx";
}
Posted on 1/25/11 8:24 AM in reply to Its Me.
Ray: I think I've followed your instructions to the dot. Here's my Portlet.xml --

<?xml version="1.0"?>

<portlet-app
version="2.0"
xmlns="http://java.sun.com/xml/ns/port­let/portlet-app_2_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
x­si:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd"
>


<portlet>
<portlet-na­me>my-email</portlet-name>
<display-name>My Email</display-name>
<portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</p­ortlet-class>
<init-param>
<name>view-jsp</name>
<value>/view.jsp</v­alue>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
­ <mime-type>text/html</mime-type>
</supports>
<portlet-info>
<title>My Email</title>
<short-title>My Email</short-title>
<keywords>My Email</keywords>
</portlet-info>
<security-role-ref>
<role-name>adminis­trator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>­guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>p­ower-user</role-name>
</security-role-ref>
<security-role-ref>
<role-na­me>user</role-name>
</security-role-ref>

</portlet>

<user-attribute>
<description>User Given Name</description>
<name>user.name.given</name>
</user-attribute>
<user-attribute>
<description>User Last Name</description>
<name>user.name.family</name>
</user-attribute>
<user-attribute>
<descripti­on>User eMail</description>
<name>user.home-info.online.email</name>
</user-attribute>­


</portlet-app>



And the code snippet I am using to read the User's email :


<portlet:defineObjects />

<%
Map userInfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
String emailID = "init@notworking.com";
if(userInfo!=null){
emailID = (String) userInfo.get("user.home-info.online.email");
System.out.println("Email ID:"+ emailID);
}


I always get userInfo variable as a null emoticon

Can you guide me ?
Posted on 1/31/11 1:24 AM.
It took me a few minutes to see the problem myself, but once you realize that "request" is not a PortletRequest it's quite obvious. You need to get the USER_INFO from a PortletRequest, in this case it would be from the "RenderRequest" which is automatically added to the page scope by <portlet:defineObjects />.

So change the line to:

Map userInfo = (Map) renderRequest.getAttribute(PortletRequest.USER_INFO);

and it will work.
Posted on 1/31/11 6:49 AM in reply to Abhay Doshi.
Sir, you are awesome ! Thank you emoticon
Posted on 1/31/11 6:59 AM in reply to Ray Augé.
Thank you! I'm glad to help.
Posted on 1/31/11 7:10 AM in reply to Abhay Doshi.
Quick question: How can I access the user's additional email address (under profile) and custom fields ?
Posted on 2/15/11 9:39 AM in reply to Ray Augé.
hello ray... i got dis error while runnig my jsp


org.apache.jasper.JasperException: An exception occurred processing JSP page /user.jsp at line 8

5:
6: <portlet:defineObjects />
7: <%
8: Map userInfo = (Map)renderRequest.getAttribute(PortletRequest.USER_INFO);
9: String emailID = "init@notworking.com";
10: if(userInfo!=null){
11: emailID = (String) userInfo.get("user.home-info.online.email");


Stacktrace:
org.apache.jasper.servle­t.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.ja­sper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.ja­sper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.se­rvlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.ser­vice(HttpServlet.java:717)
com.liferay.portal.kernel.servlet.BaseFilter.processF­ilter(BaseFilter.java:196)
com.liferay.portal.kernel.servlet.BaseFilter.doFilter­(BaseFilter.java:126)
com.liferay.portal.kernel.servlet.PortalClassLoaderFilter.­doFilter(PortalClassLoaderFilter.java:53)
com.liferay.portal.kernel.servlet.Base­Filter.processFilter(BaseFilter.java:196)
com.liferay.portal.servlet.filters.gzi­p.GZipFilter.processFilter(GZipFilter.java:126)
com.liferay.portal.kernel.servle­t.BaseFilter.doFilter(BaseFilter.java:123)
com.liferay.portal.kernel.servlet.Por­talClassLoaderFilter.doFilter(PortalClassLoaderFilter.java:53)
com.liferay.porta­l.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:196)
com.liferay.porta­l.kernel.servlet.BaseFilter.doFilter(BaseFilte
Posted on 2/25/11 7:36 AM in reply to Abhay Doshi.
You aren't accessing the jsp through a portlet, so it won't work. You can't arbitrarily hit a jsp in a portlet and have it work as if through the portal.
Posted on 2/25/11 8:48 AM in reply to prabhakar m.
yes sir im accessing through the portal only..
Posted on 2/25/11 9:42 PM in reply to Ray Augé.
im getting error in the portlet itself.
Posted on 2/25/11 9:44 PM in reply to prabhakar m.
And the code snippet I am using to read the User's email :


<portlet:defineObjects />

<%
Map userInfo = (Map) request.getAttribute(PortletRequest.USER_INFO);
String emailID = "init@notworking.com";
if(userInfo!=null){
emailID = (String) userInfo.get("user.home-info.online.email");
System.out.println("Email ID:"+ emailID);
}%>

<%=emailID%> with this code(with out render ) im getting "init@notworking.com" if i use renderRequest... im getting apache error.. plz guide me.
Posted on 2/25/11 9:58 PM in reply to Ray Augé.
i want to dispaly the current user id who is accessing the portlet in liferay 6.0.5. ..this is my requirement.. i created a simple jsp portlet.. and installd it in to liferay portal.. help me plz
Posted on 2/25/11 10:07 PM in reply to prabhakar m.
mr.ray thanks for such a useful blog.. i hope i ll get solution here...thanks
Posted on 2/25/11 10:12 PM in reply to Ray Augé.
yes mr. ray i got user id and email... itz only show ing in view.jsp... links are not woking in dat page..but i want to get user id in my index page is it possible? because i need to save the email in to mysql database.. is it possible?
Posted on 2/28/11 11:56 PM in reply to Ray Augé.
Hi Ray
thank you for the HOWTO
but i still have null when i use liferay.user.nickName !!!!
Any idea for this!!!
Posted on 4/7/11 7:26 AM in reply to Ray Augé.
How are you using that field? I don't know if that field is even defined! It should be "user.name.nickName"
Posted on 4/7/11 7:28 AM in reply to touka touka.
[...] On top of that if you include <liferay-theme:defineObjects /> it will include below variables to be accessible from respective jsp. ThemeDisplay themeDisplay Company company Account account User user... [...] Read More
Posted on 5/12/11 3:08 AM.
[...] On top of that if you include <liferay-theme:defineObjects /> it will put a whole bunch of context sensitive objects into our JSP page context. Below is the list for the same. ThemeDisplay... [...] Read More
Posted on 5/12/11 3:18 AM.
Super helpful!
Posted on 11/15/11 1:43 PM.
hi all.i have different issue regarding personalization.i login as john and changed my portlet topper color.i want that change only reflect for john.there is any procedure to get that kind of stuff in liferay.
Posted on 3/5/12 3:40 AM.
i need portlet to be personalized based on specific user not roles.please provide some suggesions
Posted on 3/5/12 3:42 AM in reply to krishna kumar.
Who would be setting this value; the user, the site admin?
Posted on 3/5/12 11:38 AM in reply to krishna kumar.
[...] Para obtener el nombre del usuario o cualquier otro dato del mismo desde un portlet de Liferay y en concreto desde el jsp (view.jsp o cualquier otro), podemos hacer lo siguiente. En view.jsp, ponemos... [...] Read More
Posted on 6/21/12 6:23 AM.
[...] Para obtener el nombre del usuario o cualquier otro dato del mismo desde un portlet de Liferay y en concreto desde el jsp (view.jsp o cualquier otro), podemos hacer lo siguiente. En view.jsp, ponemos... [...] Read More
Posted on 6/21/12 6:23 AM.
Thank you for the
Best regards.
Posted on 8/26/12 1:55 PM.
Useful stuff ever..!!
Thanks
-Vishal
Posted on 8/27/12 11:10 PM in reply to Mihail Kirilov.
[...] http://www.java2s.com/Code/Java/JSP/Writeyourowntag.htm http://www.liferay.com/web/raymond.auge/blog/-/blogs/809893 http://www.liferay.com/community/forums/-/message_boards/message/5997940 [...] Read More
Posted on 9/8/12 11:25 AM.
[...] http://www.java2s.com/Code/Java/JSP/Writeyourowntag.htm http://www.liferay.com/web/raymond.auge/blog/-/blogs/809893 http://www.liferay.com/community/forums/-/message_boards/message/5997940 [...] Read More
Posted on 12/26/13 4:36 AM.