Vista combinada Visión Plana Vista de árbol
Discusiones [ Anterior | Siguiente ]
toggle
Abhi Ed
template in creating web content
10 de julio de 2012 7:26
Respuesta

Abhi Ed

Ranking: Regular Member

Mensajes: 118

Fecha de incorporación: 4 de junio de 2012

Mensajes recientes

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
10 de julio de 2012 7:34
Respuesta

Jignesh Vachhani

Ranking: Liferay Master

Mensajes: 785

Fecha de incorporación: 10 de marzo de 2008

Mensajes recientes

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
10 de julio de 2012 7:52
Respuesta

Abhi Ed

Ranking: Regular Member

Mensajes: 118

Fecha de incorporación: 4 de junio de 2012

Mensajes recientes

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
10 de julio de 2012 8:14
Respuesta

Jignesh Vachhani

Ranking: Liferay Master

Mensajes: 785

Fecha de incorporación: 10 de marzo de 2008

Mensajes recientes

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
10 de julio de 2012 23:40
Respuesta

Abhi Ed

Ranking: Regular Member

Mensajes: 118

Fecha de incorporación: 4 de junio de 2012

Mensajes recientes

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
10 de julio de 2012 23:48
Respuesta

Amit Doshi

Ranking: Liferay Master

Mensajes: 549

Fecha de incorporación: 29 de diciembre de 2010

Mensajes recientes

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
10 de julio de 2012 23:51
Respuesta

Bart Simpson

Ranking: Liferay Master

Mensajes: 524

Fecha de incorporación: 29 de agosto de 2011

Mensajes recientes

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
11 de julio de 2012 0:49
Respuesta

Abhi Ed

Ranking: Regular Member

Mensajes: 118

Fecha de incorporación: 4 de junio de 2012

Mensajes recientes

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
11 de julio de 2012 2:48
Respuesta

Abhi Ed

Ranking: Regular Member

Mensajes: 118

Fecha de incorporación: 4 de junio de 2012

Mensajes recientes

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
11 de julio de 2012 3:07
Respuesta

Abhi Ed

Ranking: Regular Member

Mensajes: 118

Fecha de incorporación: 4 de junio de 2012

Mensajes recientes

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
16 de enero de 2013 19:06
Respuesta

Ben Chapman

Ranking: New Member

Mensajes: 23

Fecha de incorporación: 7 de marzo de 2011

Mensajes recientes

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>