diff --git a/Commands/Base/PipeBinds/WorkflowInstancePipeBind.cs b/Commands/Base/PipeBinds/WorkflowInstancePipeBind.cs index e9e9096f0..e2fb69a6d 100644 --- a/Commands/Base/PipeBinds/WorkflowInstancePipeBind.cs +++ b/Commands/Base/PipeBinds/WorkflowInstancePipeBind.cs @@ -1,36 +1,34 @@ using System; + using Microsoft.SharePoint.Client.WorkflowServices; namespace PnP.PowerShell.Commands.Base.PipeBinds { public sealed class WorkflowInstancePipeBind { - private readonly WorkflowInstance _instance; - private readonly Guid _id; - public WorkflowInstancePipeBind() { - _instance = null; - _id = Guid.Empty; + Instance = null; + Id = Guid.Empty; } public WorkflowInstancePipeBind(WorkflowInstance instance) { - _instance = instance; + Instance = instance; } public WorkflowInstancePipeBind(Guid guid) { - _id = guid; + Id = guid; } public WorkflowInstancePipeBind(string id) { - _id = Guid.Parse(id); + Id = Guid.Parse(id); } - public Guid Id => _id; + public Guid Id { get; } - public WorkflowInstance Instance => _instance; + public WorkflowInstance Instance { get; } } } diff --git a/Commands/Workflows/GetWorkflowInstances.cs b/Commands/Workflows/GetWorkflowInstances.cs index ef8fb0a31..13b7e58d8 100644 --- a/Commands/Workflows/GetWorkflowInstances.cs +++ b/Commands/Workflows/GetWorkflowInstances.cs @@ -1,107 +1,162 @@ -using System; -using System.Management.Automation; -using Microsoft.SharePoint.Client; -using PnP.PowerShell.CmdletHelpAttributes; -using PnP.PowerShell.Commands.Base.PipeBinds; - -namespace PnP.PowerShell.Commands.Workflows -{ - [Cmdlet(VerbsCommon.Get, "PnPWorkflowInstance", DefaultParameterSetName = ParameterSet_BYLISTITEM)] - [CmdletHelp("Gets SharePoint 2010/2013 workflow instances", - DetailedDescription = "Gets all SharePoint 2010/2013 workflow instances", - Category = CmdletHelpCategory.Workflows, - SupportedPlatform = CmdletSupportedPlatform.All)] - [CmdletExample( - Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem $ListItem", - Remarks = @"Retrieves workflow instances running against the provided item on list ""My Library""", - SortOrder = 1)] - [CmdletExample( - Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem 2", - Remarks = @"Retrieves workflow instances running against the provided item with 2 in the list ""My Library""", - SortOrder = 2)] - [CmdletExample( - Code = @"PS:> Get-PnPWorkflowSubscription | Get-PnPWorkflowInstance", - Remarks = @"Retrieves workflow instances from all subscriptions", - SortOrder = 3)] - - public class GetWorkflowInstance : PnPWebCmdlet - { - private const string ParameterSet_BYLISTITEM = "By List and ListItem"; - private const string ParameterSet_BYSUBSCRIPTION = "By WorkflowSubscription"; - - protected override void ExecuteCmdlet() - { - switch (ParameterSetName) - { - case ParameterSet_BYLISTITEM: - ExecuteCmdletByListItem(); - break; - case ParameterSet_BYSUBSCRIPTION: - ExecuteCmdletBySubscription(); - break; - default: - throw new NotImplementedException($"{nameof(ParameterSetName)}: {ParameterSetName}"); - } - } - - [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List for which workflow instances should be retrieved", Position = 0)] - public ListPipeBind List; - - [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List Item for which workflow instances should be retrieved", Position = 1)] - public ListItemPipeBind ListItem; - - private void ExecuteCmdletByListItem() - { - List list = null; - ListItem listitem = null; - - if (List != null) - { - list = List.GetList(SelectedWeb); - if (list == null) - { - throw new PSArgumentException($"No list found with id, title or url '{List}'", nameof(List)); - } - } - else - { - throw new PSArgumentException("List required"); - } - - if (ListItem != null) - { - listitem = ListItem.GetListItem(list); - if (listitem == null) - { - throw new PSArgumentException($"No list item found with id, or title '{ListItem}'", nameof(ListItem)); - } - } - else - { - throw new PSArgumentException("List Item required"); - } - - var workflowServicesManager = new Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager(ClientContext, SelectedWeb); - var workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService(); - var workflows = workflowInstanceService.EnumerateInstancesForListItem(list.Id, listitem.Id); - ClientContext.Load(workflows); - ClientContext.ExecuteQueryRetry(); - WriteObject(workflows, true); - } - - [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTION, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0, ValueFromPipeline = true)] - public WorkflowSubscriptionPipeBind WorkflowSubscription; - - private void ExecuteCmdletBySubscription() - { - var workflowSubscription = WorkflowSubscription.GetWorkflowSubscription(SelectedWeb); - if (workflowSubscription == null) - { - throw new PSArgumentException($"No workflow subscription found for '{WorkflowSubscription}'", nameof(WorkflowSubscription)); - } - - var workflows = workflowSubscription.GetInstances(); - WriteObject(workflows, true); - } - } -} +using System; +using System.Management.Automation; + +using Microsoft.SharePoint.Client; +using Microsoft.SharePoint.Client.WorkflowServices; + +using PnP.PowerShell.CmdletHelpAttributes; +using PnP.PowerShell.Commands.Base.PipeBinds; + +namespace PnP.PowerShell.Commands.Workflows +{ + [Cmdlet(VerbsCommon.Get, "PnPWorkflowInstance", DefaultParameterSetName = ParameterSet_BYSITE)] + [CmdletHelp("Gets SharePoint 2010/2013 workflow instances", + DetailedDescription = "Gets all SharePoint 2010/2013 workflow instances", + Category = CmdletHelpCategory.Workflows, + OutputType = typeof(WorkflowInstance), + SupportedPlatform = CmdletSupportedPlatform.All)] + [CmdletExample( + Code = @"PS:> Get-PnPWorkflowInstance", + Remarks = @"Retrieves workflow instances for site workflows", + SortOrder = 1)] + [CmdletExample( + Code = @"PS:> $wfSubscriptions | Get-PnPWorkflowInstance", + Remarks = @"Retrieves workflow instance(s) for specified subscription(s).", + SortOrder = 2)] + [CmdletExample( + Code = @"PS:> Get-PnPWorkflowInstance ""ab77c32e-8b61-4fb4-bb41-be12193e9852""", + Remarks = @"Retrieves workflow instance by workflow instance ID.", + SortOrder = 4)] + [CmdletExample( + Code = @"PS:> Get-PnPWorkflowInstance $listItem", + Remarks = @"Retrieves workflow instances on the provided list item", + SortOrder = 5)] + [CmdletExample( + Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem 2", + Remarks = @"Retrieves workflow instances on the provided item with 2 in list ""My Library""", + SortOrder = 6)] + [CmdletExample( + Code = @"PS:> $listItems | Get-PnPWorkflowInstance", + Remarks = @"Retrieves workflow instances on the provided items", + SortOrder = 6)] + [CmdletExample( + Code = @"PS:> Get-PnPWorkflowInstance -WorkflowSubscription ""ab77c32e-8b61-4fb4-bb41-be12193e9852""", + Remarks = @"Retrieves workflow instances by workflow subscription ID", + SortOrder = 7)] + [CmdletExample( + Code = @"PS:> Get-PnPWorkflowSubscription | Get-PnPWorkflowInstance", + Remarks = @"Retrieves workflow instances from all subscriptions", + SortOrder = 8)] + + public class GetWorkflowInstance : PnPWebCmdlet + { + private const string ParameterSet_BYSITE = "By Site"; + private const string ParameterSet_BYGUID = "By GUID"; + private const string ParameterSet_BYLISTITEM = "By ListItem"; + private const string ParameterSet_BYLISTITEMOBJECT = "By ListItem object"; + private const string ParameterSet_BYSUBSCRIPTION = "By WorkflowSubscription"; + private const string ParameterSet_BYSUBSCRIPTIONOBJECT = "By WorkflowSubscription object"; + + protected override void ExecuteCmdlet() + { + switch (ParameterSetName) + { + case ParameterSet_BYSITE: + ExecuteCmdletBySite(); + break; + + case ParameterSet_BYLISTITEM: + case ParameterSet_BYLISTITEMOBJECT: + ExecuteCmdletByListItem(); + break; + case ParameterSet_BYSUBSCRIPTION: + case ParameterSet_BYSUBSCRIPTIONOBJECT: + ExecuteCmdletBySubscription(); + break; + + case ParameterSet_BYGUID: + ExecuteCmdletByIdentity(); + break; + default: + throw new NotImplementedException($"{nameof(ParameterSetName)}: {ParameterSetName}"); + } + } + + private void ExecuteCmdletBySite() + { + var instances = new WorkflowServicesManager(ClientContext, SelectedWeb) + .GetWorkflowInstanceService() + .EnumerateInstancesForSite(); + + ClientContext.Load(instances); + ClientContext.ExecuteQueryRetry(); + + WriteObject(instances, true); + } + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List for which workflow instances should be retrieved", Position = 0)] + public ListPipeBind List; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List Item for which workflow instances should be retrieved", Position = 1)] + public ListItemPipeBind ListItem; + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEMOBJECT, HelpMessage = "The ListItem for which workflow instances should be retrieved", Position = 0)] + public ListItem ListItemObject; + + private void ExecuteCmdletByListItem() + { + var list = ListItemObject?.ParentList + ?? List.GetList(SelectedWeb) + ?? throw new PSArgumentException($"No list found with id, title or url '{List}'", nameof(List)); + + var listId = list.EnsureProperty(x => x.Id); + + var listItem = ListItemObject + ?? ListItem.GetListItem(list) + ?? throw new PSArgumentException($"No list item found with id, or title '{ListItem}'", nameof(ListItem)); + + var listItemId = listItem.EnsureProperty(x => x.Id); + + var workflows = new WorkflowServicesManager(ClientContext, SelectedWeb) + .GetWorkflowInstanceService() + .EnumerateInstancesForListItem(listId, listItemId); + + ClientContext.Load(workflows); + ClientContext.ExecuteQueryRetry(); + WriteObject(workflows, true); + } + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTION, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0)] + public WorkflowSubscriptionPipeBind WorkflowSubscription; + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTIONOBJECT, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0, ValueFromPipeline = true)] + public WorkflowSubscription WorkflowSubscriptionObject; + + private void ExecuteCmdletBySubscription() + { + var workflowSubscription = WorkflowSubscriptionObject + ?? WorkflowSubscription.GetWorkflowSubscription(SelectedWeb) + ?? throw new PSArgumentException($"No workflow subscription found for '{WorkflowSubscription}'", nameof(WorkflowSubscription)); + + var workflows = workflowSubscription.GetInstances(); + WriteObject(workflows, true); + } + + [Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYGUID, HelpMessage = "The guid of the workflow instance to retrieved.", Position = 0, ValueFromPipeline = true, ValueFromRemainingArguments = true)] + public WorkflowInstancePipeBind Identity; + + private void ExecuteCmdletByIdentity() + { + var workflowInstanceId = Identity.Instance?.EnsureProperty(i => i.Id) + ?? Identity.Id; + + var instance = new WorkflowServicesManager(ClientContext, SelectedWeb) + .GetWorkflowInstanceService() + .GetInstance(workflowInstanceId); + + ClientContext.Load(instance); + ClientContext.ExecuteQueryRetry(); + + WriteObject(instance, true); + } + } +} diff --git a/Commands/Workflows/ResumeWorkflowInstance.cs b/Commands/Workflows/ResumeWorkflowInstance.cs index 0c0234149..a7581914a 100644 --- a/Commands/Workflows/ResumeWorkflowInstance.cs +++ b/Commands/Workflows/ResumeWorkflowInstance.cs @@ -1,7 +1,10 @@ using System; using System.Linq; using System.Management.Automation; + using Microsoft.SharePoint.Client; +using Microsoft.SharePoint.Client.WorkflowServices; + using PnP.PowerShell.CmdletHelpAttributes; using PnP.PowerShell.Commands.Base.PipeBinds; @@ -12,31 +15,33 @@ namespace PnP.PowerShell.Commands.Workflows "Resumes a previously stopped workflow instance", Category = CmdletHelpCategory.Workflows)] [CmdletExample( - Code = @"PS:> Resume-PnPWorkflowInstance -identity $wfInstance", - Remarks = "Resumes the workflow instance, this can be the Guid of the instance or the instance itself.", + Code = @"PS:> Resume-PnPWorkflowInstance ab77c32e-8b61-4fb4-bb41-be12193e9852", + Remarks = "Resumes the workflow instance, this can be a instance ID (Guid) or the instance itself.", SortOrder = 1)] + [CmdletExample( + Code = @"PS:> Resume-PnPWorkflowInstance -Identity ""ab77c32e-8b61-4fb4-bb41-be12193e9852""", + Remarks = "Resumes the workflow instance, this can be a instance ID (Guid) or the instance itself.", + SortOrder = 2)] + [CmdletExample( + Code = @"PS:> $wfInstances | Resume-PnPWorkflowInstance", + Remarks = "Resumes the workflow instance(s), either instance IDs or the instance objects", + SortOrder = 3)] public class ResumeWorkflowInstance : PnPWebCmdlet { - [Parameter(Mandatory = true, HelpMessage = "The instance to resume", Position = 0)] + [Parameter(Mandatory = true, HelpMessage = "The instance to resume", Position = 0, ValueFromPipeline = true)] public WorkflowInstancePipeBind Identity; protected override void ExecuteCmdlet() { - if (Identity.Instance != null) - { - Identity.Instance.ResumeWorkflow(); - } - else if (Identity.Id != Guid.Empty) - { - var allinstances = SelectedWeb.GetWorkflowInstances(); - foreach (var instance in allinstances.Where(instance => instance.Id == Identity.Id)) - { - instance.ResumeWorkflow(); - break; - } - } - } - } + var workflowInstanceService = new WorkflowServicesManager(ClientContext, SelectedWeb) + .GetWorkflowInstanceService(); + + var instance = Identity.Instance + ?? workflowInstanceService.GetInstance(Identity.Id); + workflowInstanceService.ResumeWorkflow(instance); + ClientContext.ExecuteQueryRetry(); + } + } } diff --git a/Commands/Workflows/StartWorkflowInstance.cs b/Commands/Workflows/StartWorkflowInstance.cs index 5970e9916..f253b9ae2 100644 --- a/Commands/Workflows/StartWorkflowInstance.cs +++ b/Commands/Workflows/StartWorkflowInstance.cs @@ -1,66 +1,115 @@ -using System.Management.Automation; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Management.Automation; + using Microsoft.SharePoint.Client; +using Microsoft.SharePoint.Client.WorkflowServices; + using PnP.PowerShell.CmdletHelpAttributes; using PnP.PowerShell.Commands.Base.PipeBinds; -using Microsoft.SharePoint.Client.WorkflowServices; -using System.Collections.Generic; namespace PnP.PowerShell.Commands.Workflows { [Cmdlet(VerbsLifecycle.Start, "PnPWorkflowInstance")] [CmdletHelp("Starts a SharePoint 2010/2013 workflow instance on a list item", DetailedDescription = "Allows starting a SharePoint 2010/2013 workflow on a list item in a list", + OutputType = typeof(Guid), + OutputTypeDescription = "Returns the GUID of the new workflow instance", Category = CmdletHelpCategory.Workflows, SupportedPlatform = CmdletSupportedPlatform.All)] + [CmdletExample( + Code = @"PS:> $wfSubscriptions | Start-PnPWorkflowInstance", + Remarks = "Starts a workflow instance of the specified subscription(s)", + SortOrder = 1)] + [CmdletExample( + Code = @"PS:> Get-PnPListItem -List MyList | Start-PnPWorkflowInstance -Subscription MyWorkflow", + Remarks = "Starts a MyWorkflow instance on each item in the MyList list", + SortOrder = 2)] [CmdletExample( Code = @"PS:> Start-PnPWorkflowInstance -Subscription 'WorkflowName' -ListItem $item", Remarks = "Starts a workflow instance on the specified list item", - SortOrder = 1)] + SortOrder = 3)] [CmdletExample( Code = @"PS:> Start-PnPWorkflowInstance -Subscription $subscription -ListItem 2", Remarks = "Starts a workflow instance on the specified list item", - SortOrder = 2)] + SortOrder = 4)] + [CmdletExample( + Code = @"PS:> Start-PnPWorkflowInstance ""MyWorkflow"" -Initiator 5", + Remarks = "Starts a workflow instance as a specified user ID", + SortOrder = 5)] + [CmdletExample( + Code = @"PS:> Start-PnPWorkflowInstance ""MyWorkflow"" -Initiator ""Jenny.Smith@contoso.com""", + Remarks = "Starts a workflow instance as a specified user", + SortOrder = 6)] + [CmdletExample( + Code = @"PS:> Start-PnPWorkflowInstance ""MyWorkflow"" -InitiationParameters @{ MyParameter = ""Hello""; SecondParameter = 100 }", + Remarks = "Starts a workflow instance specifiying initiation parameters", + SortOrder = 7)] public class StartWorkflowInstance : PnPWebCmdlet { - [Parameter(Mandatory = true, HelpMessage = "The workflow subscription to start", Position = 0)] + [Parameter(Mandatory = true, HelpMessage = "The workflow subscription to start", Position = 0, ValueFromPipeline = true)] public WorkflowSubscriptionPipeBind Subscription; - [Parameter(Mandatory = true, HelpMessage = "The list item to start the workflow against", Position = 1)] + [Parameter(Mandatory = false, HelpMessage = "The list item or list item id to start the workflow on", Position = 1)] public ListItemPipeBind ListItem; + [Parameter(Mandatory = false, HelpMessage = "The list item to start the workflow on", ValueFromPipeline = true)] + public ListItem ListItemObject; + + [Parameter(Mandatory = false, HelpMessage = "The user who initiated the workflow")] + public UserPipeBind Initiator; + + [Parameter(Mandatory = false, HelpMessage = "Initiation properties are external variables whose values are set when the workflow is initiated.")] + public Hashtable InitiationParameters; + protected override void ExecuteCmdlet() { - int ListItemID; - if (ListItem != null) - { - if (ListItem.Id != uint.MinValue) - { - ListItemID = (int)ListItem.Id; - } - else if (ListItem.Item != null) - { - ListItemID = ListItem.Item.Id; - } - else - { - throw new PSArgumentException("No valid list item specified.", nameof(ListItem)); - } - } - else + var subscription = Subscription.GetWorkflowSubscription(SelectedWeb) + ?? throw new PSArgumentException($"No workflow subscription found for '{Subscription}'", nameof(Subscription)); + + // not much info available about theses + // https://docs.microsoft.com/en-us/sharepoint/dev/general-development/workflow-initiation-and-configuration-properties#initiation-properties + var inputParameters = InitiationParameters?.Cast()?.ToDictionary(e => e.Key.ToString(), e => e.Value) + ?? new Dictionary(); + + if (Initiator is object) { - throw new PSArgumentException("List Item is required", nameof(ListItem)); + var initiatorUser = Initiator.User is object ? Initiator.User + : Initiator.Login is object ? SelectedWeb.EnsureUser(Initiator.Login) // this is really any string the user enters + : SelectedWeb.SiteUsers.GetById(Initiator.Id); + + var initiatorLoginName = initiatorUser.EnsureProperty(u => u.LoginName); + + // yes it's called InitiatorUserId but it actually wants a login name (claims format) + // maybe 2010 workflows are different? + inputParameters.Add("Microsoft.SharePoint.ActivationProperties.InitiatorUserId", initiatorLoginName); } - var subscription = Subscription.GetWorkflowSubscription(SelectedWeb) - ?? throw new PSArgumentException($"No workflow subscription found for '{Subscription}'", nameof(Subscription)); + var instanceService = new WorkflowServicesManager(ClientContext, SelectedWeb) + .GetWorkflowInstanceService(); - var inputParameters = new Dictionary(); + ClientResult instanceResult; - WorkflowServicesManager workflowServicesManager = new WorkflowServicesManager(ClientContext, SelectedWeb); - WorkflowInstanceService instanceService = workflowServicesManager.GetWorkflowInstanceService(); + if (ListItem is object) + { + int listItemID = ListItem?.Item?.EnsureProperty(li => li.Id) ?? (int)ListItem?.Id; + instanceResult = instanceService.StartWorkflowOnListItem(subscription, listItemID, inputParameters); + } + else if (ListItemObject is object) + { + int listItemID = ListItemObject.EnsureProperty(li => li.Id); + instanceResult = instanceService.StartWorkflowOnListItem(subscription, listItemID, inputParameters); + } + else + { + instanceResult = instanceService.StartWorkflow(subscription, inputParameters); + } - instanceService.StartWorkflowOnListItem(subscription, ListItemID, inputParameters); ClientContext.ExecuteQueryRetry(); + + WriteObject(instanceResult.Value, true); } } } diff --git a/Commands/Workflows/StopWorkflowInstance.cs b/Commands/Workflows/StopWorkflowInstance.cs index 11a561857..2d455d12a 100644 --- a/Commands/Workflows/StopWorkflowInstance.cs +++ b/Commands/Workflows/StopWorkflowInstance.cs @@ -1,11 +1,12 @@ using System; using System.Linq; using System.Management.Automation; + using Microsoft.SharePoint.Client; +using Microsoft.SharePoint.Client.WorkflowServices; + using PnP.PowerShell.CmdletHelpAttributes; using PnP.PowerShell.Commands.Base.PipeBinds; -using Microsoft.SharePoint.Client.Workflow; -using Microsoft.SharePoint.Client.WorkflowServices; namespace PnP.PowerShell.Commands.Workflows { @@ -13,58 +14,43 @@ namespace PnP.PowerShell.Commands.Workflows [CmdletHelp("Stops a workflow instance", Category = CmdletHelpCategory.Workflows)] [CmdletExample( - Code = @"PS:> Stop-PnPWorkflowInstance -identity $wfInstance", - Remarks = "Stops the workflow Instance", + Code = @"PS:> Stop-PnPWorkflowInstance -Identity $wfInstance", + Remarks = "Stops the workflow instance", SortOrder = 1)] + [CmdletExample( + Code = @"PS:> $wfInstances | Stop-PnPWorkflowInstance -Force", + Remarks = "Terminates the workflow instance(s)", + SortOrder = 2)] public class StopWorkflowInstance : PnPWebCmdlet { - [Parameter(Mandatory = true, HelpMessage = "The instance to stop", Position = 0)] + [Parameter(Mandatory = true, HelpMessage = "The instance to stop", Position = 0, ValueFromPipeline = true)] public WorkflowInstancePipeBind Identity; - + //Cancel vs Terminate: https://support.office.com/en-us/article/Cancel-a-workflow-in-progress-096b7d2d-9b8d-48f1-a002-e98bb86bdc7f - [Parameter(Mandatory = false, HelpMessage = "Forcefully terminate the workflow instead of cancelling. Works on errored and non-responsive workflows. Deletes all created tasks. Does not notify participants.")] + [Parameter(Mandatory = false, HelpMessage = "Forcefully terminate the workflow instead of cancelling. Works on errored and non-responsive workflows. Does not notify participants.")] public SwitchParameter Force; protected override void ExecuteCmdlet() { + var instanceService = new WorkflowServicesManager(ClientContext, SelectedWeb) + .GetWorkflowInstanceService(); - WorkflowServicesManager workflowServicesManager = new WorkflowServicesManager(ClientContext, SelectedWeb); - InteropService interopService = workflowServicesManager.GetWorkflowInteropService(); - WorkflowInstanceService instanceService = workflowServicesManager.GetWorkflowInstanceService(); + var instance = Identity.Instance + ?? instanceService.GetInstance(Identity.Id); - if (Identity.Instance != null) + var instanceId = Identity.Instance?.Id ?? Identity.Id; + if (Force) { - WriteVerbose("Instance object set"); - WriteVerbose("Cancelling workflow with ID: " + Identity.Instance.Id); - if(Force) - { - instanceService.TerminateWorkflow(Identity.Instance); - } - else - { - Identity.Instance.CancelWorkFlow(); - } - ClientContext.ExecuteQueryRetry(); + WriteVerbose("Terminating workflow with ID: " + instanceId); + instanceService.TerminateWorkflow(instance); } - else if (Identity.Id != Guid.Empty) + else { - WriteVerbose("Instance object not set. Looking up site workflows by GUID: " + Identity.Id); - var allinstances = SelectedWeb.GetWorkflowInstances(); - foreach (var instance in allinstances.Where(instance => instance.Id == Identity.Id)) - { - WriteVerbose("Cancelling workflow with ID: " + Identity.Instance.Id); - if (Force) - { - instanceService.TerminateWorkflow(instance); - } - else - { - instance.CancelWorkFlow(); - } - ClientContext.ExecuteQueryRetry(); - break; - } + WriteVerbose("Cancelling workflow with ID: " + instanceId); + instanceService.CancelWorkflow(instance); } + + ClientContext.ExecuteQueryRetryAsync(); } } }