Reputation: 51039
I created 2 test portlets, myportlet1 and myportlet2. Both descriptions are similiar and looks like this in portlet.xml
<portlet>
<portlet-name>myportlet1</portlet-name>
<display-name>MyPortlet1</display-name>
<portlet-class>com.inthemoon.tests.MyPortlet1</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/html/myportlet1/view.jsp</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<portlet-info>
<title>MyPortlet1</title>
<short-title>MyPortlet1</short-title>
<keywords></keywords>
</portlet-info>
<portlet-preferences>
<preference>
<name>testPreferenceForMyPortlet1</name>
<value>C:\WINDOWS1</value>
</preference>
</portlet-preferences>
<security-role-ref>
<role-name>administrator</role-name>
</security-role-ref>
<security-role-ref>
<role-name>guest</role-name>
</security-role-ref>
<security-role-ref>
<role-name>power-user</role-name>
</security-role-ref>
<security-role-ref>
<role-name>user</role-name>
</security-role-ref>
</portlet>
and like this in liferay-portlet.xml
<portlet>
<portlet-name>myportlet1</portlet-name>
<icon>/icon.png</icon>
<preferences-company-wide>true</preferences-company-wide>
<instanceable>false</instanceable>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>
/js/main.js
</footer-portlet-javascript>
<css-class-wrapper>myportlet1-portlet</css-class-wrapper>
</portlet>
the difference is with index.
So, as you see, both portlets are not instanceable and set to share their preferences company-wide.
Although, the following code shows that each portlet knows only it's own preferences but not other portlet's ones:
@Override
public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
javax.portlet.PortletPreferences prefs1 = renderRequest.getPreferences();
//com.liferay.portal.model.PortletPreferences prefs2 = PortletPreferencesLocalServiceUtil.
String portletResource = ParamUtil.getString(renderRequest, "portletResource");
if (Validator.isNotNull(portletResource)) {
try {
prefs1 = PortletPreferencesFactoryUtil.getPortletSetup(renderRequest, portletResource);
} catch (SystemException e) {
e.printStackTrace();
}
}
String testPreferenceForMyPortlet1 = prefs1.getValue("testPreferenceForMyPortlet1", "(not set)");
String testPreferenceForMyPortlet2 = prefs1.getValue("testPreferenceForMyPortlet2", "(not set)");
renderRequest.setAttribute("testPreferenceForMyPortlet1", testPreferenceForMyPortlet1);
renderRequest.setAttribute("testPreferenceForMyPortlet2", testPreferenceForMyPortlet2);
renderRequest.setAttribute("prefs1", prefs1);
super.doView(renderRequest, renderResponse);
}
What did I did wrong? Thanks
Upvotes: 0
Views: 924
Reputation: 3865
'Problem' is that preferences-company-wide
does not tells that preferences will be shared with different portlets.
It's usage is for sharing preferences within single portlet but in different communities/organizations/user_pages ... (different group id's).
To get preferences of another portlet you would have to use
PortletPreferencesFactoryUtil.getPortletSetup(portletRequest, "another portlet's id");
Upvotes: 2