SharePoint Events

  5/13/2013 - Conference: SharePoint Summit 2013
  5/21/2013 - Webcast: SharePoint 2013 and ECM: Content Migration and Storage
  5/22/2013 - Webcast: Managing CAD in SharePoint
  5/23/2013 - Webcast: SharePoint Document Automation and E-Forms for Financial Services
  5/24/2013 - Webcast: What's New in Search for SharePoint 2013

 SharePoint Videos

  Why SharePoint 2013
  SharePoint 2013 Launch
  SharePoint 2013 Migration and Governance
  SharePoint 2013 and Enterprise Content Management
  Top Benefits of SharePoint 2013
  What's New in Business Intelligence in Office and SharePoint 2013
  SharePoint and Office 2013 Integration
  SharePoint 2013 Infrastructure Preview
  SharePoint, Lync, and, Exchange in the Cloud with Office 365
  Advanced Reporting in SharePoint with Microsoft Power View

 Archives

Opening SharePoint Links in a new windowUse SHIFT+ENTER to open the menu (new window).
Mail Enabled Lists vs. The Missing Windows 2008 POP3/IMAP Server Use SHIFT+ENTER to open the menu (new window).
7 Tools for SharePoint DevelopersUse SHIFT+ENTER to open the menu (new window).
Public Facing Masterpage TechniquesUse SHIFT+ENTER to open the menu (new window).
How to Quickly Deploy and Activate a Timer Service to Your Site CollectionUse SHIFT+ENTER to open the menu (new window).
Custom SharePoint Master Page Feature with WSP BuilderUse SHIFT+ENTER to open the menu (new window).
Date Math with InfoPathUse SHIFT+ENTER to open the menu (new window).
Enterprise Search Tricks and Tips Part 1Use SHIFT+ENTER to open the menu (new window).
Populating Word Documents With SharePoint Data. Try The DIP!Use SHIFT+ENTER to open the menu (new window).
Programmatic Deep Dive into Blank SharePoint Lookup ColumnsUse SHIFT+ENTER to open the menu (new window).
1 - 10 Next
Creating a SharePoint Loopback Workflow

By: Neil Barkhina

In the many years that I have worked with SharePoint workflows, I have found that one most asked for components is a loopback. The idea behind the loopback workflow is if you have for example 2 approvers, where the first one approves and the second rejects, the workflow should “loop back” to the first approver. This process will continue until both parties give their approval.

There are many different ways to create and leverage workflows in SharePoint 2010. Generally speaking, workflow comes in 3 different flavors:

1)   Out of the Box: these include all the workflows you get in the SharePoint interface including Approval 2010 and Collect Signatures.

2)   SharePoint Designer: you have more control over actions such as sending emails, delays, and variables. Deployment is easy and can be done by non-developers.

3)   Visual Studio: this gives you full control of your workflow leveraging the entire .NET framework. However the solutions can only be deployed by a system administrator through central admin.

In order to create a true loopback workflow, the only option I have found is through Visual Studio. I was really hoping that Designer would allow for loopbacks but that did not make it in this release of SharePoint. When searching the web for information on how to create a loopback workflow, I had trouble finding a good template or sample code. In this post I will highlight the key components of this workflow and at the end you will also find a link to download the code so you can use this as a starting point in creating your own loopback workflows.

Workflow Architecture

Creating_a_SharePoint_Loopback_Workflow

The entire workflow runs inside a single while activity. Within that is a sequence activity with two more while activities for the two task change events.

While Loop

The main while loop uses a code condition to check if both people have approved their tasks. This is stored in the workflow code as simple bool variables.

private void while_bothnotapproved(object sender, ConditionalEventArgs e)

        {

            if (accounting_approved.Equals("Approved") && marketing_approved.Equals("Approved"))

                bothapproved = true;

            if (bothapproved)

                e.Result = false;

            else

                e.Result = true;

        }

Creating the task

