Get User Profile Properties Data in SharePoint Designer 2013 Workflow

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

  1. Navigate to Site Settings and click Manage site features under Site Actions.
  2. Activate feature Workflows can use app permissions.

    SharePoint

Granting the App Permissions

Firstly, we will ensure that workflow has sufficient permissions to access the User Profile Service.

  1. Navigate to Site Settings and click Site app permissions under Users and Permissions.
     
  2. 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
     
  3. With the tenant admin account, navigate to <site-url> /_layouts/15/appinv.aspx
  4. At the appinv.aspx page, fill in the Application ID and click the search button next to the ID box
  5. Copy below xml to Permission Request XML box,
    <AppPermissionRequests>  
       <AppPermissionRequest Scope="http://sharepoint/social/tenant" Right="Manage" />  
    </AppPermissionRequests>
  6. Click Create
    SharePoint
  7. Click Trust It, when prompted.

    SharePoint

Create SharePoint Designer Workflow to Get User Profile Properties Data

For example, we will concentrate on getting the Manager of  a logged-in user.

  1. Open the site in SharePoint designer and create workflow on designed list or document library.
     
  2. Set up a workflow variable UserName that stores the target user’s login name

    SharePoint
     
  3. 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

    SharePoint
  4. Let’s store the REST API in a variable called RestUrl

    SharePoint
    SharePoint
     
  5. We will encode the url as below,

    SharePoint
     
  6. Build dictionary to set up the RequestHeaders

    SharePoint

    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.

  7. Inside App Step, Add activity - Call Http Web Service to invoke the REST API.
    SharePoint

  8. Use workflow activity Get an Item from a Dictionary to read the Manager
     
  9. Enter d/GetUserProfilePropertyFor for the item name

    SharePoint
     
  10. 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

SharePoint

The output will be in the format as below,
SharePoint
To get the Manager value, change the workflow activity Get an Item from a Dictionary as,

SharePoint