Pages

Friday, December 11, 2009

I had a requirement in which user can cancel / terminate workflow from custom ASPX page, I got good reference from this site http://blog.brianfarnhill.com/2008/10/01/how-to-cancel-a-workflow-programmatically/ regarding how to cancel workflow programmatically, but what I had discovered was that normal user were getting error while canceling workflow. User who has full control rights into site, they can able to cancel workflow without any errors.

ERROR:   

Value does not fall within the expected range.   at Microsoft.SharePoint.Library.SPRequestInternalClass.CancelWorkflow(String bstrUrl, String bstrListName, Guid workflowInstanceId)

Solution:

It’s because of user permission level, from out of the box workflow status page also, contributor user don’t have permission to “Terminate this workflow” functionality, so we need to put Cancel workflow logic into RunWithElevatedPrivileges delegate code. After that normal user can also cancel workflow without any errors.

SAMPLE CODE:

SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))

                    {

                        using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))

                        {

                                                            //object model code for cancelling workflow

                                   }

                    }

              });

Enjoy!

No comments:

Post a Comment