Saturday, August 25, 2012

How to do Bulk Approvals in Sharepoint custom workflow developed using Visual Studio

As you might be aware that Sharepoint is a great tool which is used by almost all enterprises these days. One big & solid reason as why it has become such a hit is that, it integrates well with the Microsoft's infrastructure which can't be ignored! The other, it helps the enterprises to enable their business process smoothly and enforce the same among it's emaployees to carry out their operations properly. In face we ca say that the businesses run on Sharepoint if it helps to business to operate properly!
One of the most popular use of Sharepoint 2007 / 2010 is enabling business process in an organization using worklows. Sharepoint allows great ways to customize and build enterprise class application and business process to be converted in an easy to use, familiar, well integrated secured infrastructure of Micorsoft.

I have been involved in developing workflows using Sharepoint for over 3 years by now. Recently, my team got a requirement from the business that required workflow approvals to be done in bulk i.e bulk approvals of sharepoint workflows. You might me thinking of it as complex scenario as we also thought it like that. But, to my surprise and intuitive guess it turned out to be a whole lot easier! I will explain below what I did in simple steps to you:

1. I used a Grid View control to display all the tasks assigned to an approver in a new page.
2. Put Check box control to each row in the grid view and placed Approve and Reject buttons at the bottom of the page.
3. Next step was to get all selected tasks in a for loop when the user selected tasks using check boxes from the grid view and clicked Approve or reject button. (pretty simple so far!)
4. On button click I took all the grid view rows in the loop and catched the selected rows ID.
5. From the ID of the task I used AlterTask () method to call On Task Changed activity code/functionality. I set the boolean variable to true in case of Approvals and false in case of Rejection. I used this boolean value in my workflow logic to move or proceed the workflow to the next level in case it is True, and terminate or stop it in case it is Rejected (boolean value False).

To my surprise my code worked wonderfully and all the tasks got approved and rejected very smoothly!
Note the key here is to fire the AlterTask() for each selected row in the grid which we use anyways to fire the ChangeTask Activity using the task approvals or task edit forms.
I tried to Google it a lot before trying out this idea by using keywords like Bulk approvals of sharepoint workflow or batch approvals of sharepoint workflow tasks but I didn't get any clue. Finally, I tried this POC and it worked. I am sharing the same so that you may not have to stumble upon much.

Please note that the above solution presented is developed for SharePoint 2007 using Visual Studio 2008. Please feel free to contact me in case you need the working code also. Happy to Help!!