SharePoint 2013: How to use the Workflows Client Object Model (II)!

Continuing with the series of articles about using Workflow Client API, this time I’m sharing how to obtain information about the workflows deployed on a site and that have been executed at some time using this API Client in an Application Console. The code in question is:

static void GetSiteWorkflowsCSOM(string sSiteUrl,

    string sUserName,string sPassword,string sDomain)

{

    using (CSOM_NET.ClientContext spCtx=new CSOM_NET.ClientContext(sSiteUrl))

    {

        NetworkCredential ncCredentials = 

            new NetworkCredential(

                sUserName,

                sPassword,

                sDomain);

        spCtx.Credentials = ncCredentials;

 

        //Sitio

        CSOM_NET.Web spWeb = spCtx.Web;

        //Listas

        CSOM_NET.ListCollection spLists = spWeb.Lists;

        spCtx.Load(spLists);

        spCtx.ExecuteQuery();

 

        //Trabajo con la API de Cliente de Flujos de Trabajo

        CSOM_WF.WorkflowServicesManager wsmWorkflowManager =

            new CSOM_WF.WorkflowServicesManager(

                spCtx,

                spWeb

                );

        CSOM_WF.WorkflowSubscriptionService wssWorkflowSuscriptionService =

          wsmWorkflowManager.GetWorkflowSubscriptionService();

        //Instancia del Servicio de Workflow

        CSOM_WF.WorkflowInstanceService wisWorflowInstanceService =

            wsmWorkflowManager.GetWorkflowInstanceService();

 

        Console.WriteLine(

            "*****Accessing to all the lists in the Site {0} ****",sSiteUrl);

        

 

        foreach (CSOM_NET.List spList in spLists)

        {

            CSOM_WF.WorkflowSubscriptionCollection wsWorkflowSubscriptions =

                wssWorkflowSuscriptionService.EnumerateSubscriptionsByList(spList.Id);

            spCtx.Load(wsWorkflowSubscriptions);

            spCtx.ExecuteQuery();

            

            //Por cada suscripción de Workflow encontrada

            foreach (CSOM_WF.WorkflowSubscription wsWorkflowSubscritpion in wsWorkflowSubscriptions)

            {

                Console.WriteLine("List: {0} - Workflow: {1}",

                    spList.Title, wsWorkflowSubscritpion.Name);

                Console.WriteLine();

 

                CSOM_NET.CamlQuery cqQuery =

                    new CSOM_NET.CamlQuery();

                cqQuery.ViewXml=

                    "<View> <ViewFields><FieldRef Name='Title' /></ViewFields></View>";

                CSOM_NET.ListItemCollection spliListItems =

                    spList.GetItems(cqQuery);

                spCtx.Load(spliListItems);

                spCtx.ExecuteQuery();

 

                //Por cada elemento de lista

                foreach (CSOM_NET.ListItem spliLisItem in spliListItems)

                {

                    CSOM_WF.WorkflowInstanceCollection wicWorkflowInstanceCollection =

                        wisWorflowInstanceService.EnumerateInstancesForListItem(

                        spList.Id, spliLisItem.Id);

                    spCtx.Load(wicWorkflowInstanceCollection);

                    spCtx.ExecuteQuery();

                   

                    //Por cada instancia de Workflow en el elemento

                    foreach (CSOM_WF.WorkflowInstance wiWorkflowInstance in wicWorkflowInstanceCollection)

                    {

                        Console.WriteLine("List Item Title: {0} ", 

                            spliLisItem["Title"]);

                        Console.WriteLine("Workflow Status: {0} ",

                            wiWorkflowInstance.Status);

                        Console.WriteLine("Last Workflow Execution: {0} ",

                            wiWorkflowInstance.LastUpdated);

                        Console.WriteLine();

                    }

                }

            }

        }                

    }

}

The screen output obtained as a result of using the above method is:

image

SharePoint 2013: Uso de la API de Cliente para Flujos de Trabajo (II)!

