Share via


SharePoint 2013 Workflow Development - Handling Dynamic Values and Collections

SharePoint 2013 Workflow provides a new data type called DynamicValue. This new data type, in combination with the other workflow activities, makes our life much easier to work with services in SharePoint 2013 workflows. This data type would be very helpful in handling class objects, data contracts, operation contracts etc., during service calls. Basically it understands JSON. Hence when we make WCF service calls with an HTTP activity, the data/operation contract returned by the service on JSON format will be transformed into an object of type DynamicValue. we will be then use another activity to pull data out of this variables of this data type using an XPATH like notation.

Creating a Dynamic Variable 

As a first step before using the dynamic types, we need create a dynamic variable to hold the dynamic values.

For creating a dynamic value variable, go to variables tab on the workflow and create a new variable.

By default the "Microsoft.Activities.DynamicValue" wont be appearing, in such cases select option "Browse for Types..." from the Variable type drop down.

Selecting option "Browse for Types..." from the Variable type drop down, opens a new dialog to select the type. Typing in first few characters will bring you the types that start with those characters.

Select "Dynamic Value" from Microsoft.Activities group.

Creating a Dynamic Value

When the dynamic variable is created to store the dynamic value, drag drop the "BuildDynamicValue" activity from the tool box to the workflow.

Click on View Properties and start mapping your local variables (RHS) to the service's data (or) operator contract properties (LHS)

In this case UPN (string), Email (string) and DisplayName (String) are mapped to corresponding contract properties.

Make sure you create the local variables with respective data types as per the contract.

In case of complex types, the dynamic value will in turn hold another dynamic value, till the value reaches to simple types. By other terms, its the process of breaking complex types in to simple types.

For Example:

  • User (Complex Type - Dynamic Value)
    • UPN (Simple Type - string)
    • Email Id (Simple Type - string)
    • Display Name (Simple Type - string)
    • Group (Complex Type - Dynamic Value)
      • Name (Simple - string)
      • Id (Simple - string)

Creating a Dynamic Value Collection

In order to create a dynamic value collection, we need to first create a variable.

By default the collection variable wont be available for selection and we need to browse thru the types.

Search for "collection" and select the "System.Collections.ObjectModel.Collection<T>, Upon selection, the dialog box provides an option to select the collection type. You have to select "DynamicValue" here. In case if its not listed, browse for types from the drop down, search and select the "Dynamic Value".

Building a Collection

For Building a collection of any type we can use the BuildCollection<T> Activity.

Upon adding this activity to workflow, it does open a dialog box where we need to select the content type for <T>. Need to select DynamicValue as type here.

The type selection here would be tricky here, as there is no option to change the type once selected. VS2012 property explorer will not provide any option to change the type.

The option is to either delete and recreate the activity with right data type or change the type in XAML file, which might be tricky and leads to unexpected results, when workflow is accidentally messed up. Really needs some hands on experience before you can try that.

Once the activity is dropped and the type is set to "Dynamic Value", we can associate the collection variable defined on the previous step. (UserList)

 

Building a Dynamic Value

Upon creation of all required variables and setting up activities, its time to build a dynamic value.

Include the logic to pull the values for different variables required for the dynamic value. In this case its UPN, Email and DisplayName.

Drag and drop a BuildDynamicValue activity and assign the result as dynamic variable defined. in this case its User.

Click on view properties and start assigning properties.

While assigning properties, make sure the local properties on RHS are of similar type as the ones on the LHS (i.e the Data contract properties)

Important Note: The LHS types would be derived dynamically based on the types of variables assigned on RHS. In case of misconfiguration, the LHS types wont be automatically corrected upon associating another variable of different type. This would throw a type mismatch error during compilation. This needs to be corrected manually on XML, which would be risky, Hence the recommendation here is to delete the assignment line item in subject and create a new one.

Add Dynamic Value to Collection

Upon construction of a Dynamic value the same can be added to a collection.

Drag and drop a AssignToCollection<T> activity and update relevant properties.

This activity must have the Collections property associated with the Dynamic Value Collection variable (UserList), type selected as Dynamic Value and the Item to be added (User).

Calling a WCF Service

The following is the schema of a sample WCF service that provides an Operation (Ensure Users) that can accept EnsureUserRequestReponse (Data Contract - Dynamic Value) as input and responds thru the same contract. The EnsureUserRequestReponse contract in turn has collection of users  (Dynamic Value - Collection of Dynamic Values).

Building a Data Contract - Dynamic Value

With the help of Dynamic Value (User) and a Dynamic value collection (UserList)  built in above steps, we need to construct another Dynamic Value (EnsureUserRequest), to represent the Data Contact.

Making a WCF Service Call

Upon construction of data contract dynamic value, its time to make the WCF call. Drag drop a HTTPSend Activity and configure the following 4 properties.

Method

POST

RequestContent

EnsureUserRequest (Dynamic Value)

