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;
}
}
});