Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
Abhi Ed
template in creating web content
July 10, 2012 7:26 AM
Answer

Abhi Ed

Rank: Regular Member

Posts: 118

Join Date: June 4, 2012

Recent Posts

I am creating a web content in liferay. I have added a template for my structure.In template,i have chosen velocity as my script language.
I want to access logged in user's name in my vm script.
But when i use $user.getFirstName() as declared in init.vm it doesn't work, not even with $user_first_name as used in portal_normal.vm.
Can anyone help me?
Jignesh Vachhani
RE: template in creating web content
July 10, 2012 7:34 AM
Answer

Jignesh Vachhani

Rank: Liferay Master

Posts: 785

Join Date: March 10, 2008

Recent Posts

you can use below default velocity variables in web content :
http://www.liferaysolution.com/2011/10/default-velocity-variables-for-web.html .

But to get logged-in user details , you may have to call user service in template and need to set in separate variable using service locator.
And then you can use all the methods to get login user details.
Abhi Ed
RE: template in creating web content
July 10, 2012 7:52 AM
Answer

Abhi Ed

Rank: Regular Member

Posts: 118

Join Date: June 4, 2012

Recent Posts

Jignesh Vachhani:
you can use below default velocity variables in web content :
http://www.liferaysolution.com/2011/10/default-velocity-variables-for-web.html .

But to get logged-in user details , you may have to call user service in template and need to set in separate variable using service locator.
And then you can use all the methods to get login user details.

Thanks for prompt reply.
But I don't know how to call user service in template.
I followed this link
http://www.liferay.com/community/wiki/-/wiki/Main/Access+Liferay+Services+in+Velocity#section-Access+Liferay+Services+in+Velocity-The+findService()+Method
but here there is no description for calling user service .
Jignesh Vachhani
RE: template in creating web content
July 10, 2012 8:14 AM
Answer

Jignesh Vachhani

Rank: Liferay Master

Posts: 785

Join Date: March 10, 2008

Recent Posts

you can use below code in your web content template :

#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService"))

Now print $userLocalService and check what and all information you are getting.
Abhi Ed
RE: template in creating web content
July 10, 2012 11:40 PM
Answer

Abhi Ed

Rank: Regular Member

Posts: 118

Join Date: June 4, 2012

Recent Posts

Jignesh Vachhani:
you can use below code in your web content template :

#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService"))

Now print $userLocalService and check what and all information you are getting.



Hello,
This $userLocalService is not returning anything.I guess we need to use different service.Any idea?
Amit Doshi
RE: template in creating web content
July 10, 2012 11:48 PM
Answer

Amit Doshi

Rank: Liferay Master

Posts: 549

Join Date: December 29, 2010

Recent Posts

Hi Abhi,

Please check below link. It is given with example.

http://liferaytrends.blogspot.in/2012/05/access-custom-portlet-service-in.html

Hope it helps.

Thanks & Regards,
Amit Doshi
Bart Simpson
RE: template in creating web content
July 10, 2012 11:51 PM
Answer

Bart Simpson

Rank: Liferay Master

Posts: 524

Join Date: August 29, 2011

Recent Posts

It's most probably because you have to allow it via portal-ext.properties.
This can be done by the property
1 journal.template.velocity.restricted.variables=
leaving it blank will allow service locator to get the service
Abhi Ed
RE: template in creating web content
July 11, 2012 12:49 AM
Answer

Abhi Ed

Rank: Regular Member

Posts: 118

Join Date: June 4, 2012

Recent Posts

Paul .:
It's most probably because you have to allow it via portal-ext.properties.
This can be done by the property
1 journal.template.velocity.restricted.variables=
leaving it blank will allow service locator to get the service



Thanks!! I missed that.But the catch is, its still not working. My variable in template is still not getting set.I have set below mentioned variables.:
#set ($user = $userLocalService.getUser($request.get("theme-display").get("user-id")))
#set($userLocalService = $serviceLocator.findExceptionSafeService("com.liferay.portal.service.UserLocalService
Abhi Ed
RE: template in creating web content
July 11, 2012 2:48 AM
Answer

Abhi Ed

Rank: Regular Member

Posts: 118

Join Date: June 4, 2012

Recent Posts

Hello,
I can access $userLocalService when i set it like below:

#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
Ican also access, $userCount by the setting : #set($userCount=$userLocalService.getUsersCount())

But , #set ($user = $userLocalService.getUserById($request.get("theme-display").get("user-id"))) here, $user is not accessible.

I have to get users first name.Any help???
Abhi Ed
RE: template in creating web content
July 11, 2012 3:07 AM
Answer

Abhi Ed

Rank: Regular Member

Posts: 118

Join Date: June 4, 2012

Recent Posts

Finally, i got it.

For me the following lines worked :
#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))

#set ($userId = $getterUtil.getLong($request.remote-user))
#set($user = $userLocalService.getUserById($userId))
#set($userFirstName=$user.getFirstName())

Hope it helps,if someone searches the same.
Ben Chapman
RE: template in creating web content
January 16, 2013 7:06 PM
Answer

Ben Chapman

Rank: New Member

Posts: 23

Join Date: March 7, 2011

Recent Posts

Thanks for all of this. This really helped me get this sorted out.

For others who might be searching and wanting to access expando content, here's an example of how to do that:

 1
 2###
 3## Don't forget to set journal.template.velocity.restricted.variables=
 4## in portal-ext.properties; in other words, set it to nothing to unset the restriction.
 5###
 6
 7## See http://www.liferay.com/community/forums/-/message_boards/message/14789299
 8## this is where this comes from
 9
10#set ($userLocalService= $serviceLocator.findService("com.liferay.portal.service.UserLocalService"))
11#set ($userId = $getterUtil.getLong($request.remote-user))
12#set($user = $userLocalService.getUserById($userId))
13
14#set ($userFirstName=$user.getFirstName())
15## This is a drop down select, so must be accessed this way
16#set ($class_year = $user.getExpandoBridge().getAttribute("gradYear").get(0))
17#set ($ugrad = $user.getExpandoBridge().getAttribute("UndergradSchool"))
18#set ($userCount=$userLocalService.getUsersCount())
19
20<h2>Getting variables from user, system, and expando</h2>
21<p>Year: $class_year</p>
22<p>School: $ugrad</>
23<p>First Name: $userFirstName</p>
24<p>User count: $userCount </p>