Lock Page with initiator or author user from workflow | Community
Skip to main content
Level 2
May 31, 2022 7:05 PM UTC
Solved

Lock Page with initiator or author user from workflow

Is there any option where we can lock the page in workflow from workflow process step with initiator or author user.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SantoshSai
SantoshSaiAccepted solution
New Participant
May 31, 2022 8:03 PM UTC

Hi @sai2teja 

Passing same code from @debal_das

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**** {} ");
		}
	}

}

 

Similar question here: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/lock-and-unlock-publish-page-using-aem-workflow/td-p/453767


Regards,
Santosh

Santosh Sai

3 replies

sunil_kumar_
Level 5
Jun 1, 2022 4:21 AM UTC

Hi @sai2teja ,
There is OOTB process to lock payload. You can't lock for specific user. You you want payload/page to be looked for specific user, You have to implement custom functionality. 

OOTB process - Lock Payload Process

Translate
sai2tejaAuthor
Level 2
Jun 1, 2022 4:23 PM UTC

Hey Sunil,

 

I tried in the below mentioned 3 ways:

 

1 - By impersonating with author user I tried to lock the page with author it didn't worked.

 

2 - By getting author user session, I tried to lock the page it didn't worked as well.

 

3 - I tried updating the lockowner property value to author, but no luck.

Translate
SantoshSai
SantoshSaiAccepted solution
New Participant
May 31, 2022 8:03 PM UTC

Hi @sai2teja 

Passing same code from @debal_das

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**** {} ");
		}
	}

}

 

Similar question here: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/lock-and-unlock-publish-page-using-aem-workflow/td-p/453767


Regards,
Santosh

Translate
Santosh Sai