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

Dentro de las distintas posibilidades que nos proporciona el Modelo de Objetos en Cliente de SharePoint 2013, tenemos la posibilidad de interactuar con los Flujos de Trabajo desplegados haciendo uso del ensamblado específico que para el Service Pack 1 (SP1) está disponible en la ruta: C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI. En concreto, el ensamblado a utilizar es Microsoft.SharePoint.Client.WorkflowServices.dll. Como ejemplo de uso, el siguiente script permite obtener el estado de ejecución de todos los Flujos de Trabajo desplegados en un sitio de SharePoint 2013 OnPremises (para SharePoint Online, la idea sería la misma). El script, os lo podéis descargar desde la siguiente Url: How get the workflow execution status for all the workflows in a SharePoint Site

$host.Runspace.ThreadOptions = "ReuseThread"

 

#Definition of the function that gets the workflow execution status for all the workflows deployed to a SharePoint Site

function Get-SPSitesInSC

{

    param ($sSiteColUrl,$sUserName,$sDomain,$sPassword,$sCSOMPath)

    try

    {    

        Write-Host "-----------------------------------------------------------------------------------"  -foregroundcolor Green

        Write-Host "Getting the workflow execution status for all the workflows deployed in sSiteColUrl" -foregroundcolor Green

        Write-Host "-----------------------------------------------------------------------------------"  -foregroundcolor Green

     

        #Adding the Client OM Assemblies

        $sCSOMRuntimePath=$sCSOMPath +  "\Microsoft.SharePoint.Client.Runtime.dll"

        $sCSOMWorkflowPath=$sCSOMPath + "\Microsoft.SharePoint.Client.WorkflowServices.dll"

        $sCSOMPath=$sCSOMPath +  "\Microsoft.SharePoint.Client.dll"             

        Add-Type -Path $sCSOMPath         

        Add-Type -Path $sCSOMRuntimePath

        Add-Type -Path $sCSOMWorkflowPath

 

        #SharePoint Client Object Model Context

        $spCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl) 

        $spCredentials = New-Object System.Net.NetworkCredential($sUserName,$sPassword,$sDomain)  

        $spCtx.Credentials = $spCredentials 

 

        if (!$spCtx.ServerObjectIsNull.Value) 

        {

            $spWeb = $spCtx.Web

            $spLists = $spWeb.Lists

            $spCtx.Load($spLists);

            $spCtx.ExecuteQuery();

 

            $spWorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($spCtx, $spWeb);

            $spWorkflowSubscriptionService = $spWorkflowServicesManager.GetWorkflowSubscriptionService();

            $spWorkflowInstanceSevice = $spWorkflowServicesManager.GetWorkflowInstanceService();

            

            Write-Host ""

            Write-Host "Getting all the Lists in $sSiteColUrl" -ForegroundColor Green

            Write-Host ""

 

            foreach ($spList in $spLists)         

            {   

                $spWorkflowSubscriptions = $spWorkflowSubscriptionService.EnumerateSubscriptionsByList($spList.Id);

                $spCtx.Load($spWorkflowSubscriptions);                

                $spCtx.ExecuteQuery();                

                foreach($spWorkflowSubscription in $spWorkflowSubscriptions)

                {            

                    Write-Host "**************************************************************************************"

                    Write-Host "List: "$spList.Title " - Workflow: "$spWorkflowSubscription.Name -ForegroundColor Green

                    Write-Host "***************************************************************************************"

                    Write-Host ""

 

                    $spCamlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery

                    $spCamlQuery.ViewXml = "<View> <ViewFields><FieldRef Name='Title' /></ViewFields></View>";

                    $spListItems = $spList.GetItems($spCamlQuery);

                    $spCtx.Load($spListItems);

                    $spCtx.ExecuteQuery();

 

                    foreach($spListItem in $spListItems)

                    {

                        $spWorkflowInstanceCollection = $spWorkflowInstanceSevice.EnumerateInstancesForListItem($spList.Id,$spListItem.Id);

                        $spCtx.Load($spWorkflowInstanceCollection);

                        $spCtx.ExecuteQuery();

                        foreach ($spWorkflowInstance in $spWorkflowInstanceCollection)

                        {

                           Write-Host "List Item Title:"$spListItem["Title"] 

                           Write-Host "Workflow Status:"$spWorkflowInstance.Status 

                           Write-Host "Last Workflow Execution:"$spWorkflowInstance.LastUpdated

                           Write-Host ""

                        }

                    }                   

                    Write-Host ""

                }

            }

              

            $spCtx.Dispose() 

        }        

    }

    catch [System.Exception]

    {

        write-host -f red $_.Exception.ToString()   

    }    

}

 

#Required Parameters

$sSiteColUrl = "http://<SiteUrl>/" 

$sUserName = "<SharePointUser>" 

$sDomain="<OnPremisesDomain>"

$sPassword ="<Password>" 

$sCSOMPath="C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI"

 

Get-SPSitesInSC -sSiteColUrl $sSiteColUrl -sUserName $sUserName -sDomain $sDomain -sPassword $sPassword -sCSOMPath $sCSOMPath

La salida por pantalla que se genera es la siguiente:

image