Liferay: Add related assets to a blog entry or wiki page programmatically

In Liferay an asset is something like a blog entry, a wiki page, a message board message or a document. These assets could either be tagged (categorized) or connected as related assets. So each asset can be connected with each other asset.

In Liferay 7 it looks for example like this (in Liferay 6.2 the icons might be a little bit different):

Screenshot of a blog entry in Liferay 7.0 m1 with related assets.
Screenshot of a blog entry in Liferay 7.0 m1 with related assets.

In the related assets area you see two links. The first links to a related message board message. The second links to a related wiki page. If you want to create these relations programmatically you could use the following code:

// create related assets between blog entry and wiki page:

long blogAssetId = AssetEntryLocalServiceUtil.getEntry( BlogsEntry.class.getName(), entry.getEntryId() ).getEntryId();
long wikiAssetId = AssetEntryLocalServiceUtil.getEntry( WikiPage.class.getName(), wp.getResourcePrimKey() ).getEntryId();

AssetLinkLocalServiceUtil.addLink(themeDisplay.getUserId(), blogAssetId, wikiAssetId, AssetLinkConstants.TYPE_RELATED, 0);

entry is your blog entry object (from BlogsEntry class) and wp is your wiki page object (from WikiPage class).

If you don’t know how to get your themeDisplay, you can use the following in the processAction method:

public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) 
	throws IOException, PortletException {
		
	themeDisplay = (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
}

If you only have your blog entry’s id, you can easily fetch your full entry:

BlogsEntry entry = BlogsEntryLocalServiceUtil.fetchBlogsEntry(entryId);

To get the asset entry id of a message board message, use this:

AssetEntry assetEntry = AssetEntryLocalServiceUtil.getEntry( MBMessage.class.getName(), mbMessage.getMessageId() );

long mbMAssetId = assetEntry.getEntryId();

Schreibe einen Kommentar