Click here to Skip to main content
Click here to Skip to main content
Technical Blog

String processing workflow actions for SharePoint 2013 and SharePoint Online

, 25 Mar 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article I will demonstrate a set of custom string processing the workflow actions for SharePoint 2013 and SharePoint Online (Office 365). Out-of-the-box workflow actions have limited functionality of string processing, so I will show how to extend it by using the custom string processing wor

In this article I will demonstrate a set of custom string processing workflow actions for SharePoint 2013 and SharePoint Online (Office 365). Out-of-the-box workflow actions have limited functionality of string processing, so I will show how to extend it by using the custom string processing workflow actions.

I divided this article into three parts:

  1. Description of workflow actions. It is a quick reference for all developed string processing workflow actions.
  2. Short guide to the using of the string processing workflow actions. This guide describes how to use the split string workflow action mostly, because it’s not a real problem to understand the using of other workflow actions.
  3. Description of the implementation. In this part I will show you how to create string processing workflow actions without any code behind. It allows you to use workflow actions for SharePoint Online workflows as well as for SharePoint 2013 On Premise.

If you are not interested in the implementation, just read the first part and start using workflow actions in your projects. You can find ready-to-use installation packages, instructions and source code of string actions in the codeplex project.

Note: Workflow actions support SharePoint 2013 workflows (Workflow Foundation 4.5). All workflow actions are compatible with SharePoint Online and SharePoint 2013, except for Foundation, because SharePoint 2013 Foundation doesn't support SharePoint 2013 workflows (Workflow Foundation 4.5).

Description of workflow actions

Split string

This workflow action splits a string into a collection of substrings using the specified separator. You can use a character as well as a string as a separator. This workflow action returns a dictionary as its result. It is possible to enumerate through the dictionary using out-of-the-box loop, and I will show how to do that later.

Format date

This workflow action formats a date using a format string. It returns the formatted date as a string. I used DateTime.ToString(string formatString) method as the basis for this workflow action. You can use format string templates from the MSDN documentation:

String contains

This workflow action checks if a string contains a substring. The workflow action has an additional property called IgnoreCase, which allows the comparer to ignore the case of the string and of the substring. By default IgnoreCase property is set to Yes. The workflow action returns a Boolean value as a result.

String starts with

This workflow action checks if a string starts with a substring. This workflow behaves the same as String contains workflow action.

String ends with

This workflow action checks if a string ends with a substring. This workflow behaves the same as String contains workflow action.

String length

This workflow action calculates the length of a string and returns it as an Integer value.

String to lower

This workflow action transforms a string to a lower case string. It returns the transformed string as the result.

String to upper

This workflow action transforms a string to an upper case string. It returns the transformed string as the result.

How to use workflow actions

As you can see from the examples of workflow actions above, most of them are self-documented. You just need to fill the input properties in and to save the result into a variable.

However, Split string workflow action needs an additional explanation. This action returns the collection of substrings as a dictionary. It is not obvious, but dictionary in the SharePoint workflow can contain a collection of elements as well as key-value pairs. In our case, it contains a collection of strings in the following format:

[
  "test1",
  "test2",
  "test3",
  "test4"
]   

I will show how to enumerate through this collection using out-of-the-box loop. Have a look at this workflow:

Firstly, I count items in the dictionary, then I initialize the indexing variable. We need to know the count of the items in the dictionary to configure the loop. In the end of the loop I increment the current index by using the calculate workflow actions. Finally, I build the key string by using the index to get the item from the dictionary. The key looks like “(1)”, "(2)", etc. I use the index variable in the Get item from dictionary workflow action to build the key. That is all, now you can enumerate through the dictionary like you would through an array.

Implementation of string processing workflow actions

We used to work with server side code when working with workflow actions for SharePoint 2010 workflows. SharePoint 2013 workflows have a completely new architecture, so we have to use different approaches to develop the workflow actions. SharePoint 2013 workflow actions support the server- side code, but SharePoint Online does not allow any workflow actions with the server- side. If we want to develop universal workflow action, there are two options:

  1. To call web services from workflow actions. We can call existing SharePoint web services or create our own web service with custom logic.
  2. To create workflow actions as a combination of other workflow actions and user C# expressions to extend the functionality.

Now, I will describe how to use the second method to create string processing workflow actions. I chose this method because I did not want to create and maintain a new web service. Moreover, SharePoint does not provide any out-of-the-box web services, which could help me with my task. I just needed to execute basic operations with strings and that was enough to use the second approach.

