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

Un pensamiento en “SharePoint 2013: How to use the Workflows Client Object Model (II)!

  1. Pingback: SharePoint 2013 & Office 365: Resumen de posts (LXVIII)! | Pasión por la tecnología...

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s