How to find out on which page your Web Content is displayed

Think about the following problem:

You created a WebContent and within one of your portlets you need to find out where this WebContent is displayed (using Journal Article Display portlets). Sadly, there is no convenience method for this, so we have to biuld our own. We will do this using dynamic queries.

First we are going to get all portlet preferences that have been saved by portlet "56" (which is the Journal Article Display Portlet) and contain the ID of the Web Content we are looking for. This is exactly what is written into the database if you are selecting a WebContent in the WebContent Display portlet. I haven´t found another way to get the info.

Once we have the portlet preferences, we use their "plid" property to get the layouts (= Liferay pages) the portlet is deployed on. And that´s it. You can for example use the friendly URL to navigate to that page.

//create query to retrieve Layouts

DynamicQuery layoutQuery = DynamicQueryFactoryUtil.forClass(Layout.class, PortalClassLoaderUtil.getClassLoader());

//create query to retrieve portlet preferences

DynamicQuery preferencesQuery = DynamicQueryFactoryUtil.forClass(PortletPreferences.class, PortalClassLoaderUtil.getClassLoader());

//tell preferences to look for instances of portlet "56" with the journalArticle in its preferences

preferencesQuery.add(RestrictionsFactoryUtil.like("portletid", "56 INSTANCE%"));
preferencesQuery.add(RestrictionsFactoryUtil.like("preferences", "%<name>article-id</name><value>"+journalArticleId+"</value>%"));

//just return the layouts that have the plid set for the ones we retrieve in the preferences query


//get the data

List dynamicQuery = PasswordPolicyLocalServiceUtil.dynamicQuery(layoutQuery);

If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.