Create compact and powerful conditions in your workflows

Create compact and powerful conditions in your workflows

Rate This
  • Comments 11

clip_image001

Hi, Andy Lewis here again. I'm writing to share some tips on how to use SharePoint Designer to create workflows that leverage compound conditions and regular expressions. If you know your way around a SharePoint workflow, you know that you can use a workflow to examine the value of one or more fields in a list item, and then take actions based on a condition set by you. For example, you can have a workflow follow different paths depending on whether a text field contains a certain word.

One challenge you may face in building your application is being able to build (and maintain) a workflow that tests a field against a large number of valid values. If you have a really large number of values to match against, creating and managing the workflow can be a chore.

Or perhaps your needs are even more complex and you need to leverage the power of a regular expression. For example, you want to moderate message board content on your site to comply with various business rules or guidelines. One rule you have is that you want to exclude messages that contain links to sites other than your company's site. You want to allow links to http://www.contoso.com/products, but not http://www.wingtiptoys.com/catalog.

In this article I will demonstrate how to use a workflow to match list data against a compound condition and against a regular expression. I presume that you have designed a workflow before. If you are new to workflows, I recommend that you first consult Introduction to workflows before reading this article.

Match list data against a single compound condition

Suppose you want to check the Title of a list item to see if it contains one of several string values. For example, you want to see if one value from a long list of vehicle types appears in the Title field of a list item. In the Workflow Designer, you could express the condition this way:

image1

But there is an easier way to express this kind of logic. You can load all the possible field values into the condition by using either || (for an "or" condition) or && (for an "and" condition). Here is how we can express the above conditions as a single condition:

image2

Be careful when negating

One pitfall to avoid is using a negative condition in combination with an or condition. For example, suppose you wanted to negate the condition in the above example. You might try to express the condition this way:

image3

This is probably not going to give you the result you want. The condition will always be true unless the title contains every one of these values! The kind of result you probably want is for the workflow to take an action if the Title does not contain at least one of the values in your list. You can express that intent by adding a new branch (an "else" condition):

image4

Limitations

These operators function correctly only with the top two conditions on the menu shown when you click the Conditions button:

  • Compare <list name> field
  • Compare any data source

Also, using this approach, we are not able to create conditions that do things like:

  • Express logical groupings of conditions such as If Title contains (car || automobile || truck) && (airplane || spaceship)
  • Ignore capitalization differences for certain operators, such as begins with or ends with.
  • Deal with white space (including possibly unpredictable numbers of blank spaces) within values; for example: "dump truck"
  • Evaluate complex expressions

But despair not; to solve these kinds of problems and others, there is another very powerful option supported by SharePoint workflows; the matches regular expression operator!

Match list data against a regular expression

Suppose you work at Contoso and you want to make sure that no messages in your Announcement list contain links to sites other than your company's. If you (or a good savvy friend who owes you a favor) know how to write a regular expression, then creating a workflow to watch and alert you to messages that break this rule is easy.

By the way, regular expressions are also known as regex to those cool people who know how to write them, so henceforth I will use this very hip lingo. Not to say that I'm a regex expert or anything, but I am starting to learn some pretty nifty tricks you can do with them. Best of all, I can now impress my friends. Instead of saying "Is that string a valid email address?", I can now say, "Does that string match ("^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$?" (Special thanks to whoever wrote MSDN Example: Confirming Valid E-Mail Format.)

Important: In a SharePoint workflow, the evaluation of whether a value matches your regex is done by the regex engine in the .NET system. So a good reference to use when coding a regex for a SharePoint workflow is this one: .NET Framework General Reference: Regular Expression Language Elements

Design the regex

So here is the regex I came up with that is designed to find a hyperlink to a site other than those with the contoso.com host name:

https?://\S*(?<!\.contoso)\.com

Did you notice the slick negative look-behind in there ( (?<!\.contoso) )?  That part says to look before the .com part of the URL and if it finds anything other than .contoso , then the link must be to a site other than contoso.com. Well maybe it is slick to some people, but not to those people who are not regex-gurus, such as whoever came up with that email example above. Anyway, the point is that coming up with the regex itself is in most cases the hard part. The next part is very simple, when you use SharePoint Designer to build the workflow that matches your regex against a value in a list item.

