Tuesday, 8 March 2016

Get all Workflow Associated Lists in SharePoint Online

We can easily get all the lists and document libraries that are associated with workflow in SharePoint Online using CSOM. In this article, I am going to write C# code example to find and get all workflow enabled lists in a given site using CSOM (Client-Object Model).

Get all workflow enabled lists in a given site

using Microsoft.SharePoint.Client;
//-------------------------------------
static void GetWorkFlowAssociatedListsInSPO()
{
    string siteUrl = "https://tenant-name.sharepoint.com";

    var siteCtx = new ClientContext(siteUrl);

    var secPwd = new SecureString();
    foreach (char c in "MyPassword".ToCharArray()) secPwd.AppendChar(c);

    siteCtx.Credentials = new SharePointOnlineCredentials("admin@tenant.onmicrosoft.com", secPwd);
    Web site = siteCtx.Web;
    siteCtx.Load(site.Lists, li => li.Include(w => w.WorkflowAssociations,
                               w => w.Title, w => w.BaseType));
    siteCtx.ExecuteQuery();
    // Getting all workflow enabled lists
    foreach (List list in site.Lists.Where(a => a.WorkflowAssociations.Count > 0
                                           && a.BaseType == BaseType.GenericList))
    {
        Console.WriteLine("List Name: " + list.Title);
        Console.WriteLine("SubscribedWorkflows: " + 
                     string.Join(";", list.WorkflowAssociations.Select(a => a.Name)));
        Console.WriteLine("---------------------");
    }
}

Get all workflow enabled document libraries in a SharePoint Online site

using Microsoft.SharePoint.Client;
//-------------------------------------
static void GetWorkFlowAssociatedLibrariesInSPO()
{
    string siteUrl = "https://tenant-name.sharepoint.com/sites/contosopartners/News";

    var siteCtx = new ClientContext(siteUrl);

    var secPwd = new SecureString();
    foreach (char c in "MyPassword".ToCharArray()) secPwd.AppendChar(c);

    siteCtx.Credentials = new SharePointOnlineCredentials("admin@tenant.onmicrosoft.com", secPwd);
    Web site = siteCtx.Web;
    siteCtx.Load(site.Lists, li => li.Include(w => w.WorkflowAssociations, 
                       w => w.Title, w => w.BaseType));
    siteCtx.ExecuteQuery();
    // Getting all workflow enabled Document Libraries
    foreach (List list in site.Lists.Where(a => a.WorkflowAssociations.Count > 0
                            && a.BaseType == BaseType.DocumentLibrary))
    {
        Console.WriteLine("List Name: " + list.Title);
        Console.WriteLine("SubscribedWorkflows: " + 
                      string.Join(";", list.WorkflowAssociations.Select(a => a.Name)));
        Console.WriteLine("---------------------");
    }
}

Advertisements
Advertisements

No comments:

Post a Comment