Message Boards

Liferay 7.2 - Liferay-portlet runtime defaultPreferences not replacing

Fabio Carvalho, modified 4 Years ago.

Liferay 7.2 - Liferay-portlet runtime defaultPreferences not replacing

Junior Member Posts: 81 Join Date: 6/25/19 Recent Posts
Hi,

I have a portlet with a embed navigation menu portlet. I have a value for the defaultPreferences that can change sometimes. The problem is that when it changes, the portlet always uses the previous defaultPreferences.

My java code is the following:
@Override
public void doView(RenderRequest request, RenderResponse response) throws IOException, PortletException {
    ThemeDisplay theme = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);        

&nbsp;   List<sitenavigationmenu> menus = SiteNavigationMenuLocalServiceUtil.getSiteNavigationMenus(theme.getLayout().getGroupId());
&nbsp;&nbsp; &nbsp;SiteNavigationMenu menu = findMenu(menus);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;if (menu != null) {
        PortletPreferences preferences = PortletPreferencesLocalServiceUtil.getDefaultPreferences(theme.getCompanyId(), "com_liferay_site_navigation_menu_web_portlet_SiteNavigationMenuPortlet");
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;preferences.setValue("siteNavigationMenuId", Long.toString(menu.getSiteNavigationMenuId()));
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String xml = PortletPreferencesFactoryUtil.toXML(preferences);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;request.setAttribute("preferences", xml);
&nbsp;&nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;
&nbsp;&nbsp; &nbsp;super.doView(request, response);
}&nbsp; &nbsp; 

private SiteNavigationMenu findMenu(List<sitenavigationmenu> menus) {
    for (SiteNavigationMenu menu : menus) {
&nbsp; &nbsp;     if (menu.getName().equals("Main Menu")) {
&nbsp; &nbsp; &nbsp; &nbsp;     return menu;
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; }&nbsp;
 &nbsp; &nbsp; &nbsp; 
&nbsp;   return null;
}</sitenavigationmenu></sitenavigationmenu>

And in my view.jsp:
&lt;% String preferences = (String) request.getAttribute("preferences"); %&gt;

<liferay-portlet:runtime   defaultPreferences="<%= preferences %>" portletName="com_liferay_site_navigation_menu_web_portlet_SiteNavigationMenuPortlet_INSTANCE_some_id" />


So to explain the code: on my doView method I find the menu that is called "Main Menu". If the Menu is not null, I get the preferences of the default Navigation Menu Portlet and set the value for the "siteNavigationMenuId" the menuId. Finally I convert to a XML String and pass it as a request attribute. On my view.jsp I get this attribute and set it as my "defaultPreferences".

This works the first time. But If I delete my Main Menu and create another with the same name, the second one will have a different ID. The doView method will pass an attribute with a differente "siteNavigationMenuId", but the "defaultPreferences" on the JSP is still trying to get the previous menuId.

My guess is that Liferay is saving my "com_liferay_site_navigation_menu_web_portlet_SiteNavigationMenuPortlet_INSTANCE_some_id" on the database and is using it instead of updating it.
YOGESH CHANDRA, modified 4 Years ago.

RE: Liferay 7.2 - Liferay-portlet runtime defaultPreferences not replacing

New Member Posts: 6 Join Date: 4/1/20 Recent Posts
Hi, Were you able to fix this, I need to set preferences for Web content display. 
Below is my sample code.PortletPreferences preferences = PortletPreferencesLocalServiceUtil.getDefaultPreferences(themeDisplay.getCompanyId(), "com_liferay_journal_content_web_portlet_JournalContentPortlet");
        preferences.setValue("articleId", ja.getArticleId());
        preferences.setValue("groupId",String.valueOf( ja.getGroupId()));
        preferences.setValue("ddmTemplateKey",ja.getTemplateId());
        
        pref= PortletPreferencesFactoryUtil.toXML(preferences);
/*         JournalArticleDisplay articleDisplay = JournalArticleLocalServiceUtil.getArticleDisplay(themeDisplay.getCompanyGroupId(),
        ja.getArticleId(), ja.getTemplateId(),
        "", themeDisplay.getLanguageId(),
        themeDisplay);
        content = articleDisplay.getContent(); */
  %>
  <div>
 <liferay-portlet:runtime portletName="com_liferay_journal_content_web_portlet_JournalContentPortlet" instanceId="isfgjsnsjkjk" defaultPreferences="<%=pref %>"/>
</div>
YOGESH CHANDRA, modified 3 Years ago.

RE: Liferay 7.2 - Liferay-portlet runtime defaultPreferences not replacing

New Member Posts: 6 Join Date: 4/1/20 Recent Posts
Hi,I am using web content display and setting preferences dynamically . You can reference  below code its working for me.      
             javax.portlet.PortletPreferences preferences = PortletPreferencesFactoryUtil
                    .getLayoutPortletSetup(themeDisplay.getLayout(), "com_liferay_journal_content_web_portlet_JournalContentPortlet_INSTANCE_"+instanceId);
            preferences.setValue("ddmTemplateKey","");
            preferences.setValue("assetEntryId", new Long( ja.getResourcePrimKey()).toString());
            preferences.setValue("userToolAssetAddonEntryKeys", "");
            preferences.setValue("enableViewCountIncrement", "false");
            preferences.setValue("groupId", new Long(ja.getGroupId()).toString());
            preferences.setValue("articleId",new Long(ja.getArticleId()).toString());
            preferences.setValue("contentMetadataAssetAddonEntryKeys","");
            PortletPreferencesLocalServiceUtil.updatePreferences(themeDisplay.getScopeGroupId(),
            3,0, "com_liferay_journal_content_web_portlet_JournalContentPortlet_INSTANCE_"+instanceId, preferences); Check portletpreferences  table as well .