Nick Grattan’s SharePoint Blog

About Microsoft SharePoint and some .NET

While loops in SharePoint Designer Workflows

with 28 comments

When implementing processes, we’re often faced with creating workflows that require something like “while not approved, ask document to be edited and re-approved”. This is difficult to do with SharePoint Designer workflows as it requires looping.

For a while now I’ve been using our Custom Workflow Activity Pack to create loops in SharePoint designer. This simulates a state workflow and is described here: Stateful Workflow Paper. This approach works, but is cumbersome and involves a number of moving parts. A “While Workflow” custom action has now been added that greatly simplifies doing this. This custom action takes four arguments:

  • A field in the document library or list with which the workflow is associated.
  • A condition to test (e.g. “equal to”, “greater than”).
  • A value to test against (either a constant or a lookup to another value in the list, a workflow variable or value from another list)
  • The workflow to call to create the loop. This workflow will be repeatedly called while the expression in the first three arguments returns false.

In this example, a document library has two fields “Verified” (Yes/No) and “Verify Comments” (Single line text). Two workflows need to be created:

  • A workflow (e.g. named “Verify Document”) that contains the task that is to be repeated
  • Another (e.g. named “While Not Verified”) workflow that repeatedly calls the first workflow until the task is completed successfully.

The first workflow, “Verify Document” in this example, simply creates a task requesting the user to verify the document. After the task is completed the “Verified” field is updated in the document library:

whileloop1.PNG

The second workflow uses the “While Workflow” custom action to create the loop. This will repeatedly call the “Verify Document” workflow until the document is verified.

whileloop2.PNG

You can download the latest version of the Custom Activity Pack here. A SharePoint solution file (WSP) is now provided to automate installation. If you already have a previous version installed, follow the instructions in the documentation to uninstall the previous version first.

Download Custom Activity Pack.

Update: See here how while loops are now directly supported in SharePoint 2013: http://nickgrattan.wordpress.com/2012/07/22/while-loops-in-sharepoint-designer-2013-workflows/

Update: 10-Nov-2013. Source code and WSP now available in GitHub: https://github.com/NickGrattan/NGCCustomActivities.git

About these ads

Written by Nick Grattan

December 14, 2010 at 7:09 am

28 Responses

