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

When I upload an xml document to a document library, the workflow associated with that document library won't start. Here is my the code I use to upload:

            using (SPSite site = new SPSite(@"http://sp2010/"))
                SPWeb web = site.RootWeb;

                SPFolder docLib = web.Folders["DocLib"];

                using (var stream = new MemoryStream())
                    var writer = XmlWriter.Create(stream);

                    // create the file
                    docLib.Files.Add(fileName + ".xml", stream, true);


However, if I take this same xml document, save it to my hard drive and manually upload it by clicking the "Upload Document" button in the document library's ribbon, the workflow starts just fine. Note: The xml is valid and opens just fine regardless of which method I use.

Any thoughts as to why this method of programmatically uploading a document won't start the workflow?

Edit: I have also tried this method with no luck either:

                    SPFile destFile = docLibrary.RootFolder.Files.Add(web.Url + "/" + docLibName + "/" + intakeID + ".xml", stream.ToArray());
                    SPListItem i = destFile.Item;
                    i["Intake ID"] = intakeID;;
                    i["Opened Date"] = DateTime.Now;
                    i["WorkflowFieldName"] = 2;
share|improve this question
I can only guess, but if the workflow is set to start when an item is added to the library, perhaps uploading the file through Files.Add doesn't count as "adding an item", since SharePoint does it for you behind the scenes? Maybe try calling SPDocumentLibrary.AddItem to forcefully add an item to the library, and start the workflow? –  Mihai Nov 24 '12 at 0:43
That unfortunately throws an SPException "To add an item to a document library, use SPFileCollection.Add()" Do you know of another way to upload a file to a document library? –  Meyer Denney Nov 26 '12 at 18:10

3 Answers 3

Create a reference to the Workflow Association and start it yourself.

void startWorkflow(SPListItem item, string workflowName)

    SPWorkflowAssociation wfA = item.ParentList.WorkflowAssociations.GetAssociationByName(workflowName,         System.Globalization.CultureInfo.CurrentCulture);
item.Web.Site.WorkflowManager.StartWorkflow(listItem, wfA, wfA.AssociationData, true);
share|improve this answer
I get a SPException thrown with no error message. The inner message says: "<nativehr>0x8102009b</nativehr><nativestack></nativestack>". According to Google this means a workflow has already started on the item and/or terminated via error. This is strange because as far as I can tell no workflow has started on the item. –  Meyer Denney Nov 28 '12 at 18:58

I am basically having a similar issue. When I programmatically add the file, the workflow starts on create. It will then fail. Sometimes, it appears to not start at all.

What I have gathered from the log files is that the workflow is being started before the add operation is complete. SharePoint throws an error in the workflow because listitem has been modified after the workflow was started.

I have been able to confirm this is my problem by manually starting the workflow on the file after I programmatically upload and it works.

In the code when you add the file, you could stop the workflow from starting automatically, then add the file and then manually start the workflow, then set the workflow back to automatic. This is what I have chose to do, as the other methods (add a delay to the workflow or create an synchronous event receiver to fire the workflow manually on itemadded) are either too much work or sketchy, at best.

Also, I'm pretty sure you do not need to call the update method on the document library after you add the file. This may be creating the lock that is causing the workflow not to fire.

Another thing is that the account that your code is running under to add the file may not have permissions to run the workflow.

** UPDATE **

I just finished my fix for this. Here is my solution. Code could still use a little clean up :-)

private void startWorkflow(SPListItem item, Guid workflowID)

        SPWorkflowAssociation wfA = item.ParentList.WorkflowAssociations.GetAssociationByBaseID(workflowID);
        item.Web.Site.WorkflowManager.StartWorkflow(item, wfA, wfA.AssociationData, true);
    private void ChangeAutoCreateWorkflow(SPDocumentLibrary library, Guid workflowID, bool startOnCreate)
       // SPWorkflowAssociation changedOne = null;
        SPWorkflowAssociation wfA = library.WorkflowAssociations.GetAssociationByBaseID(workflowID);
        wfA.AutoStartCreate = startOnCreate;


//Get folder where I am moving the file to

                        SPFolder uploadsLibrary = item.Web.Folders[uploadsLibraryName];
                        //get library for the workflow
                        SPDocumentLibrary uploads = web.GetListByInternalName(uploadsLibraryName) as SPDocumentLibrary;
                        Guid workflowGuid = new Guid("FAKEACCC-9686-4244-A7AC-A31BDD56625F");
                        //stop the autocreate on the workflow
                        ChangeAutoCreateWorkflow(uploads, workflowGuid, false);
                        //move the file
                        file.MoveTo(uploadsLibrary.Url + "/" + fileName);
                        //get the new file
                        SPFile newFile = uploads.RootFolder.Files[uploadsLibrary.Url + "/" + fileName];
                        //get the file listitem
                        SPListItem newListItem = newFile.Item;
                        //run the workflow on it
                        startWorkflow(newListItem, workflowGuid);
                        //set workflow back to auto start on create
                        ChangeAutoCreateWorkflow(uploads, workflowGuid, true);
share|improve this answer
I thought this was working so I marked correct, but upon further examination I am still getting an Error Occurred for my workflow status. I believe its because when I create my SPFile file object, I am already adding the file to the document library, then later on you have the file.MoveTo line which is moving the file to the same location it is already at. Can you inform me on how you initialized your SPFile file object in this sample code? Thanks! –  Meyer Denney Dec 13 '12 at 18:54
// create the file
docLib.Files.Add(fileName + ".xml", stream, true);

The above statment kicks off the workflow. you do not need Update() on library.

Comment the line after, like this:

 // docLib.Update();

if you need to update some fields after uploading you need Update(). In that case, run your workflow in Item Changed event, not Created.

share|improve this answer

Your Answer


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.