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);
intake.WriteTo(writer);
writer.Flush();
// create the file
docLib.Files.Add(fileName + ".xml", stream, true);
docLib.Update();
}
}
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;
destFile.Update();
i.Update();
Files.Adddoesn'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