I have a custom built web service that is built to simply add items into a list in SharePoint Foundation 2010. I have workflow attached to the list but when I create an item using my web service (which references the SharePoint Object Model) workflows 'Fail on Start'. If I add an item to the list directly within SharePoint the workflows start as required with no problems. I have tried the following so far (including all variations of using them together) but non of these fix the issue:
- Using SPSecurity.RunWithElevatedPrivilages to run my code
- Impersonating a different user to the Administrator and System Account to create the item as
- Running my application pool as the same user as my site's application pool
- Running my application pool as a different account to my site's application pool user The web service is not failing and it is creating the items, just the workflows are not running... can anyone help me with this please?
The code I am using to create the list item is as follows:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite oTempSite = new SPSite(SharePointSite);
SPUser oUserImpersonate = oTempSite.OpenWeb().EnsureUser(UserToEntryAs);
SPSite oSite = new SPSite(SharePointSite, oUserImpersonate.UserToken);
SPWeb oWeb = oSite.OpenWeb();
try
{
oSite.AllowUnsafeUpdates = true;
oWeb.AllowUnsafeUpdates = true;
SPList oList = oWeb.Lists["Sample Log"];
SPListItem oNewItem = oList.Items.Add();
oNewItem["Customer"] = intCustomerID;
oNewItem["Cust. Contact Name"] = strCustomerContactName;
oNewItem["Contact Email"] = strCustomerContactEmail;
oNewItem["Sample Number"] = strSampleNumber;
oNewItem["Notes"] = strNotes;
oNewItem["Application"] = strSampleApplication;
oNewItem["Despatch Method"] = strDespatchMethod;
oNewItem["Cost"] = dblCost;
oNewItem["Sample 1"] = intSampleProductID;
oNewItem["Weight 1"] = strSampleWeight;
oNewItem["Batch No. 1"] = strSampleBatch;
//Handle Account Manager(s):
SPFieldUserValueCollection usrAccountManagers = new SPFieldUserValueCollection();
foreach (string strAcctMrg in AccountManagers.Split(';'))
{
SPUser oUser = oWeb.EnsureUser(strAcctMrg);
usrAccountManagers.Add(new SPFieldUserValue(oWeb, oUser.ID, oUser.LoginName));
}
oNewItem["Account Manager"] = usrAccountManagers;
oNewItem["Content Type"] = "Ingredient Sample"; //Set the content type to be 'Ingredient Sample'
oNewItem["Ingredient Sample Status"] = "Awaiting Result"; //Set the status to default to 'Awaiting Result'
oNewItem.Update();
Thanks in advance...