URL

WCF Service URL

ResponseContent

EnsureUserResponse (Dynamic Value)

 Upon completion, the activity might look similar to the following.

Retrieving WCF Response (using GetDynamicValueProperties )

The response (EnsureUserRequestResponse) from the WCF call will be stored to the dynamic variable (EnsureUserResponse) and can be retrieved thru GetDynamicValueProperties Activity.

Drag drop the GetDynamicValueProperties activity and configure the source as EnsureUserResponse.

Configure the Properties to be retrieved from the Dynamic value. In this case its Users (LHS - Name of the property on WCF service data contract) and EnsuredUserList (RHS - Local Dynamic variable)

Looping Thru Response Collection (Dynamic Value collection)

Can use a ForEach activity to loop thru the Dynamic Value collection (EnsuredUserList) and GetDynamicValueProperties activity again to read thru each Dynamic Value (Item) and get properties.

In case of nested dynamic values, we need to use GetDynamicValueProperties as well in the loop to retrieve the dynamic value at different levels.

Hope this helps you in playing around the dynamic values, collections and making WCF service calls from SharePoint 2013 workflows.

This is a new data type in WAW. There's a more technical and complicated explanation to this data type but I think it's easier to understand in the context of how it's used. Basically it understands JSON. So when you call a WCF service with an HTTP activity, it returns an object of type DynamicValue. You then use another activity to pull data out of this variables of this data type using an XPATH like notation. I'll show how to use this in the next post. I'm a HUGE fan of this!
Read more at https://www.andrewconnell.com/blog/SP2013Whats-new-with-Workflow#hBsPYppRqZkh4OkL.99

This is a new data type in WAW. There's a more technical and complicated explanation to this data type but I think it's easier to understand in the context of how it's used. Basically it understands JSON. So when you call a WCF service with an HTTP activity, it returns an object of type DynamicValue. You then use another activity to pull data out of this variables of this data type using an XPATH like notation. I'll show how to use this in the next post. I'm a HUGE fan of this!
Read more at https://www.andrewconnell.com/blog/SP2013Whats-new-with-Workflow#hBsPYppRqZkh4OkL.99

This is a new data type in WAW. There's a more technical and complicated explanation to this data type but I think it's easier to understand in the context of how it's used. Basically it understands JSON. So when you call a WCF service with an HTTP activity, it returns an object of type DynamicValue. You then use another activity to pull data out of this variables of this data type using an XPATH like notation. I'll show how to use this in the next post. I'm a HUGE fan of this!
Read more at https://www.andrewconnell.com/blog/SP2013Whats-new-with-Workflow#hBsPYppRqZkh4OkL.99

I mentioned this new data type called DynamicValue. This new data type, in combination with the new HTTP activities, makes it so much easier to work with services in SharePoint 2013 workflows. What Microsoft has given us are a few new activities that you use to acquire, create and parse these data types. Here's a high-level how it works:
Read more at https://www.andrewconnell.com/blog/SP2013-Workflow-Dynamic-Values#I7RxHqjdwxBjOob2.99

I mentioned this new data type called DynamicValue. This new data type, in combination with the new HTTP activities, makes it so much easier to work with services in SharePoint 2013 workflows. What Microsoft has given us are a few new activities that you use to acquire, create and parse these data types. Here's a high-level how it works:
Read more at https://www.andrewconnell.com/blog/SP2013-Workflow-Dynamic-Values#I7RxHqjdwxBjOob2.99

In my last post about workflow in SharePoint 2013, I mentioned this new data type called DynamicValue. This new data type, in combination with the new HTTP activities, makes it so much easier to work with services in SharePoint 2013 workflows. What Microsoft has given us are a few new activities that you use to acquire, create and parse these data types. Here's a high-level how it works:
Read more at https://www.andrewconnell.com/blog/SP2013-Workflow-Dynamic-Values#I7RxHqjdwxBjOob2.99

Comments

  • Anonymous
    April 04, 2015
    I am using HttpSend activity in VS 2013 declarative workflow. The activity will return me multiple items. I want to iterate through each item. Could you please provide me sample code on how to iterate through HttpSend response. I know I need to use Dynamic value, but how to use that.

  • Anonymous
    April 07, 2015
    Im interested in the answer to the same question as Pradeep Thorat.  It is probably somewhere in the article but more info would be appreciated.

  • Anonymous
    September 18, 2015
    Hi Thiru, Thanks for nice article on dynamic collections. In my SPD workflow i am calling rest url and setting response type as json. and i am getting required results and i am able to traverse the results and make use of it. However, in the results, i have a field of type person or group and allows multiple values. And so the result is a collection for this field and i am not able to access the values from that collection. I have to read every item in that collection and log it some where. Could you please help me. If you are not still clear what i am asking, please create a person or group filed with allow multiple values in a list and get that in the result of your webservice call. And try to print that field values. thank you before hand. Raj