Getting Email id of workflow initiator | Community
Skip to main content
Level 2
September 2, 2017
Solved

Getting Email id of workflow initiator

  • September 2, 2017
  • 4 replies
  • 5844 views

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Himanshu_Singhal

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

4 replies

Himanshu_Singhal
Community Advisor
Community Advisor
September 2, 2017

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

Level 2
September 2, 2017

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??

Level 4
September 2, 2017

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.

Himanshu_Singhal
Community Advisor
Himanshu_SinghalCommunity AdvisorAccepted solution
Community Advisor
September 3, 2017

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