locked
declarative workflows not running when items are added to list from Visual Studio workflow RRS feed

  • Question

  • I have a workflow designed in Visual Studio. The workflow code adds items to a SP list, then executes the declarative WF attached to the list.

    I have another declarative WF created in SP Designer that is also triggered when a item is added to the list. This WF doesn't run though. 

    When I look at the list item created by the VS WF in edit mode, it says it was created by the system account. I have read that since SP1, the system account can't run declarative WFs unless you set the app pool on the SP server to run as a domain user. We did this but the WF still doesn't run. I don't know if I have to impersonate a user in my code when I initially add the list item in the VS WF or not, and I have been banging my head finding the code to do it. My objective is to have the list item created as the user that triggered the VS WF so thier name shows up as the "created by" when you edit the list item.

    Summary:

    -VS WF creates list item, runs attached WF

    -Newly created list item should trigger a declarative WF created in SP designer (but doesn't)

    -I can manually execute the WF from SP and it works.

    Hope that makes sense...

    Thursday, May 10, 2012 1:51 AM

Answers

  • Thank you for this post.

    I hae a similar situation in MOSS2007 that I hope you can help me with. After an item is added to a sharepoint library, a workflow is supposed to populate a column with a modified version of the UserName (it takes their last name). It works for me, the admin. Other accounts have the same error message you had been getting: access denied for the SHAREPOINT\System account.

    So my question is you could clarify the two sections in your code for me? It looks like one is related to permissions, but is the other section the actual workflow code? My workflow was created in SPD2007. Will any part of this example help me in that case?

    I know nothing about coding nor do I know where to put this code. Any help would be appreciated.


    Wednesday, May 16, 2012 8:20 PM

All replies

  • I found a solution:

            private SPUserToken GetOwnerToken()
            {
                /* have to impersonate a domain user (site owner) because visual studio creates items
                 * as the system account, which cannot execute declarative workflows
                 * attached to the lists. 
                 */
                try
                {
                    SPUser user = null;
                    using (SPSite site1 = new SPSite(workflowProperties.SiteId))
                    {
                        user = site1.Owner;
                    }
                    SPUserToken token = user.UserToken;
                    return token; 
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
    
        public void addApproval(string approvalType, string approverColumn) 
        { 
            SPSecurity.RunWithElevatedPrivileges(delegate() 
            { 
                //get the site owner token 
                SPUser user = null; 
                using (SPSite site1 = new SPSite(workflowProperties.SiteId)) 
                { 
                    user = site1.Owner; 
                } 
                SPUserToken token = user.UserToken; 
     
                using (SPSite site = new SPSite(workflowProperties.SiteId, token)) 
                { 
                    using (SPWeb web = site.OpenWeb(workflowProperties.WebId)) 
                    { 
                        SPList list = web.Lists["Approvals"]; 
                        SPListItem item = list.Items.Add(); 
                        item["Title"] = "ECO Approval"; 
                        item["Approval Type"] = approvalType; 
                        item["Approver"] = workflowProperties.Item[approverColumn]; 
                        item["ECO ID"] = workflowProperties.Item["ID"]; 
                        item["ECO Number"] = web.Url + "/Lists/ECO/DispForm.aspx?ID=" + workflowProperties.ItemId + ", " + workflowProperties.ItemId; 
                        item.Update(); 
                        SPWorkflowManager objWorkflowManager = null; 
                        SPWorkflowAssociationCollection objWorkflowAssociationCollection = null; 
                        objWorkflowManager = item.Web.Site.WorkflowManager; 
                        objWorkflowAssociationCollection = item.ParentList.WorkflowAssociations; 
                        foreach (SPWorkflowAssociation objWorkflowAssociation in objWorkflowAssociationCollection) 
                        { 
                            SPWorkflow.CreateHistoryEvent(this.workflowProperties.List.ParentWeb, workflowId, 0, workflowProperties.OriginatorUser, TimeSpan.MinValue, "WorkFlow " + objWorkflowAssociation.Name, "WorkFlow " + objWorkflowAssociation.Name, "WorkFlow " + objWorkflowAssociation.Name); 
                            if (objWorkflowAssociation.Name == "Approval Workflow") 
                            { 
                                site.WorkflowManager.StartWorkflow(item, objWorkflowAssociation, objWorkflowAssociation.AssociationData); 
                            } 
                        } 
                    } 
                } 
            }); 
        } 
    
    The list item now shows up as being created and modified by a user (owner) instead of the system account.
    Friday, May 11, 2012 5:19 PM
  • Thank you for this post.

    I hae a similar situation in MOSS2007 that I hope you can help me with. After an item is added to a sharepoint library, a workflow is supposed to populate a column with a modified version of the UserName (it takes their last name). It works for me, the admin. Other accounts have the same error message you had been getting: access denied for the SHAREPOINT\System account.

    So my question is you could clarify the two sections in your code for me? It looks like one is related to permissions, but is the other section the actual workflow code? My workflow was created in SPD2007. Will any part of this example help me in that case?

    I know nothing about coding nor do I know where to put this code. Any help would be appreciated.


    Wednesday, May 16, 2012 8:20 PM
  • The code is from a workflow designed in Visual Studio, not in SharePoint or SharePoint Designer, so I don't know how much it will help you. That said, the first section is getting a authentication token for the site owner. I put it into a separate function (GetOwnerToken) because I need to call it from other methods. The methods (addApproval for example) use the token to run the workflows.

    You may want to look at this article:

    http://support.microsoft.com/kb/947284

    It says to set the application pool account in SP to use a different user account (domain user preferably).

    Friday, May 25, 2012 8:39 PM