Declarative workflow actions for SharePoint 2013 allow to use C# expressions inside existing workflow actions, which is a great feature. You can call C# methods and properties directly in the workflow action. Thus, you can create workflow actions by combining standard Assign workflow actions. It is really helpful for creating of relatively simple workflow actions. For example, this is how String to lower workflow action looks in Visual Studio:


As you can see I used String.ToLower() C# method inside the Assign action to implement String to lower workflow action. Yes, it was just a single workflow action with one C# expression.

//C# expression
SourceString.ToLower()


Now let us see what is inside. You can open StringToLower.xaml and find definition of the Assign workflow action:

<Assign sap2010:WorkflowViewState.IdRef="Assign_1">
  <Assign.To>
    <OutArgument x:TypeArguments="x:String">
      <mca:CSharpReference x:TypeArguments="x:String">
        ResultString
      </mca:CSharpReference>
    </OutArgument>
  </Assign.To>
  <Assign.Value>
    <InArgument x:TypeArguments="x:String">
      <mca:CSharpValue x:TypeArguments="x:String">
        SourceString.ToLower()
      </mca:CSharpValue>
    </InArgument>
  </Assign.Value>
</Assign>


As you can see it uses CSharpValue to define C# expression declaratively.

You could be wondering whether it is safe to use C# expressions? Is it possible to execute C# expressions in the Sandboxed Code Service? Microsoft have announced that server side code in sandboxed solutions is deprecated, and it is not recommended to use it for development. You are right, but C# expressions will not be executed inside Sandboxed Code Service. You can ensure it yourself. Just deploy the string actions package and stop Microsoft SharePoint Foundation Sandboxed Code Service in the SharePoint Central Administration. Then try to use any workflow action. All of them will work fine. Moreover, workflow actions will work on SharePoint Online as well as in SharePoint 2013.

Unfortunately, standard C# expressions are very limited. You can’t use most of .NET classes, methods, properties and enums, but some of the methods are available. Here is the list of methods and properties available for String class:

  • Contains
  • EndsWith
  • Equals
  • IndexOf
  • Length
  • Replace
  • StartsWith
  • Substring
  • ToLower
  • ToLowerInvariant
  • ToString
  • ToUpper
  • Trim


The String class goes hand in hand with the StringComparison enumeration. Unfortunately, it is not supported, as well as the most of enumerations. To implement ignore case for string processing workflow actions I had to use ToLower method. This is how String contains workflow action looks in Visual Studio:

//C# expression for contains
SourceString.Contains(Substring)

//C# expression for ignore case contains
SourceString.ToLower().Contains(Substring.ToLower())


I used the same logic to implement String starts with and String ends with workflow actions.

As you can see from the list above, String class also does not support Split method. To implement Split string workflow action I had to parse string in a loop using out-of-the-box workflow actions and C# expressions. There are a lot of workflow actions and the whole picture of the Split string workflow action would not fit into this page. You can download the source code and analyze it by yourself. It works just fine - as well as the other workflow actions.

Fortunately, C# expressions support most of methods and properties of the DateTime class. I used ToString(string format) method to implement Format date workflow action:

//C# expression
SourceDate.ToString(FormatString)


You can find list of supported methods and properties for DateTime class below. I had some plans to implement workflow action for the calculation of work days , but, as I mentioned earlier, most of the enumerations are not supported, including the DayOfWeek enumeration.

The list of supported methods and properties:

  • Add
  • AddDays
  • AddHours
  • AddMilliseconds
  • AddMinutes
  • AddMonths
  • AddSeconds
  • AddYear
  • CompareTo
  • Equals
  • Hour
  • Kind
  • Minute
  • Month
  • Second
  • Subtract
  • ToString
  • Year

Conclusion

In this article I showed how to extend the set of out-of-the-box string processing workflow actions with the help of the custom workflow actions. You can find ready-to-use installation packages, instructions and source code of string actions in the codeplex project.

C# expressions are very limited, but they allow to quickly implement simple workflow actions. It is possible to execute basic calculations and to call a limited set of C# properties and methods. You can analyze the supported methods and properties and use them to implement your own workflow actions.

Above, I mentioned the two approaches for the implementing of the universal workflow actions. I used the second approach, but you can combine both of them and implement quite complex workflow actions for SharePoint 2013 and SharePoint Online.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anton Khritonenkov
Technical Lead
Russian Federation Russian Federation
SharePoint expert                                                            
Co-founder of Plumsail: http://plumsail.com
My blog: http://plumsail.blogspot.com
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 25 Mar 2014
Article Copyright 2014 by Anton Khritonenkov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid