Posted on September 19th, 2014
In this post, you will learn how to get workflow initiator information in SharePoint hosted app using Visual Studio 2013. In the most of the workflows we work, Initiator information is very important because we need to send an email to Initiator based on workflow status, etc.
We are sharing on workflows in SharePoint 2013 and Office 365 using Visual Studio 2013. These apply to both on-premises and online. All these blog posts here based on SharePoint hosted apps.This post is applicable for both SharePoint 2013 online and SharePoint 2013 on-premises.
Let us check what are the basic workflow activities we use to get the workflow initiator information.
Below are the list of workflow activities you will learn while going through this post.
Let's start creating an app to test our workflow.
We will create a SharePoint hosted app in our example, we will use the same steps in future blog posts related to custom workflows.
Open visual studio 2013, click on new project select Apps under the office/SharePoint from which select project type as Apps for SharePoint 2013.
Enter your project name, here it is InitiatorInfo, then click OK. It will open the wizard to create the project.
Select the site where you want to deploy the app. "https://velegandla.sharepoint.com/sites/developersite".
Hosting option should be : SharePoint-hosted.
Click Finish. It will create the project.
Right-click on the project add a List called Products, in which we will use standard Custom List Definition. If you haven't done creating a list within the app, please check this post here.
Once you have done with the List, let's create the workflow.
We will use list workflow that will be running on Products list.(Please see below screens).
Right click on the project, add [ New Item] Select workflow and click on [Add]
In the below select type of the workflow, in our case this is List workflow.
Select the list on which you want to run the workflow, Here it is Products and creating new "WorkflowToHistory" and "WorkflowTaskList" for storing workflow history and task info.
Click Next, and choose the starting options accordingly. In this, I am setting to start the workflow automatically when an item added.
Now you have added the workflow successfully to the project. Let's add the activities to the workflow.
Add Lookupworkflowcontextproperty from the tool box to the sequence. This activity helps to get workflow
In this example, we need only Initiator.
In the below figure, we are setting Property Name to Initiator and setting result to a variable called Initiator of type string. For variable check the "Workflow variables" section below. From the Initiator, we will get the user name. Our requirement is to get user email, so we will use Initiator variable in the other workflow activities to get the email address.
Below is the list of workflow variables we have used in this example.
LookupSPPrincipalId gives user Id based on userName. Let's pass the Initiator variable here get Output Result as InitiatorId.
Once you have user Id, you can get user properties based on that Id. Here we will use LookupSPUser activity to get the email property. Here the InitiatorProperties variable is of type "Dynamicvalue".
After completing the properties of LookupSPUser, click on properties that will automatically add "GetDynamicValueProperties" activity to the workflow.
In this activity, the source is the output of LookupSPUser, which is InitiatorProperties variable.
Now click on Dictionary ellipses from the above screen that will open the screen below. From the path choose Email and in the Assign To, you can create your variable or click on "Populate Variables" to populate variable automatically for you.
Let's add the "WriteToHistory" activity to log the "Initiatoremail" variable.
Once you complete all the steps, the workflow looks below. I have updated the "Sequence" to Initiator Info, so that whoever sees this workflow they will understand the functionality.
Right-click on the project and click on deploy. Once we deploy it, add an item in the Products list, and it should start the workflow automatically. Now go to workflow status page to see the history info we have written for Initiator email. Please see below screen.
You can find the SharePoint hosted app project here https://github.com/devendrasv/InitiatorInfo
In this post, you have learned how to get workflow initiator information in SharePoint hosted app using Visual Studio 2013.
Devendra Velegandla is a four-time recipient of Microsoft's Most Valuable Professional (MVP) award (2014-2017) for Office Servers and Services MVP and SharePoint Server. He received the MCC award for his contributions in Microsoft MSDN/TechNet forums. He loves sharing articles, videos, and tutorial on SharePoint and Office 365.