Tuesday, October 11, 2011

How to start Nintex Workflow programtically

 Problem Statement: I have a SPList with Ninetex WF attached to it. The WF triggers for every item creation/updating on the list. But here I am creating/updating list items through SharePoint object model. While doing so, list items are getting updated but Ninetex WF is not triggered.
Reason: In SharePoint 2007, we cannot start the Ninetex WF using “system account” credentials (i.e. Drawback of Ninetex tool).
Below code will help you to run the Nintex workflow even with "System account" credentials also.
SPSecurity.RunWithElevatedPrivileges(delegate())
{
using (SPWeb currentweb = new SPSite(siteUrl).OpenWeb())
{
 currentweb.AllowUnsafeUpdates = true;
 SPList spList = currentweb.Lists["Custom List Name"];
 SPListItem spListItem = spList.Items.Add();
 private SPWorkflowManager wfManager;
 private SPWorkflowAssociationCollection spWorkFlows;
 // To replace the WF name globally
 string WorkflowName =ConfigurationManager.AppSettings["WorkflowName"].ToString();
 wfManager = spList.ParentWeb.Site.WorkflowManager;
 spWorkFlows = spList.WorkflowAssociations;
 foreach (SPWorkflowAssociation sendingWF in spWorkFlows)
 {
  if (sendingWF.Name == WorkflowName.Trim())
  {
    spListItem.Update();
    //To start Ninetex WF after updating Items in list
    wfManager.StartWorkflow(spListItem,sendingWF,
                            sendingWF.AssociationData, true);
    break;
  }
 }
});
Note: SPSecurity.RunWithElevatedPrivileges, it runs with the “System Account” credentials.

No comments:

Post a Comment