Planet Technologies

Solving Recursive Loops in SharePoint Designer Using SandBoxed Solutions

Email | Print

This is a blog post that I looked forward to writing; this post deals with a problem that has plagued SharePoint Workflows when limited to using SharePoint Designer, and a solution is here now in SharePoint 2010 using Sandboxed Solutions. What problem is this you may ask, well it is the means by which you implement a loop structure in your workflow, you may notice that here is no such activity as an available option in SharePoint Designer 2007 or 2010, hmm I wonder will it be in the next version?  Inquiring minds want to know.

Problem Stated
You have two SharePoint Lists, data elements in List A are related to data elements in List B. You want to make a change to a singular item in List A and for every related element in List B, you want to effectuate a change as well.
In practical terms let’s say that you have a pool of candidates that you plan to hire, those candidates are in List A, they will go through a list of interviews with individuals and teams. You want a workflow such that if (1) During the process, the candidate drops out of consideration [Cancelled], is put on a wait list [Deferred], or “knows someone” so they don’t need all those formalities [Approved].
How would you do it if you were limited to creating the workflow in SharePoint Designer 2010?

BlogPic1
Above: List A – The Candidates Table (Before Shot)

BlogPic2
Above: List B- The Interview Board Tasks (Before Shot)

Solution Design
Now, there are several post and guidance’s out there in the Blog-osphere that speak to solutions that employ using Multiple List and Libraries to act as counters with delay activities to ensure that the timing is maintained between iterating through the items in LIST B when the helper table managing the counter is in a work state; now I have my opinions on those solutions, but notwithstanding that, I fully understand that those solutions in the past were necessary in order to get the job done. Enter today with SharePoint 2010 and Sandboxed Solutions; this affords us the ability to write targeted solutions that are scoped at the Site Collections level and accessible through SharePoint Designer tooling, here is an excerpt of what a Sandboxed Solution is taken from Microsoft’s site.
“A sandbox is a restricted execution environment that enables programs to access only certain resources, and that keeps problems that occur in the sandbox from affecting the rest of the server environment. Solutions that you deploy into a sandbox, which are known as sandboxed solutions, cannot use certain computer and network resources, and cannot access content outside the site collection they are deployed in. For more information about solutions, see Solutions Overview(http://go.microsoft.com/fwlink/p/?LinkID=156638).”

Approach
Using Custom Workflow Activities, create a loop structure to look for changes in List A (on changed event fires) and then traverse List B, looking for the qualifying criteria that will count as a match in List A, then update that item. Loop through the entire list until all every list item has been evaluated. This logic will be encompassed in a Method of a Class you will create, the class must be scoped “public” and the method will take as a parameter “SPUserCodeWorkflowContext” object and it will return a type of Hashtable, also remember that your feature needs to be Scoped at “Site”.
• Create a Visual Studio Empty SharePoint Project that is Scoped to the Sandbox
• Create Logic to use the SharePoint API to traverse items in List B based on changes in List A
• Deploy the Solution, open up SharePoint Designer for the Site Collection it is Activated on
• Use the Custom Activity in SharePoint Designer as you would for any other OOB Activity

Build
1. Create your SharePoint Project
Begin by launching Visual Studio and create an Empty Project, scoped at Sandboxed Solution in the Project Properties or as you are setting up your Project for the first time. Next add a Class to create the method.

BlogPic3

You are not over quite yet, now you need to make this available to SharePoint Designer, we do that by adding an Empty Module to our project. In my example below I am calling it CASDefinition and it includes and Elements.xml file that I will modify as such…

BlogPic4

You will notice that the structure of the file lends itself to how it will be viewed and used in SharePoint Designer; also there is a reference to the class file function names, class names[lines 4 through 8], what category (placement)[myself serving line 10] it will appear in SharePoint Designer. Next you have parameters, now certainly here I could have made this configurable by taking the List name as an input Parameter or maybe a specific parameter that I wanted to update rather than all, but this is a demo, you get the idea, for now I have an output parameter.
By now, your project should look like this below.

BlogPic5

A few housekeeping notes here, I did mention it above but just to re-enforce it, please change the Feature File to “Site” Scope so it is available at the Site Collection level, and you can see in the project properties that the “Sandboxed Solutions” property is set to true. One more thing, you will see that there is another project in there, “CASTester”, that is just a console app I use for testing ANY solution logic, it is not necessary for this to work.
Assemble your SharePoint Designer Workflow
Now that you have deployed your package, you should see it as an available option in SharePoint Designer 2010 under Workflow Actions.

BlogPic6

Notice the Fabian Custom Actions section and the Name of the Custom Workflow Activity therein. Now at this point you can set this workflow to run on a Changed Event in List A, or set it for Manual, either way, when it runs, it will subsequently change all items related in List B accordingly.

BlogPic7
Above: List A – The Candidates Table (After Shot)

BlogPic8
Above: List B- The Interview Board Tasks (After Shot)

Conclusion
So now you have a tried and tested way of doing Loops in SharePoint Designer 2010 albeit through a Custom Workflow Activity within the boundaries of a Sandboxed Solution in SharePoint 2010. In addition, if this solution goes rogue, then as it is indeed a Sandboxed Solution, a resource threshold may be set to halt it from running based on settings and rules configured by the Site Collection administrator.

Cheers, happy SharePoint-ing

Written by: Fabian Williams, Planet Technologies SharePoint Architect, Microsoft Dynamics SharePoint Partner

Related Posts



Ask This Expert a Question or Leave a Comment

Switch to our mobile site