Create the workflow and set triggers

In SharePoint Designer, use the Workflow Designer to create a new workflow to watch the Announcements list for new or changed items:

image6

Initialize the regex variable

We need to use the same regex more than once so that we can compare it against a few different Text fields. To reduce the potential for a bug in our workflow when changing the regex, we will put it in a workflow variable.

image7

Of course instead of encoding the regex in the workflow itself, you could also initialize the variable using a workflow lookup from a SharePoint list. This alternative would give you the flexibility of being able to modify the logic of this workflow without having to actually use SharePoint Designer to open and modify the workflow itself.

Evaluate the message

The next and last step is simply to test whether any of the Text fields match the regex. 

image8

Now you have a workflow that screens any messages created in the Announcements list to make sure there are no links to sites not hosted on contoso.com. You could also instead use the Set Content Approval Status action to make the workflow either reject or approve the item. But keep in mind that the workflow runs at the permission level of the user who creates or changes the item, so this approval action will only work if the user who triggers the workflow has permission to approve items in that list.

Closing

Here are some next steps you might want to take:

  • If you are inspired and want to know more about how you can use SharePoint Designer to create workflows, see the workflow topics on Office Online.
  • If you find that none of the SharePoint Designer conditions meet a particular business need, one recourse is to create a custom condition. To get started, see the workflow topics on MSDN; in particular, Condition Element (WorkflowActions) describes the construct you will need to create.  From there you can use the table of contents to find other helpful information. Note that to go this route, you will need the ability and permissions to create and deploy code on your SharePoint server.
  • If you are an IT Pro and need to plan for using workflows in your organization, see Workflows roadmap.

That's all for now. Thanks for the time you spent reading this article; I hope you found it helpful.  Please feel free to share the gnarliest regex you can think of that would be useful in a SharePoint application. See you next time!

  • PingBack from http://microsoft.wawblog.info/?p=24820

  • Is the Contains condition Case Sensitive? I am having a difficulties getting a condition to evaluate consistently and I'm wondering if this may be the cause.

    Thanks

    Dean

  • Hi dlgross,

    Yes, CONTAINS is case sensitive.  If you want to ignore case while doing a CONTAINS operation, you should use MATCHES REGULAR EXPRESSION instead. FYI, I have updated this article a bit since original publication after learning about some constraints around using compound conditions.

    Andy

  • Hello

    Can you suggest that how can I do conditional formatting in email messaging using SPD workflow like remove time from datetime field, if then else condition etc etc.

    Ani

  • Hi,

    not exactly related to this, but... I'm using SPD to create a workflow on a W2K3+MOSS English install, with spanish lang pack. Everything runs fine, but some parts of SPD and XSLT processing have problems with date or decimal number conversions.

    for example, on a workflow, if I create a number variable with 60 as default value, when I display it, 60.00 appears. If I use it to add minutes to a date, 6000 minutes are added. If I erase value and enter 60 works fine.

    and, if I use a dataformwebpart to display a list, I have simmilar problems with numerical data display, related to . and , conversions between US and Spanish locale.

    Any ideas?

    thanks in advance!

  • Is there a way using Designer to access data in another list?

  • Tal y como os adelantaba en el último post , aquí tenéis la enésima entrega...bueno más bien la 18ª sobre

  • ashishkanoongo: Did you manage to find an answer to your question? I'm having the same issue where the date displays with the time in an email and I can't see how to format it.

    Jeni

  • So this is cool - using regex in conditions

    Is there a way to _construct_ a string using a regex to replace/substitue text in a field or variable?

  • Hi zootist,

    No, SPD cannot be used to create a workflow that uses regex to modify a string.

    Andy

  • I did not see this condition available in my spd? should I update my moss 2007? I have sp3.

Page 1 of 1 (11 items)
Leave a Comment
  • Please add 4 and 2 and type the answer here:
  • Post