Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Setting PortletPreferences programmatically
I have a page with a WebContentDisplay in it and I have a JournalArticle to associate with it.
i would like to associate the JournalArticle to the portlet programmatically.
So I get the portlet preferences of the portlet but I don't know how to set the new values, the article-id for example.
Here's the code I'm using:
//getting the portlet preferences of the portlets of my layout
List<com.liferay.portal.model.PortletPreferences> preferences = PortletPreferencesLocalServiceUtil
.getPortletPreferencesByPlid(layoutDocumenti.getPlid());
//iterating over PortletPreferences
if (preferences != null) {
for (PortletPreferences preference : preferences) {
//checking the id of the portlet i want to set
if (preference.getPortletId().equals("56_INSTANCE_Jn0b")) {
//getting the preference
String xml = preference.toXmlString();
//AND NOW HOW TO SET NEW VALUES ????
}
}
}
Thanks for any idea!!
Alfredo
i would like to associate the JournalArticle to the portlet programmatically.
So I get the portlet preferences of the portlet but I don't know how to set the new values, the article-id for example.
Here's the code I'm using:
//getting the portlet preferences of the portlets of my layout
List<com.liferay.portal.model.PortletPreferences> preferences = PortletPreferencesLocalServiceUtil
.getPortletPreferencesByPlid(layoutDocumenti.getPlid());
//iterating over PortletPreferences
if (preferences != null) {
for (PortletPreferences preference : preferences) {
//checking the id of the portlet i want to set
if (preference.getPortletId().equals("56_INSTANCE_Jn0b")) {
//getting the preference
String xml = preference.toXmlString();
//AND NOW HOW TO SET NEW VALUES ????
}
}
}
Thanks for any idea!!
Alfredo
In web content vm template you can try this code to set preferences
#set($layoutLocalService = $serviceLocator.findService("com.liferay.portal.service.LayoutLocalService"))
#set($layout = $layoutLocalService.getLayout($getterUtil.getLong($request.theme-display.plid)))
#set ($locPortletId = "56_INSTANCE_Arg7pSXTGtht")
#set ($portletPreferencesFactoryUtil = $portal.getClass().forName("com.liferay.portlet.PortletPreferencesFactoryUtil"))
#set ($portletSetup = $portletPreferencesFactoryUtil.getLayoutPortletSetup($layout, $locPortletId))
#set ($temp = $portletSetup.setValue("articleId", "11039"))
#set ($temp = $portletSetup.setValue("groupId", $groupId.toString()))
#set ($temp = $portletSetup.store())
Thanks , at least I found the solution!!
The code I used inside my portlet is similar. Here's:
.
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
.
String portletId = "56_INSTANCE_Jn0b";
javax.portlet.PortletPreferences preferences = PortletPreferencesFactoryUtil
.getLayoutPortletSetup(mylayout, portletId);
preferences.setValue("group-id", new Long(groupId).toString());
preferences.setValue("article-id", myarticle.getArticleId());
// update the portlet preferences
PortletPreferencesLocalServiceUtil.updatePreferences(ownerId,
ownerType, mylayout.getPlid(), portletId, preferences);
.
.
In this way I'm able to associate my article (myarticle) to the web content display of my page (mylayout).
Alfredo
The code I used inside my portlet is similar. Here's:
.
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
.
String portletId = "56_INSTANCE_Jn0b";
javax.portlet.PortletPreferences preferences = PortletPreferencesFactoryUtil
.getLayoutPortletSetup(mylayout, portletId);
preferences.setValue("group-id", new Long(groupId).toString());
preferences.setValue("article-id", myarticle.getArticleId());
// update the portlet preferences
PortletPreferencesLocalServiceUtil.updatePreferences(ownerId,
ownerType, mylayout.getPlid(), portletId, preferences);
.
.
In this way I'm able to associate my article (myarticle) to the web content display of my page (mylayout).
Alfredo
I am using Liferay 6.1EE
And its not working in my case.
Attaching you the code .
And its not working in my case.
Attaching you the code .
javax.portlet.PortletPreferences preferences = PortletPreferencesFactoryUtil
.getLayoutPortletSetup(layout, journalPortletId);
//PortletPreferences prefs = PortletPreferencesFactoryUtil.getLayoutPortletSetup(layout, journalPortletId);
long ownerId = PortletKeys.PREFS_OWNER_ID_DEFAULT;
int ownerType = PortletKeys.PREFS_OWNER_TYPE_LAYOUT;
preferences.setValue("article-id", articleId);
preferences.setValue("group-id", String.valueOf(groupId));
preferences.store();
PortletPreferencesLocalServiceUtil
.updatePreferences(ownerId, ownerType,
layout.getPlid(), journalPortletId,
preferences);
System.out.println("layout.getPlid() = "+layout.getPlid());
System.out.println("---------------------");
if (preferences != null) {
System.out.println(preferences.getMap());
//System.out.println("groupId = "+preferences.getValue("group-id", ""));
}
System.out.println("---------------------");
If you simply want to store your form elements value in preferences then here is trick in LR 6.1
Check this http://itsliferay.blogspot.in/2013/01/save-portlet-preferences-in-liferay-61.html.
Check this http://itsliferay.blogspot.in/2013/01/save-portlet-preferences-in-liferay-61.html.
Thanks for the reply Jitendra...
Actually in Liferay 6.1 they have created two article-id and group -id in PortletPreference object.
articleId and groupId .. After setting the values in these two fields its working fine.
Actually in Liferay 6.1 they have created two article-id and group -id in PortletPreference object.
articleId and groupId .. After setting the values in these two fields its working fine.
Community
Company
Feedback