0

I am using the following code to update the list item and trigger the workflow. But the workflow is not triggering when the item is updated. Any thoughts on this.

 using (SPSite site = new SPSite("http://spweb02:91/test"))
                {
                    using (SPWeb web = site.OpenWeb())
                    {


                        SPUserToken tokentemp = web.SiteUsers[@"i:0#.w|testdomain\" + "gs-test-r"].UserToken;


                        using (SPSite sitetemp = new SPSite("http://spweb02:91/test", tokentemp))
                        {
                            using (SPWeb webtemp = sitetemp.OpenWeb())
                            {

                                try
                                {
                                    sitetemp.AllowUnsafeUpdates = true;
                                    webtemp.AllowUnsafeUpdates = true; 
                                    SPList parentlist = webtemp.Lists["Test"];
                                    SPListItem parent = parentlist.Items[0];
                                    parent["Title"] = DateTime.Now.ToString();                           

                                    parent.Update();
                                    webtemp.AllowUnsafeUpdates = false;
                                    sitetemp.AllowUnsafeUpdates = false;

                                }
                                catch (Exception ex)
                                {


                                }
                            }
                        }
                    }
                }
8
  • Updates to items made by the system account will never fire a workflow. Is this the case here? – Morten K Nov 13 '18 at 12:17
  • It is not system account, I am using the normal user account as mentioned in the code but still the workflow is not triggering. – user17274 Nov 13 '18 at 12:21
  • Ok that wasn't clear to me if it was a system account. I afraid I won't be of much help then. The only thing I can add is if you choose to use Jquery I have a working example that's triggering the workfloow on the newly created item. – Morten K Nov 13 '18 at 13:07
  • Actually the workflow has to be triggered when the item is updated pro-grammatically. Users are not updating the items from UI. – user17274 Nov 13 '18 at 13:12
  • Then you have to make sure that whatever user as triggering the workflow isn't considered a system account. It doesn't matter if a user is updating an item from the UI or for Access for instance (Since it runs under the user credentials) – Morten K Nov 13 '18 at 13:26
0

Run the following code after SPListItem.Update().

SPSite.WorkflowManager.Dispose();

For example:

using(SPSite site = new SPSite(url))
{
    using(SPWeb web = site.OpenWeb())
    {
        SPList list = web.GetList(url);
        SPListItem item = list.Items[1];
        item[updateField] = DateTime.Now.ToLongDateString();
        item.Update();
        site.WorkflowManager.Dispose();
    }
}

Refer to:

SPListItem.Update() not starting workflow

1
  • Sorry for the late reply.. The above mentioned solution is working fine now.. Thanks a lot.. – user17274 Nov 28 '18 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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