The next component is creating the task. This is done in the createTask activity. You have to set the task ID to a new GUID because each time it loops back you need to make sure it’s a new task. Then you set some properties on the task such as the title, assigned to and percent complete. In this example I simply hard coded the Assigned To field but what you can do is capture these values from a workflow initiation page.

private void createTask1_MethodInvoking(object sender, EventArgs e)

        {

            this.createTask1_TaskId1 = Guid.NewGuid();

            SPListItem item = workflowProperties.Item;

            this.createTask1_TaskProperties1.Title = "Review task for Accounting - " + item.Name;

            if (createTask1_TaskProperties1.AssignedTo == null)

            {

                this.createTask1_TaskProperties1.AssignedTo = "gig-werks\\neilb";

            }

            this.createTask1_TaskProperties1.PercentComplete = 0.0f;

            this.createTask1_TaskProperties1.SendEmailNotification = true;

        }

 

Within the workflow designer you will need to set up several properties. I have found that these settings must be done perfectly or else the workflow will bomb. The key things to remember here are when setting the TaskId and TaskProperties, I like to use fields versus .NET properties. For the correlationToken just type taskToken1 and make sure the ownerActivityName is the sequence activity. On the second task you will type taskToken2 with the same ownerActivityName.

 

Creating_a_SharePoint_Loopback_Workflow

On Task Changed

How you want to determine the completion of the tasks is entirely up to you. You can check for the % complete, or the Status. In my case I actually chose to have a Choice Column on the Task List with the choices “Pending”, “Rejected” and “Approved”. During the On Task Changed event I simply extract that value using the following code:

private void onTaskChanged1_Invoked(object sender, ExternalDataEventArgs e)

        {

            try

            {

                accounting_approved = workflowProperties.TaskList.Items.GetItemById(onTaskChanged1_AfterProperties1.TaskItemId)["Approval"].ToString();

            }

            catch (Exception ex)

            { }

           

        }

 

Again as with the Task Created activity, make sure you use that same taskToken1 Correlation Token and also set the owner to sequenceActivity1.

Creating_a_SharePoint_Loopback_Workflow

As I mentioned earlier, after I deployed this solution I created a choice column on the Task List called Approval. I also hid all the other columns from the Workflow Task content type to make the interface a little friendlier for end users. Once all this is done, here is what your edit form should look like:

http://www.thesharepointblog.net/PublishingImages/Creating_a_SharePoint_Loopback_Workflow_Picture4.png

If you would like to download the visual studio project, you can get it here:

http://www.thesharepointblog.net/Documents/LoopbackWorkflow.zip

By: Neil Barkhina

        

Comments

SC Vinod

Hi,

Nice post, Can't u accomplish this using a State Machine workflow instead of a Sequential workflow.
at 6/30/2011 2:54 AM

Neil

Hey, thanks for the question. You could do a state machine workflow, but I wanted to show serial workflows because they are a little bit easier to grasp. The built in Microsoft Approval workflow is actually also implemented using a serial workflow, but a state machine would be a great idea as well.
at 6/30/2011 1:50 PM

Everton

This is exactly what I need to develop...

I created a project (sequential sharepoint 2010 workflow) with source code very very very similar to try understand the code. But, the workflow doesn't work... In iteration 1 the workflow works perfectly (create the accounting task and the marketing task). In iteration 2 the workflow doesn't work. The workflow doesn't create the "new tasks" and doesn't stop in "ontaskchanged" activity. So, the workflow stays in infinite loop (but without create tasks and without wait tasks changed).

I downloaded your code, and the workflow works fine...

Help me, please? Could you look at my project, please?
at 2/10/2013 11:26 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Your Name *


e-mail address *


Website (optional)


Comment *


Attachments

 Subscribe

  GigWerks RSS  Gig Werks Mailing List 

 Contact Us

 Connect

 Resources

  On Demand SharePoint Webcast Recordings
  Upcoming Webinars
  SharePoint Resources
  Business Intelligence Resources
  Gig Werks Website



©2009 Gig Werks. All rights reserved. Privacy Policy