Hi All,
please help how to terminate a workflow on a perticular list item by csom code.
Description:
I am writing a CSOM code which connect to a site and then list and find all the list items for which already running workflow is throwing error if status is error then cancel/terminate the workflow and retrigger it.
Status :
I got below reference from a site and come to know in csom code I need to use workflow.asmx web service and then get template id to terminate WF from list item.
Please can someone guide and give me sample code for this?
Reference Link:
http://sbrickey.com/Tech/Blog/Post/Mission_Impossible_Remotely_terminating_a_SharePoint_Workflow
Code:
public static void GetWorkflowStatus(string strLibName, string strWFTitle) { List list = cc.Web.Lists.GetByTitle(strLibName); cc.Load(list); cc.ExecuteQuery(); string strWorkFlowStatusColName = Constant.WA_WORKFLOW_STATUS_COL_INT_NAME; CamlQuery query = new CamlQuery(); query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='" + strWorkFlowStatusColName + "' /><Value Type='WorkflowStatus'>" + 3 + "</Value></Eq></Where></Query><ViewFields><FieldRef Name='Title'/><FieldRef Name='" + strWorkFlowStatusColName + "'/><FieldRef Name='ID'/></ViewFields></View>"; ListItemCollection itemCollection = list.GetItems(query); cc.Load(itemCollection); cc.ExecuteQuery(); if (itemCollection.Count > 0) { Utility.CSVWriter("Workflow Error Occurred Item Count: " + itemCollection.Count, LogReport); foreach (ListItem objItem in itemCollection) { try { if (objItem.FileSystemObjectType.Equals(FileSystemObjectType.File)) { ListItem wfStatusFldItm = objItem.File.ListItemAllFields; cc.Load(wfStatusFldItm); cc.ExecuteQuery(); if (wfStatusFldItm[strWorkFlowStatusColName] != null && Constant.WA_WORKFLOW_ERR_OCC_STATUS_CODE.Equals(wfStatusFldItm[strWorkFlowStatusColName].ToString())) { string statusValue = wfStatusFldItm[strWorkFlowStatusColName].ToString(); Utility.CSVWriter("Result for Item ID " + wfStatusFldItm["ID"] + " :" + statusValue, LogReport); //New code after adding Workflow web service Workflow ws = new Workflow(); ws.Url = "http://sp2010hvpkdev08:6789/sites/qr/_vti_bin/workflow.asmx"; ws.Credentials = CredentialCache.DefaultCredentials; } } } catch (Exception ex) { Utility.CSVWriter("Error while getting item status.", LogReport); Utility.CSVWriter(ex.Message, LogReport); } } } else { Utility.CSVWriter("No error occurred Workflow items are found.", LogReport); } }
sudhanshu sharma Do good and cast it into river :)