Subscribe to comments with RSS.

  1. Hi Nick,

    Thanks for the great work. I want to know whether Custom Activity Pack can be used with Share point 2010. Please advise. Thanks in advance.

    Mahr Mohyuddin

    February 20, 2011 at 8:42 am

    • Mahy, Yes, we’ve tested with SharePoint 2010 so you should find the same functions are available as with SharePoint 2007. Regards, Nick.

      Nick Grattan

      February 20, 2011 at 8:02 pm

      • Dear Nick,

        While installing it as mentioned in the documentation (but for SP 2010) I get the following error.
        “Object reference not set to instance to an object
        The solution installation failed.” Could you please help

        Mahr Mohyuddin

        February 21, 2011 at 1:08 pm

  2. Does this have to be installed into the GAC? Can it be run from a bin folder and scoped to a particular site collection?

    A T

    May 24, 2011 at 8:10 pm

    • Hi Alan, yes you can install this into the bin folder. However, you’ll need to do this manually and there will probably be issues to do with the trust levels configured in the web.config for the web application. By default the default trust level is set to: . With this setting, the activities will probably fail through a security issue. You can either create a new trust level (a long and difficult task) or set the trust level to “full” (which is not recommended as it allows all assemblies in the bin folder to run with full trust). Is there a reason why you don’t want the assembly in the GAC? Regards, Nick.

      Nick Grattan

      May 25, 2011 at 12:25 pm

      • Thanks for the quick response Nick. Much appreciated. Here’s our company policy:

        “We only support WebParts that will run in your Bin folder and are deployed using a SharePoint Solution file (.wsp). If the web part requires elevated permission you will need to create a custom policyFile that identifies the specific permission set. The Solution, Feature or Web Part must be scoped at the web application. When deployed your Solution, Feature or Web Part must NOT appear as feature on any other SharePoint Web Application other than the one it is scoped for.”

        Would love to use these activities but am hamstrung by this policy.

        A T

        May 25, 2011 at 1:04 pm

  3. The tools seems promising but the “while” action does not seems to work as i always get

    Error starting workflow with ‘StartWorkflow’: Workflow ‘Travel Approval Post Travel’ generated an exception:Workflow ‘Travel Approval Post Travel’ does not exist or could not be loaded.

    Exception of type ‘System.Workflow.ComponentModel.WorkflowTerminatedException’ was thrown.

    I tried both the name of workflow, and unique name of workflow, but neither work. FYI, both workflow are in the same list (same content type), and it is on SP2010 standard

    Roger Leong

    June 1, 2011 at 12:32 am

    • Hi Roger, thanks for your comment. Does your workflow “Travel Approval Post Travel” have an initiation form? To test this, start the workflow manually against the item and see if a initiation form is presented that prompts users for values. If an initiation form is present, the “While” action will fail to start the workflow as it cannot display the initiation form to the user. Regards, Nick.

      Nick Grattan

      June 1, 2011 at 5:27 am

      • Nope there is no initiation field. Anyway, I’d figure that you could be using WorkflowAssociations.GetAssociationByName retrieve the associated workflow, hence I try to create a simple workflow action by to debug the WorkflowAssociations collection of my list, and it return length = 0? Since the workflow is attached to a content type, I tried web.ContentType["mytype"] it also return length = 0? There must be something missing as workflows are definitely added to list’s content type through SharePoint list setting.

        Roger Leong

        June 1, 2011 at 6:06 am

      • The are two reusable workflows attached to content type of a SharePoint list. Would the “While” workflow action work as it seems to query against the WorkflowAssociations of current list?

        Roger Leong

        June 1, 2011 at 6:15 am

      • It seems that the while workflow activity would only work with workflow attached to list, rather than workflow attached to content type within a list.

        I have created my own custom workflow action that does the similar thing by

        SPWorkflowAssociation workflowAssoc = list.ContentTypes[ContentType].WorkflowAssociations.GetAssociationByName(“MyWorkflow”, System.Globalization.CultureInfo.CurrentCulture);

        site.WorkflowManager.StartWorkflow(listItem, workflowAssoc, workflowAssoc.AssociationData);

        Roger Leong

        June 3, 2011 at 12:00 am

  4. Hi Nick,
    Your custom activities are fantastic and fill such a basic gap in the options provided by Microsoft!
    I’m having an issue with the Start Workflow that seems to create one instance of the Workflow but execute the workflow steps four times. So I get four lots of log statements and four lots of tasks etc. Any idea’s on what’s causing this or how I can get around it?
    Cheers

    Karin

    July 10, 2011 at 8:43 am

  5. Hi Nick,
    Your extension pack sounds really interesting, especially the While loop. I was wondering whether this could be deployed as a sandboxed solution to a SharePoint Online site collection. From the installation instructions, it doesn’t sound like it, but I’m not an expert so I thought I’d ask to verify. Thanks!

    Mathias

    October 25, 2011 at 3:46 am

    • Hi Mathias, Thanks for your comment. I’m afraid that it’s not installable on SharePoint Online/Office 365. It requires a GAC install through a farm solution and this is not possible. Sorry. Regards, Nick.

      Nick Grattan

      October 25, 2011 at 5:53 am

  6. [...] at last! Our Action Pack (see here) has been providing a work around for a number of years, but a platform supported solution is most [...]

  7. hi Nick,
    I was able to deploy this but when I tried it in SPD, nothing is showing on the Actions after I click the While Workflow in Workflow Actions window. Is there something else I can do? Do I need to update our MOSS with patches?

    Thank you so much!!

    • Hi Denisse.

      This normally implies that updates to the web.config file have not completed successfully. The documentation suggests how to check this. This can be the case where you have multiple web applications.

      Regards, Nick.

      Nick Grattan

      May 25, 2012 at 7:37 am

  8. Hi Nick,

    Thank you for the assistance. Is it possible that the installation of GAC was successful but the web.config was not updated? I was told that the installation was successful but the status is stuck to “deploying”

    Thanks again!

  9. [...] Joy of joys!  Of course, enabling while loops in SharePoint Designer 2010 is much more difficult – you can find one solution here:  http://nickgrattan.wordpress.com/2010/12/14/while-loops-in-sharepoint-designer-workflows/ [...]

  10. Hi Nick

    This While Workflow action is a life saver, however I’m experiencing delays between loops of 10 mintues.

    Andy ideas?

    Thanks
    Andy

    iamandyogden

    July 30, 2012 at 10:55 am

    • Hi Andy, The nature of the beast I’m afraid. The activity needs to test whether to continue the loop or not, and this is done when the SharePoint Workflow Timer Job runs – the default for this is to run every 5 minutes. You can change the SharePoint Workflow Timer Job to run every 1 minute but you should only do this when testing or on lightly loaded systems. Regards, Nick.

      Nick Grattan

      July 30, 2012 at 11:26 am

  11. Is it possibly to use this to simulate a “For Each” loop? Or can you only attach the “while” task to a single item?

    Carly

    February 5, 2013 at 7:40 pm

  12. Hi Nick!
    I installed the wsp in SharePoint 2010, but when I create a new workflow in ‘Actions’ don’t appears the custon actions. I check the GAC, the folders and the web.config and all it’s ok.

    Any idea? Thanks a lot!.

    Daniel

    May 27, 2013 at 4:22 pm

    • I was having hanging deployments. GAC, Feature, Workflow.ACTIONS, were getting deployed only on the server from where I launch installation. When I backed out of everything (Assembly wouldn’t delete manually, had to use command line to delete), then retried from Application Server instead of particular WFE I got the deployment to go through and actions to show up. The actions, however, are just new entries in the SPD 2010 Actions menu that are there for show. All they did was make my menu list longer. When I click on any of them they do nothing. Which Custom Action are looking for? There are other freely available ones for text handling and other functions like ilovesharepoint on Codeplex. I needed the NGC one for the While loop.

      Peter

      June 27, 2013 at 4:52 pm

  13. Source code and WSP now available in GitHub. See update to post above. Nick.

    Nick Grattan

    November 10, 2013 at 10:47 am

    • Hi, we have been using NGC activities on our SharePoint 2010 applications and now after migrating to SharePoint 2013 we cannot get the activities to work. they show up in Designer 2013 but it doesn’t add the activity if I click on it. I had deployed the wsp using compatibility All. Is this a know issue?

      Manoj

      February 10, 2014 at 2:39 pm

  14. Hi Nick, I have een using your Activity pack for over a year now and all the actions are great!! I just ran across a problem that I haven’t been able to figure out. I have a large workflow that sends out an email using the “Start Workflow” action throughout the main workflow. At the end of this workflow I am using the same action for another email workflow and I keep getting the following error: Error starting workflow with ‘StartWorkflow’: Workflow ‘EmailProcess3′ generated an exception: Exception of type ‘System.Workflow.ComponentModel.WorkflowTerminatedException’ was thrown.
    I have checked to make sure that I do not a have an initiation form associated with the workflow. At first I thought that an orphaned workflow was the culprit so I re-wrote the email workflow using a different name and I am still getting the same error. Do you have any suggestions?? I thought it was strange that the same action is working great throughout the workflow that and then refuses to work on one instance – against a different workflow.
    By the way – I even applied a manual start to the questioned workflow and it started fine.

    Jim Grooms

    March 20, 2014 at 7:17 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 49 other followers

%d bloggers like this: