1

I have a custom list and associated workflow with it. It is working fine from within SharePoint, but the workflow is not triggered when new item is added from a WebPart.

I have added SPWorkflowAssociation which is also not helping.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
  using (SPSite site = new SPSite(SPContext.Current.Site.Url))
  {
    site.AllowUnsafeUpdates = true;
    using (SPWeb web = site.OpenWeb())
    {
     web.AllowUnsafeUpdates = true;
     SPList ListEmployeeNew = web.Lists["CustomList"];
     SPWorkflowAssociationCollection asscoll = ListEmployeeNew.WorkflowAssociations;
     SPListItem newItem = ListEmployeeNew.AddItem();
     newItem["First Name"] = "Fist Name";
     newItem["Last Name"] = "Last Name";
     newItem.Update();
     foreach (SPWorkflowAssociation spwfa in asscoll)
     {
       site.WorkflowManager.StartWorkflow(newItem, spwfa, spwfa.AssociationData);
     }
     web.AllowUnsafeUpdates = false;
   }
 }
});
3
  • 2
    You have to set value for AssociationProperty AutoStartCreate=True Commented Jan 8, 2016 at 12:58
  • Thanks for your reply, but I am getting error as "New instances of this workflow template are currently disallowed." Commented Jan 8, 2016 at 13:08
  • @DipenShah Thanks for your help, foreach was taking all the versions of workflow, with the name condition it is working fine Commented Jan 8, 2016 at 13:46

1 Answer 1

0

Thanks Dipen Shah for helping to resolve this issue.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
  using (SPSite site = new SPSite(SPContext.Current.Site.Url))
  {
    site.AllowUnsafeUpdates = true;
    using (SPWeb web = site.OpenWeb())
    {
     web.AllowUnsafeUpdates = true;
     SPList ListEmployeeNew = web.Lists["CustomList"];
     SPWorkflowAssociationCollection asscoll = ListEmployeeNew.WorkflowAssociations;
     SPListItem newItem = ListEmployeeNew.AddItem();
     newItem["First Name"] = "Fist Name";
     newItem["Last Name"] = "Last Name";
     newItem.Update();
     foreach (SPWorkflowAssociation spwfa in asscoll)
     {
       if (spwfa.Name == "Workflow Name")
       {
         spwfa.AutoStartCreate = true;             
         site.WorkflowManager.StartWorkflow(newItem, spwfa, spwfa.AssociationData);
       }
     }
     web.AllowUnsafeUpdates = false;
   }
 }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.