Permission to start Workflow

Hello,

I'm using Sharepoint 2013 and Visual Studio 2012 to deploy a workflow on a list. I start the workflow programmatically, but some users haven't permission to start it. So I've introduced the SPSecurity.RunWithElevatedPrivileges. In detail the code is:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(properties.OpenWeb().Site.ID))
    {
        using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
        {
     var wsm = new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager(web);
     var subscription = wsm.GetWorkflowSubscriptionService().GetSubscription(new Guid(workflowGuid));
     var wfi = wsm.GetWorkflowInstanceService();
     
     var payload = new Dictionary<string, object>();
     wfi.StartWorkflowOnListItem(subscription, itemCopiedID, payload);
        }
  }
});

The error is System.UnauthorizedAccessException. The RunWithElevatedPrivileges impersonate the SHAREPOINT\system account, it's possible that this account haven't permission to start workflow? If I use another method to impersonate users (only users that are site administrator) and after start workflow, with some users the workflow start but with one of those don't start.

Thanks, Danilo

March 26th, 2013 2:02pm

Hi,

Please try with below code to impersonate System Account.

SPUserToken systemAccountUserToken = SPContext.Current.Site.SystemAccount.UserToken;
using (SPSite elevatedSite =new SPSite("your-site-collection-URL",systemAccountUserToken)) {}

Free Windows Admin Tool Kit Click here and download it now
March 26th, 2013 2:19pm

Thanks for your reply, but don't work. I've the same System.UnauthorizedAccessException exeption.

However SPContext.Current is null, so i've used properties.Web.Site.SystemAccount.UserToken. Where 'properties' is the parameter (type SPItemEventProperties) of the ItemAdded method in EventHandler

Danilo

March 26th, 2013 2:52pm

Hi,

Please check your System Account (which is in Web Apps Pool user) user available in Site Collection Admin.

Also check above user can access SQL DB.

Free Windows Admin Tool Kit Click here and download it now
March 26th, 2013 3:52pm

Excuse me but i don't understand what to do

Thanks

March 26th, 2013 4:08pm

Hi,

Means SharePoint's Web Application Pool User has full access in Site Collection & SQL DB.

Also please try below code.

SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(properties.OpenWeb().Site.ID))
                {
                    using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
                    {
                        //var wsm = new Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager(web);
                        //var subscription = wsm.GetWorkflowSubscriptionService().GetSubscription(new Guid(workflowGuid));
                        //var wfi = wsm.GetWorkflowInstanceService();
                        //var payload = new Dictionary<string, object>();
                        //wfi.StartWorkflowOnListItem(subscription, itemCopiedID, payload);
                        SPListItem listItem = properties.ListItem;
                        SPWorkflowAssociation wfAssoc = listItem.ParentList.WorkflowAssociations.GetAssociationByName("Workflow Name", System.Globalization.CultureInfo.CurrentCulture);
                        listItem.Web.Site.WorkflowManager.StartWorkflow(listItem, wfAssoc, wfAssoc.AssociationData, true);
                    }
                }
            });

Free Windows Admin Tool Kit Click here and download it now
March 26th, 2013 4:49pm

Sorry but...

listItem.ParentList.WorkflowAssociations is empty, also if from inteface i can view the workflow associated with the list.

Danilo

March 26th, 2013 6:26pm

Hi, have you found a solution for that problem?

I'm recieving the same message when running sp 2013 wf programmatically under system account.

Vadim.

Free Windows Admin Tool Kit Click here and download it now
February 6th, 2014 6:43am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics