0
votes

I've search everywhere and I still am unable to solve my problem. Scenario: I have a list where I will create items that must be anonymous. So we use a provider hosted SharePoint addIn to create the list item using AppOnly permissions. Once the list item is created, there is a SharePoint 2010 workflow that must run. The reason is that it will email external email addresses. Unfortunately, when the AddIn creates the list item, because it is being created by an App and not a user, the workflow does not get fired automatically. So I thought, Easy! I'll just add code to the AddIn which triggers the workflow once the item is created. Well, here is where the problems start. I get a Permission Denied error when I try to start the 2010 workflow. So I thought an easy fix would be to create a 2013 workflow, give it App permissions and have it start the 2010 workflow in an App Step. I can start the 2013 workflow, but it stops at trying to start the 2010 workflow with a Permission Denied error. So I am stuck! Here is the code that should start the 2010 workflow:

public static void Start2010Workflow(ClientContext clientContext, string listName, int itemId, string workflowName, Dictionary<string, object> wfParams)
{
    var web = clientContext.Web;
    var workflowServicesManager = new WorkflowServicesManager(clientContext, web);
    var workflowInteropService = workflowServicesManager.GetWorkflowInteropService();
    clientContext.Load(web);
    clientContext.Load(workflowInteropService);
    clientContext.ExecuteQuery();       
    List sharePointList = web.Lists.GetByTitle(listName);
    ListItem sharePointListItem = sharePointList.GetItemById(itemId);
    clientContext.Load(sharePointList);
    clientContext.Load(sharePointListItem);
    clientContext.ExecuteQuery();

    Guid itemGuid = Guid.Empty;
    if (sharePointListItem.FieldValues.ContainsKey("GUID"))
    {
        object temp = sharePointListItem.FieldValues["GUID"];
        if (temp != null)
            itemGuid = (Guid)temp;
    }
    var wfServManager = new WorkflowServicesManager(clientContext, clientContext.Web);
    var wfInteropService = wfServManager.GetWorkflowInteropService();
    clientContext.Load(wfInteropService);
    clientContext.ExecuteQuery();
    //Start the Workflow
    ClientResult<Guid> resultGuid = wfInteropService.StartWorkflow(workflowName, new Guid(), sharePointList.Id, itemGuid, wfParams);

    clientContext.ExecuteQuery();
}

As soon as that last ExecuteQuery is run I get a : Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: 'Access denied. You do not have permission to perform this action or access this resource.'

I have tried multiple ways. I am able to list the Workflows, but I cannot start them. I have no problems starting a SharePoint 2013 workflow.

I also tried setting the credentials to my own with the following code:

SecureString password = new SecureString();
foreach (char c in "MyP@5sW0rD89@12".ToCharArray()) password.AppendChar(c);
Context.Credentials = new SharePointOnlineCredentials("[email protected]", password);

With no success.

Has anyone actually managed to get this working? Or maybe provide another workaround idea??

Thanks!

1

1 Answers

1
votes

I had the same exception working in my onprem environment, so it's not entirely the same situation and i didnt test it for SPO... As i didnt find any other solution i applied the following workaround

  1. Look for the service account the addin is running with (IIS -> Application Pools -> Identity domain\username)
  2. Grant this account the required permissions to start workflows in the site collection you need to start workflows in
  3. In your code recreate the ClientContext without the TokenHelper (e.g. new ClientContext(clientContext.Url))
  4. Use your code above with the new ClientContext

This should do the trick... I know it is far from ideal and not the idea of provider-hosted AddIns in the first place, but had to get it to work. Any other ideas will be appreciated!