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;
   }
 }
});
  • 2
    You have to set value for AssociationProperty AutoStartCreate=True – Dipen Shah Jan 8 '16 at 12:58
  • Thanks for your reply, but I am getting error as "New instances of this workflow template are currently disallowed." – Rao Jan 8 '16 at 13:08
  • @DipenShah Thanks for your help, foreach was taking all the versions of workflow, with the name condition it is working fine – Rao Jan 8 '16 at 13:46
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, privacy policy and cookie policy

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