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.
- 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.
EnsureUserRequest (Dynamic Value)
WCF Service URL
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.