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

Among the different possibilities provided by the Client Object Model in SharePoint 2013, we have the ability to interact with the Workflows deployed in a fram using the specific CSOM assembly that for SharePoint 2013 Service Pack 1 (SP1) is available in the path: C:\Program Files \Common Files\microsoft shared\web Server Extensions\16\ISAPI. Specifically, the assembly to be used is Microsoft.SharePoint.Client.WorkflowServices.dll. As an example of how to use CSOM for SharePoint 2013 Workflows, the following script allows to get the execution status of all Workflow deployed in a SharePoint 2013 OnPremises farm (for SharePoint Online, the idea is the same). The script can be downloaded from the following 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

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

image

And that’s all about how to use Workflows CSOM for SharePoint 2013 in PowerShell. In the next article I will show you how to do the same using .NET Code.

2 pensamientos en “SharePoint 2013: How to use the Workflows Client Object Model (I)!

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

  2. Pingback: SharePoint 2013: How to use the Workflows Client Object Model (II)! | 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 )

Google photo

Estás comentando usando tu cuenta de Google. 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 )

Conectando a %s