lundi 9 mars 2015

Unable to get the list of all SP2010/SP2013 Work Flows (OOTB and SPD) which are associated with a List using CSOM



We came across an issue while pulling the workflow associations of lists, CSOM and Splist.WorkflowAssociations will not list the workflows created through SPD and attached to list. If anyone faced below issue earlier and could share insights on this would be helpful.


Please Find Below the detailed information on the issue we are facing.


Example:



  1. SP_WorkFlow_13 => (We have created this Workflow using SPD and have selected Platform Type: “SharePoint 2013 WorkFlow”)

  2. SP_WorkFlow_10 => (We have created this Workflow using SPD and have selected Platform Type: “SharePoint 2010 WorkFlow”)

  3. Also we have few OOTB work flows


Case1: If we write the code using CSOM approach to get all work-flows which are associated with the list, then it’s showing only SPD Work Flow which is created using Platform Type: “SharePoint 2013 WorkFlow”


Case2: If we write the code using Server Side approach to get all work-flows which are associated with the list, then it’s showing SPD Work Flow which is created using Platform Type: “SharePoint 2010 WorkFlow” and also showing OOTB Workflow



Requirement: We need the details/list of all workflows(SPD/2010/2013/OOTB), which are associated with any list, using CSOM Approach.






CSOM Code




public void GetListWorkflowAssociations(string siteCollectionUrl)
{
// connect to the workflow services via a CSOM client context
var clientContext = new ClientContext(siteCollectionUrl);
var workflowServicesManager = new WorkflowServicesManager(clientContext, clientContext.Web);

Web web = clientContext.Web;
List list = web.Lists.GetByTitle("CustomList_WF_POC");
clientContext.Load(list);
clientContext.ExecuteQuery();

// connect to the subscription service
var workflowSubscriptionService = workflowServicesManager.GetWorkflowSubscriptionService();

// get all workflow associations
var workflowAssociations = workflowSubscriptionService.EnumerateSubscriptionsByList(list.Id);

clientContext.Load(workflowAssociations);
clientContext.ExecuteQuery();

foreach (var association in workflowAssociations)
{
string name = association.Name;
Console.WriteLine(name);
}
}



Server Side Script




public List<ListWorkflowBase> GetWorkflowAssociationsToList(SPList spList, string webUrl,
string webApplication = "N/A", string sitecollection = "N/A")
{
List<ListWorkflowBase> objLWFBase = new List<ListWorkflowBase>();
SPWorkflowAssociationCollection workFlowCollection = spList.WorkflowAssociations;
foreach (SPWorkflowAssociation workFlow in workFlowCollection)
{
ListWorkflowBase obj = new ListWorkflowBase();
obj.ListId = spList.ID.ToString();
obj.ListTitle = spList.Title;
obj.BaseId = workFlow.BaseId.ToString();
obj.Created = workFlow.Created.ToString();
obj.Description = workFlow.Description;
obj.Id = workFlow.Id.ToString();
obj.Modified = workFlow.Modified.ToString();
obj.Name = workFlow.Name;
obj.ParentList = workFlow.ParentList.ToString();
obj.InternalName = workFlow.InternalName;
obj.Enabled = workFlow.Enabled.ToString();
obj.GloballyEnabled = workFlow.GloballyEnabled.ToString();
obj.HistoryListId = workFlow.HistoryListId.ToString();
obj.HistoryListTitle = workFlow.HistoryListTitle;
obj.ParentSite = workFlow.ParentSite.ToString();
obj.ParentWeb = workFlow.ParentWeb.ToString();
obj.WebUrl = webUrl;
obj.WebApplication = webApplication;
obj.SiteCollection = sitecollection;
objLWFBase.Add(obj);
return objLWFBase;
}
}







0 commentaires:

Enregistrer un commentaire