Expand my Community achievements bar.

SOLVED

Getting Email id of workflow initiator

Avatar

Level 3

Hi,

I have created a workflow in which the author_group user starts the workflow and in the next step it gets assigned to Publisher group through OOTB participant step process. Now at the end of the workflow i need to send an email to the initiator, but since the workflow session got transferred to publisher group so I am not able to get the session of initiator . Can anyone please tell how to get email id or Session of the initiator as workItem.getWorkflow().getInitiator(); only returns user id and not email id. Please note that i am using AEM 6.3.

Thanks

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi Rohit,

You can check attached sample code for your implementation.

import java.util.Collections;

import org.apache.jackrabbit.api.security.user.Authorizable;

import org.apache.jackrabbit.api.security.user.UserManager;

import org.apache.sling.api.resource.LoginException;

import javax.jcr.Session;

import javax.jcr.Value;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.ResourceResolverFactory;

import org.apache.sling.jcr.resource.JcrResourceConstants;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import com.day.cq.workflow.WorkflowException;

import com.day.cq.workflow.WorkflowSession;

import com.day.cq.workflow.exec.WorkItem;

import com.day.cq.workflow.exec.WorkflowProcess;

import com.day.cq.workflow.metadata.MetaDataMap;

@Component(service=WorkflowProcess.class, property={Constants.SERVICE_DESCRIPTION + "=Sample Workflow",

"process.label" + "=Sample Workflow Process"})

public class SampleWorkflow implements WorkflowProcess{

@Reference

ResourceResolverFactory resourceResolverFactory;

@Override

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap dataMap) throws WorkflowException {

        ResourceResolver resourceResolver = null;

        try {

            // Get the ResourceResolver from workflow session

            resourceResolver = getResourceResolver(workflowSession.getSession());

            UserManager manager = resourceResolver.adaptTo(UserManager.class);

            Authorizable authorizable = manager.getAuthorizable(workItem.getWorkflow().getInitiator());

            Value[] email = authorizable.getProperty("./profile/email");

            System.out.println(email);

        }catch(Exception e){

      

        }

}

    private ResourceResolver getResourceResolver(Session session) throws LoginException {

        return resourceResolverFactory.getResourceResolver(Collections.<String, Object>singletonMap(JcrResourceConstants.AUTHENTICATION_INFO_SESSION,

                session));

    }

}

This code would help!

Regards,Himanshu

View solution in original post

4 Replies

Avatar

Community Advisor

Hi Rohit,

Once you've got the user ID, you can use Authorizable to fetch the properties of user,

Reference code:

UserManager manager = request.getResourceResolver().adaptTo(UserManager.class);

Authorizable authorizable = manager.getAuthorizable(session.getUserID());

Value[] email = authorizable.getProperty("./profile/email");

Hope this helps!

Regards,

Himanshu

Avatar

Level 3

Hi Himanshu,

many thanks for your reply. But since I am new to AEM could you please guide me how to get the request parameter in Workflow class. We have workflow session parameter so is there any way that we can adapt it to Request??

Avatar

Level 5

Hi rohit,

You can Service based authentication to get the access to ResourceResolver. Please refer this article for the same.

Give this system user to read access of your author's profile. Later, you can use this resource resolver to get the email Id of them as described by Himanshu.

Avatar

Correct answer by
Community Advisor

Hi Rohit,

You can check attached sample code for your implementation.

import java.util.Collections;

import org.apache.jackrabbit.api.security.user.Authorizable;

import org.apache.jackrabbit.api.security.user.UserManager;

import org.apache.sling.api.resource.LoginException;

import javax.jcr.Session;

import javax.jcr.Value;

import org.apache.sling.api.resource.ResourceResolver;

import org.apache.sling.api.resource.ResourceResolverFactory;

import org.apache.sling.jcr.resource.JcrResourceConstants;

import org.osgi.framework.Constants;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.component.annotations.Reference;

import com.day.cq.workflow.WorkflowException;

import com.day.cq.workflow.WorkflowSession;

import com.day.cq.workflow.exec.WorkItem;

import com.day.cq.workflow.exec.WorkflowProcess;

import com.day.cq.workflow.metadata.MetaDataMap;

@Component(service=WorkflowProcess.class, property={Constants.SERVICE_DESCRIPTION + "=Sample Workflow",

"process.label" + "=Sample Workflow Process"})

public class SampleWorkflow implements WorkflowProcess{

@Reference

ResourceResolverFactory resourceResolverFactory;

@Override

public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap dataMap) throws WorkflowException {

        ResourceResolver resourceResolver = null;

        try {

            // Get the ResourceResolver from workflow session

            resourceResolver = getResourceResolver(workflowSession.getSession());

            UserManager manager = resourceResolver.adaptTo(UserManager.class);

            Authorizable authorizable = manager.getAuthorizable(workItem.getWorkflow().getInitiator());

            Value[] email = authorizable.getProperty("./profile/email");

            System.out.println(email);

        }catch(Exception e){

      

        }

}

    private ResourceResolver getResourceResolver(Session session) throws LoginException {

        return resourceResolverFactory.getResourceResolver(Collections.<String, Object>singletonMap(JcrResourceConstants.AUTHENTICATION_INFO_SESSION,

                session));

    }

}

This code would help!

Regards,Himanshu