Enable Liferay staging programmatically - Surekha Technologies
Enable Liferay staging programmatically
As we know Liferay provides staging functionality with which we can assemble, test and review new versions of a website before it goes to production. With a staging functionality, clone of a site gets created in which one is a staging site and other is a live site. User can only edit content of staging site and not of a live site. After doing all required changes and reviewing content, user can publish content to live site with “Publish to Live” button that Liferay provides out of the box.
Liferay provides two type of staging:
1. local live staging.
2. remote live staging.
In the local live staging, staging environment and local environment are hosted on same server. In the remote staging, staging environment and local environment are hosted on different server instance may be on different physical machines.
You can find more information about staging feature in liferay and how to enable it in control panel from here.
In this blog, I have covered that how we can enable staging in liferay programmatically.
Liferay by default staged some of the portlet like web content, document and media etc. To enable staging programmatically, below is the Liferay API.
StagingLocalServiceUtil.enableLocalStaging(long userId, Group liveGroup, boolean branchingPublic, boolean branchingPrivate, ServiceContext serviceContext)
Explanation For the Parameters :
- userId : It is current userId.
- liveGroup : It is group(site) object for that you need to enable staging feature.
- branchingPublic : set this to true if you want to enable page versioning for the public pages.
- branchingPrivate: set this to true if you want to enable page versioning for the private pages.
After adding this method into your code, you are able to enable staging for your site.
If you want to add more portlet in staged portlet category other than Liferay default staged portlets, then you have to set it into serviceContext’s attribute.
For example, if you want stage blog portlet then you need to set serviceContext’s attribute as shown below:
ServiceContext serviceContext = ServiceContextFactory.getInstance(Group.class.getName(), actionRequest); serviceContext.setAttribute(StagingConstants.STAGED_PORTLET+"161", true); // add blog portlet in staged portlet list
Explanation :
In the above code, I have set attribute name as StagingConstants.STAGED_PORTLET+"161” to true in which StagingConstants.STAGED_PORTLET is liferay constant prefix and 161 is portlet id (here blog portlet id).
So you can add other portlet by the same way setting StagingConstants.STAGED_PORTLET+”portletId” attribute to true in serviceContext.
You can check that portlet is staged successfully by going into site’s control panel where you can see check mark against blog portlet.
One more thing, if you need to enable main variation for the staging site then you could do it by following code :
LayoutSetBranch layoutSetBranch = LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(groupId, false, LayoutSetBranchConstants.MASTER_BRANCH_NAME); LayoutSetBranchLocalServiceUtil.updateLayoutSetBranch(layoutSetBranch);
Complete code for Enabling Liferay Staging :
ServiceContext serviceContext = ServiceContextFactory.getInstance(Group.class.getName(), actionRequest); serviceContext.setAttribute(StagingConstants.STAGED_PORTLET+"161", true); StagingLocalServiceUtil.enableLocalStaging(long userId, Group liveGroup, boolean branchingPublic, boolean branchingPrivate, ServiceContext serviceContext); LayoutSetBranch layoutSetBranch = LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(groupId, false, LayoutSetBranchConstants.MASTER_BRANCH_NAME); LayoutSetBranchLocalServiceUtil.updateLayoutSetBranch(layoutSetBranch);
For Your Business Requirements
2 - 4 October 2024
Hall: 10, Booth: #B8 Brussels, Belgium