Wednesday, November 1, 2017

SharePoint Online Designer Workflow 2013 : Accessing "People Picker" properties

When we are accessing a out list (A list which is not associated with a Workflow ) which has a people picker and if you need to access it's data properties such as UserName (Name in SP Online terms) or Email or Department. Though it's pretty much straight forward you might mislead by trying to looking to that in REST JSON , or d result angel. it's bit tricky. Anyway before start it's advisable to go through (SharePoint Online Designer Workflow 2013 : Calling REST API & Looping through list items - d results) following article which explains how to get results using REST calls and do the required iteration.

Ok once you finished that article, here are the modification that you must do.

Modification.

a people picker column been added to the same list (UserDetail) called UserID



1) REST call URL
in your REST call url insted of this "/_api/web/lists/getbytitle('UserDetail')/items" which is given commonly access to all items . modified it to "/_api/web/lists/getbytitle('UserDetail')/Items?$select=*,UserID/EMail&$expand=UserID/Id" here you are taking EMail property out separately and  expand the people picker column based on Id, while havving all other columns available "select=*," . it looks like this. yeah looks bit messy.


Let's add this URL to the Workflow

Then let's goto the loop and Add UserID/EMail to the get Dictionery action item. what it dose , it extract the Email from the user object in the People Picker and added to the WF variable.



Now when we run the WF, here is the outcome.



ok That's It ... 
HAPPY CODING

SharePoint Online Designer Workflow 2013 : Calling REST API & Looping through list items - d results

Why ?


One of my colleague was working on a Designer workflow which had been attached to the SharePoint Online portal. based on the requirement, that it is required to access a different list ( not the list which, workflow been associated with ) and extract some data. there were some issues when we were trying to extract information from People picker columns ( After you go through this : go the following Article SharePoint Online Designer Workflow 2013 : Accessing "People Picker" properties ). After we sorted out that ( though it's a simple thing, it took like a day to figured it out ), Got to know that many people struggle in similar kind of situation. This post will explain all the necessary steps  that is required to access a outer list through a Designed workflow.

How ?

With SharePoint 2013 , workflow fabrication been rapidly changed. introducing new feature and mechanism so developers could tackle many issues with new SharePoint 2013 WF manager, which they used to find some workarounds by beating around the bush in traditional SharePoint 2010 WF structure.  One of them is ability to call HTTP web services by supporting of REST API, which enhanced capability to querying list and libraries and returning data objects in JSON ( simply mean d results )


Steps.

This is what we are going to do ....
1) Create a Simple list called "WF Main List" - this is the list that we are going to attach our WF


2) Create a simple list called UserDetail which hold user id and Full Name


3)Create a WF which can called the UserDetail list and get all items then looping though them and add user's Full name to WF history log

Let's begin ....

1- Open SharePoint 2013 designer and add your SharePoint online site, then goto the workflow section and chose list workflow then select the list which you need this WF associate with.  in our case "WF Main List"




Give a name to the workflow like "REST loop testing" and select SharePoint 2013 Workflow (which is default )

This is the first screen your are going to get. Rename the stage with a meaningful name.  (Eg : "Retrieve User Detail")





Let's add a "Set Workflow Variable" action which creates a new String variable called url



Then add REST service called URL Eg "<your site>/_api/web/lists/getbytitle('UserDetail')/items"
Then we need to add a Dictionary variable called "requestHeaders" through "Build a Dictionary action".





Then we should customize the action with following parameters.  

Build the Dictionary by adding both String type entries  "Accept" and "Content-Type". and make sure that value been added as "application/json;odata=verbose". reason behind this that REST API understand this and set response content as JSON






Set out out variable as "requestHeaders"

It's time to call a Web Service action and set its properties. The request will be the url variable



Response content will assigned to a new variable called responseContent (Dictionary type).

Same as responseHeaders and responseCode (String Type)


Then go to the Properties and set Request header as follows
Now let's add separate stage to process retrieved data.



Let's add a "Get an Item from a Dictionary" action and set the item as d/results, and the source as reponseContent, then set output to a new variable of type Dictionary called list.


Then count items from the list variable by adding a "Count Items in Dictionary" action and store the item count in a new Integer variable called count. this will give us logic to define boundaries for the loop to iterate




Then let's define an index variable and set it's initial value as 0 , so we are able to iterate item by using the index variable. 




It's time to add a loop to iterate items.  to do that let's add a loop type called "Loop n Times"
By default looping set to number 1 means one time looping. we need this mechanism to loop for number of item count times.

In the loop we can get value d/results([%Variable:index%]) using a "Get an Item from a Dictionary" action from responseContent and store it in a new Dictionary variable called item. but here is the catch. do not type this in the string builder. first you need to add the Variable index by using workflow variable then do the modification. otherwise you will get some errors when trying to process.

Then we can access list columns by using Get an Item from a Dictionary action make sure that you type the column name as it is with case sensitivity 

Then we can log the item to history


You can concat some string with the variable such as "User Full Name :"

Then we need to make the index increment by 1 till end of looping . in-order to do that we can use "Do Calculation" action

Then we can end the workflow.

Let's Save the workflow and publish it.

Let's do the test by adding an item to "WF main List" and start the workflow. 

Once WF completed we can check the log history











That's it .........  Results are there ... Next post I'm gonna explain some modification to this WF in order to extract information from People picker columns

ok That's It ... 
HAPPY CODING