Siguiendo con la serie de artículos sobre el uso de la API de Cliente para Flujos de Trabajo, en esta ocasión os comparto como obtener la información de los flujos desplegados en un sitio y que han sido ejecutados haciendo uso de dicha API de Cliente en una Aplicación de Consola. El código en cuestión es el siguiente:

static void GetSiteWorkflowsCSOM(string sSiteUrl,

    string sUserName,string sPassword,string sDomain)

{

    using (CSOM_NET.ClientContext spCtx=new CSOM_NET.ClientContext(sSiteUrl))

    {

        NetworkCredential ncCredentials = 

            new NetworkCredential(

                sUserName,

                sPassword,

                sDomain);

        spCtx.Credentials = ncCredentials;

 

        //Sitio

        CSOM_NET.Web spWeb = spCtx.Web;

        //Listas

        CSOM_NET.ListCollection spLists = spWeb.Lists;

        spCtx.Load(spLists);

        spCtx.ExecuteQuery();

 

        //Trabajo con la API de Cliente de Flujos de Trabajo

        CSOM_WF.WorkflowServicesManager wsmWorkflowManager =

            new CSOM_WF.WorkflowServicesManager(

                spCtx,

                spWeb

                );

        CSOM_WF.WorkflowSubscriptionService wssWorkflowSuscriptionService =

          wsmWorkflowManager.GetWorkflowSubscriptionService();

        //Instancia del Servicio de Workflow

        CSOM_WF.WorkflowInstanceService wisWorflowInstanceService =

            wsmWorkflowManager.GetWorkflowInstanceService();

 

        Console.WriteLine(

            "*****Accessing to all the lists in the Site {0} ****",sSiteUrl);

        

 

        foreach (CSOM_NET.List spList in spLists)

        {

            CSOM_WF.WorkflowSubscriptionCollection wsWorkflowSubscriptions =

                wssWorkflowSuscriptionService.EnumerateSubscriptionsByList(spList.Id);

            spCtx.Load(wsWorkflowSubscriptions);

            spCtx.ExecuteQuery();

            

            //Por cada suscripción de Workflow encontrada

            foreach (CSOM_WF.WorkflowSubscription wsWorkflowSubscritpion in wsWorkflowSubscriptions)

            {

                Console.WriteLine("List: {0} - Workflow: {1}",

                    spList.Title, wsWorkflowSubscritpion.Name);

                Console.WriteLine();

 

                CSOM_NET.CamlQuery cqQuery =

                    new CSOM_NET.CamlQuery();

                cqQuery.ViewXml=

                    "<View> <ViewFields><FieldRef Name='Title' /></ViewFields></View>";

                CSOM_NET.ListItemCollection spliListItems =

                    spList.GetItems(cqQuery);

                spCtx.Load(spliListItems);

                spCtx.ExecuteQuery();

 

                //Por cada elemento de lista

                foreach (CSOM_NET.ListItem spliLisItem in spliListItems)

                {

                    CSOM_WF.WorkflowInstanceCollection wicWorkflowInstanceCollection =

                        wisWorflowInstanceService.EnumerateInstancesForListItem(

                        spList.Id, spliLisItem.Id);

                    spCtx.Load(wicWorkflowInstanceCollection);

                    spCtx.ExecuteQuery();

                   

                    //Por cada instancia de Workflow en el elemento

                    foreach (CSOM_WF.WorkflowInstance wiWorkflowInstance in wicWorkflowInstanceCollection)

                    {

                        Console.WriteLine("List Item Title: {0} ", 

                            spliLisItem["Title"]);

                        Console.WriteLine("Workflow Status: {0} ",

                            wiWorkflowInstance.Status);

                        Console.WriteLine("Last Workflow Execution: {0} ",

                            wiWorkflowInstance.LastUpdated);

                        Console.WriteLine();

                    }

                }

            }

        }                

    }

}

La salida por pantalla que se obtiene como resultado de hacer uso del método anterior es:

image