Introduction
Workflows helps SharePoint users to collaborate on documents, list items and to manage tasks by implementing business processes on documents and list items.
Creating and assigning tasks to Manager is a very common scenario in workflows. All the user related information is stored in User profile service application (UPA). Although this sounds very straightforward, getting the user profile properties of the logged in user is very tricky in SPD 2013 workflow.
SPD 2010 supported out of box activity to access user profile fields. However, User Profile service option is not present in SharePoint 2013 Designer workflows.
Objective
This article will detail the necessary steps to retrieve user profile properties in a SPD workflow as well as highlight a few common issues you may encounter during development.
Activate site feature - Workflows can use app permissions
- Navigate to Site Settings and click Manage site features under Site Actions.
- Activate feature Workflows can use app permissions.
Granting the App Permissions
Firstly, we will ensure that workflow has sufficient permissions to access the User Profile Service.
- Navigate to Site Settings and click Site app permissions under Users and Permissions.
- Make note of the App ID for your Workflow – portion of the Workflow App Identifier between | and @.
Example
i:0i.t|ms.sp.ext|fc7f8bbb-ac8f-4129-9459-dbdc6f56e2cc@2cf426a0-55c4-461e-81d7-b124b0746498
- With the tenant admin account, navigate to <site-url> /_layouts/15/appinv.aspx
- At the appinv.aspx page, fill in the Application ID and click the search button next to the ID box
- Copy below xml to Permission Request XML box,
- <AppPermissionRequests>
- <AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="Manage" />
- </AppPermissionRequests>
- Click Create
- Click Trust It, when prompted.
Create SharePoint Designer Workflow to Get User Profile Properties Data
For example, we will concentrate on getting the Manager of a logged-in user.
- Open the site in SharePoint designer and create workflow on designed list or document library.
- Set up a workflow variable UserName that stores the target user’s login name
- We will use the REST API to get UserProfile Properties
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='Manager')?@v='i:0%23.f|membership|[email protected]'
It is better to test the above url in any browser to make sure it returns the Manager
- Let’s store the REST API in a variable called RestUrl
- We will encode the url as below,
- Build dictionary to set up the RequestHeaders
Set the values as below to dictionary.
Accept | application/json;odata=verbose |
Content-Type | application/json;odata=verbose |
Authorization | |
Most simple REST calls from a SharePoint Designer 2013 workflow requires headers are Accept and Content-Type. However, you must explicitly add an Authorization header with an empty string value in order to avoid an Unauthorized response. If you are getting a response of Unauthorized, then make sure you have the Authorization header in this dictionary.
- Inside App Step, Add activity - Call Http Web Service to invoke the REST API.
- Use workflow activity Get an Item from a Dictionary to read the Manager
- Enter d/GetUserProfilePropertyFor for the item name
- The workflow variable Manager should get populated with the Manager
Issues and Troubleshoot
Few times, d/GetUserProfilePropertyFor does not return the value for Manager.
To troubleshoot,
Log the HTTP response code to the Workflow History list as a quick way of determining the root cause of the error.
Log the value of ResponseContent
The output will be in the format as below,
To get the Manager value, change the workflow activity Get an Item from a Dictionary as,