Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Lock and unlock publish page using aem workflow

Avatar

Level 2

Hi All,

I have requirement by default page lock whenever page publish it should be unlocked to the particular author for review once accepted then it will be published page and if it is rejected then it will return back to the author and locked the page.also we can able to scheduled time as well where we can add the time when it will be published.

 

Does anyone worked on such type of requirement?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@bangar20 

workflow.png

 

 

This just a high level solution not exactly to your use case. But it will help to design your solution. 
1. As soon workflow trigger. It will lock page. 
2. You can use any Participant Chooser as per need. There are others which may be used for your need. You can have this before lock as per need.
3. Assigned reviewer can choose next step or you can write custom logic to move to next step as per your need. 

4. Left Branch -  If rejected. Assign this to workflow initiator(You can use ECMA script to get Workflow initiator)

5. Right Branch - If approved,

          -> Reviewer can schedule publish. In this case activate step not required.
          ->You can write custom step, If this has to be done in workflow only(If don't want to use OOTB publish schedule)  

         -> Unlock Page

I hope this will help. Again, This is not the exact solution to your use case. But a high level solution as per how much I understood.

ECMA script function to get workflow initiator

 

function getParticipant() {
    return workItem.getWorkflow().getInitiator();
}

View solution in original post

10 Replies

Avatar

Community Advisor

Hi @bangar20 

Sharing some code snippet!

To check page is published

public static Boolean isPublished(Resource resource) {
   Boolean activated;
   ReplicationStatus status = null;
   activated = false;
   if (resource != null) {
   try {
       Page page = resource.adaptTo(Page.class);
       status = page.adaptTo(ReplicationStatus.class);
   } catch (Exception e) {
   LOG.debug(e.getMessage(), e);
 }
if (status != null) {
  activated = status.isActivated();
  }
}
  return activated;
}

To check Page is locked

public class PageServiceImp implements PageService {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    private String user = "";
    private Session session;
    //Inject a Sling ResourceResolverFactory

    @Reference
    private ResourceResolverFactory resolverFactory;

    public String MovePage()
    {
       String pagePath = "/content/MovePage64/fr";
       String newPagePath ="/content/MovePage64/en" ;
       String templatePath = "/apps/AEMPage/templates/page-home";
       String pageTitle = "AEM home page";
       Page newPage;
PageManager pageManager; ResourceResolver resolver = null; String newPageName = ""; Map<String, Object> param = new HashMap<String, Object>(); param.put(ResourceResolverFactory.SUBSERVICE, "datapersist"); try { //Invoke the adaptTo method to create a Session used to create a QueryManager resolver = resolverFactory.getServiceResourceResolver(param); Resource res = resolver.getResource(pagePath); //Adapts the resource to another type - in this example to a com.day.cq.wcm.api.page Page page = res.adaptTo(Page.class); //Lock this page page.lock(); logger.info("The page is locked!"); // session = resolver.adaptTo(Session.class); return "" } catch (Exception e) { // TODO Auto-generated catch block logger.info("&&&&& BIG ERROR" +e.getMessage()); } return "" ; } }


To check page unlock

//Unlock this page
 page.unlock(); 


Refer above code snippet and customize your requirement in workflow.

Hope that helps!

Regards,
Santosh

Avatar

Employee Advisor

Quick questions here , are you planning to start the publishing process via workflow and then that page will be locked for review ?

How are you planning to select that particular author? 

If you could provide these inputs, then it will help to design entire process.

In the meantime I am sharing page lock and unlock process step -

 

Page lock workflow process step -

/**
 * 
 */
package com.aem.demo.core.workflows;

import java.util.Objects;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;

/**
 * @author debal This workflow step is responsible to lock AEM page.
 */

@Component(property = { Constants.SERVICE_DESCRIPTION + "=This workflow step is responsible to lock AEM page",
		Constants.SERVICE_VENDOR + "=AEM Demo Debal", "process.label" + "=Page lock process" })
public class PageLockWorkflowStep implements WorkflowProcess {

	private final Logger logger = LoggerFactory.getLogger(PageLockWorkflowStep.class);

	@Override
	public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
			throws WorkflowException {

		String payloadPath = workItem.getWorkflowData().getPayload().toString();

		ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);

		Resource resource = resourceResolver.getResource(payloadPath);

		if (Objects.nonNull(resource) && resource.isResourceType("cq:Page")) {

			Page page = resource.adaptTo(Page.class);
			try {
				page.lock();
			} catch (WCMException e) {

				logger.error("Unable to lock the given Page", e.getMessage());
			}

		} else {
			logger.info("**** Resource isn't a page**** {} ");
		}
	}

}

2. 

Page unlock workflow process step -

/**
 * 
 */
package com.aem.demo.core.workflows;

import java.util.Objects;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.WCMException;

/**
 * @author debal This workflow step is responsible to Unlock AEM page
 *
 */
@Component(property = { Constants.SERVICE_DESCRIPTION + "=This workflow step is responsible to Unlock AEM page",
		Constants.SERVICE_VENDOR + "=AEM Demo Debal", "process.label" + "=Page Unlock process" })
public class PageUnLockWorkflowStep implements WorkflowProcess {

	private final Logger logger = LoggerFactory.getLogger(PageUnLockWorkflowStep.class);

	@Override
	public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
			throws WorkflowException {

		String payloadPath = workItem.getWorkflowData().getPayload().toString();

		ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);

		Resource resource = resourceResolver.getResource(payloadPath);

		if (Objects.nonNull(resource) && resource.isResourceType("cq:Page")) {

			Page page = resource.adaptTo(Page.class);
			try {
				if (page.canUnlock()) {
					page.unlock();
				}

			} catch (WCMException e) {

				logger.error("Unable to lock the given Page", e.getMessage());
			}

		} else {
			logger.info("**** Resource isn't a page**** {} ");
		}
	}

}

 

 

 

 

Avatar

Level 2

Hi @DEBAL_DAS ,

I am planning to start publish page using workflow and particular user(author).

 

Avatar

Community Advisor

Hi,

There are OOTB lock and unlock steps. You can use those within workflow model.



Arun Patidar

Avatar

Level 2

Hi @arunpatidar ,

Requirement is that user who locked the page it is unlocked for the same user not all the user.

OOTB step unlock for all the user.

Also need to define the author can select the publish date and time.

 

Avatar

Community Advisor

Hi,

You cannot unlock the page only for a user. If it is unlock then it can be acted by any of the user.

You can though lock the page.

You can use the approver/workflow initiator to lock by and once workflow is done, unlock step will unlock the page.

Select publish date and time can be part of first step where initiator can select that from custom dialog.



Arun Patidar

Avatar

Community Advisor

Please provide some information to understand your scenario properly. 
1. How will you trigger this workflow. Will this trigger on page publish. If yes, then you must disable OOTB publish workflow.

2. How will you select Author/Reviewer. Is it based on content hierarchy or manually. 

3. If it is Rejected, Whom it should go back. ie. the user who initiated publish. 

4. How will you scheduled time to publish. At what stage, you will schedule time. ie. At starting the workflow or assigned Reviewer will schedule time while reviewing it, If it is not rejected.

I can try to design workflow after having this information.  

Avatar

Level 2

Hi @sunil_kumar_ ,

1.Yes while page selection start the workflow manually.

2.if it is rejected then it return back to author who start the workflow.

3.after reviewer accepted unlock the page and he can schedule time for publishing.

 

Avatar

Correct answer by
Community Advisor

@bangar20 

workflow.png

 

 

This just a high level solution not exactly to your use case. But it will help to design your solution. 
1. As soon workflow trigger. It will lock page. 
2. You can use any Participant Chooser as per need. There are others which may be used for your need. You can have this before lock as per need.
3. Assigned reviewer can choose next step or you can write custom logic to move to next step as per your need. 

4. Left Branch -  If rejected. Assign this to workflow initiator(You can use ECMA script to get Workflow initiator)

5. Right Branch - If approved,

          -> Reviewer can schedule publish. In this case activate step not required.
          ->You can write custom step, If this has to be done in workflow only(If don't want to use OOTB publish schedule)  

         -> Unlock Page

I hope this will help. Again, This is not the exact solution to your use case. But a high level solution as per how much I understood.

ECMA script function to get workflow initiator

 

function getParticipant() {
    return workItem.getWorkflow().getInitiator();
}

Avatar

Community Advisor

I think @arunpatidar have given good clue about OOTB on this 

 

I found a couple of links, check if these help -

Regards,

Santosh