Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

We have created a group of SPD workflows that are associated to different document libraries in a Record Center. The workflow themselves work great, but right now (after installing the latest CU [december 2011]) the workflows are not starting up automatically even though they are configured to do so.

I looked online and discovered that workflows will not start automatically if they are using the system account. These workflows seem to be using that account (for example, after the workflow is manually run, the modified by field says System Account).

Something worth mentioning is that these items are moved automatically to the correct document library by the Record Center's Content Organizer rules, so technically, the user that is adding these items to the respective DL is the System Account (I'm assuming that the content organizer rules are run by the system account)

My question is how can I change the workflow so that it runs with a user account, or reconfigure the Content Organizer rules to achieve the same effect.

share|improve this question
add comment

2 Answers

The answer is to use an event receiver. The following code is what I wrote based on the suggestions of someone in the MSDN Forums. This has worked really well for me, and I think it will solve your problem as well.

    /// <summary>
    /// An item was added.
    /// </summary>
    public override void ItemAdded(SPItemEventProperties properties)
    {
        StartWorkflow(properties.ListItem);
        base.ItemAdded(properties);
    }// end ItemAdded()

    /// <summary>
    /// An item was updated.
    /// </summary>
    public override void ItemUpdated(SPItemEventProperties properties)
    {
        StartWorkflow(properties.ListItem);
        base.ItemUpdated(properties);
    }// end ItemUpdated()

    /// <summary>
    /// Start the workflow associated with the specified list item
    /// </summary>
    /// <param name="item"></param>
    private void StartWorkflow(SPListItem item)
    {
        if (item.ContentType.WorkflowAssociations.Count > 0)
        {
            // Grab the item's Editor attribute to get the user who last checked it in
            string author = item["Author"].ToString();
            int userID = Convert.ToInt32(author.Substring(0, author.IndexOf(";#")));

            // create user token for authenticating workflow start
            SPUser wfAuth = item.ParentList.ParentWeb.AllUsers.GetByID(userID);
            SPUserToken wfAuthToken = wfAuth.UserToken;

            // using the authentication for the item's last author, get a new site collection context
            using (SPSite site = new SPSite(item.ParentList.ParentWeb.Site.ID, wfAuthToken))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // new instance of the item's list and item with the correct authentication
                    SPList list = web.Lists[item.ParentList.ID];
                    SPListItem uItem = list.GetItemById(item.ID);

                    // get the workflow manager ready
                    SPWorkflowManager mgr = site.WorkflowManager;
                    SPWorkflowAssociationCollection wfacol = uItem.ContentType.WorkflowAssociations;

                    foreach (SPWorkflowAssociation wfa in wfacol)
                    {
                        mgr.StartWorkflow(uItem, wfa, wfa.AssociationData, true);
                    }
                }
            }
        }
    }// end StartWorkflow()
share|improve this answer
add comment

I believe it does not matter who creates the document into the document library. In your case, if its done through the Record Center's Content Organizer rules, let it be.

It always matter who has deployed the workflow. What you should do is, open the workflow in SharePoint Designer using an account who has rights to deploy the workflow, possibly a Site Owner or so. Save the workflow again and republish it. This will effectively change the account that is associated with the workflow, which will allow it to start automatically.

share|improve this answer
    
I created a new account (sp.workflows), assigned it to the Site Collection Administrators and used it to re-publish the workflows. Still, when they are run, all comments on the workflow history, and the changes appear as the System Account –  Carlos G. Jan 23 '12 at 6:26
    
You could use the impersonation step, if you are in with your workflows users all the steps in the impersontation step will be run under that user –  Anders Aune Jan 23 '12 at 6:32
    
The problem with impersonation Anders is that the workflow is still executed by the System Account and my objective is to have the workflow start automatically, something that apparently cant happen if the executor is the System Account –  Carlos G. Jan 23 '12 at 7:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.