Luke
Luke

Reputation: 422

Workflows Fail on Start when adding an item to a list programmatically

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:

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...

Upvotes: 1

Views: 1797

Answers (1)

Luke
Luke

Reputation: 422

After researching the ULS log errors I found the issue was with my web.config file on my Web Service that needed the Authorized types declaring for the workflows to be able to run on the item I had created.

I used this post to help me resolve it: http://social.msdn.microsoft.com/forums/en-US/sharepointworkflow/thread/71d23c0e-24c5-4d61-8d62-265c374ac81c/

Upvotes: 2

Related Questions