Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 111e765

Browse files
James MayJames May
James May
authored and
James May
committedAug 26, 2020
WorkflowInstance cmdlets improvements
Get-WorkflowInstance: * add OutputType annotation * add getting site workflow instances * add get by instance guid * positional and piped list item and subscription parameters * get by list item object without specifying a list pipebind * added extra examples Resume-WorkflowInstance: * improved implementation retrieve just the requested instance instead of all * added examples Start-WorkflowInstance: * return the guid of the started instance * added extra examples * made list item optional, allowing start of site workflows * positional and piped subscription and list item parameter * added Initiator and InitiationParameters parameters Stop:-WorkflowInstance: * added extra example * positional and piped instance parameter * verbose message now indicates cancel vs terminate
1 parent 172d641 commit 111e765

5 files changed

+298
-205
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
11
using System;
2+
23
using Microsoft.SharePoint.Client.WorkflowServices;
34

45
namespace PnP.PowerShell.Commands.Base.PipeBinds
56
{
67
public sealed class WorkflowInstancePipeBind
78
{
8-
private readonly WorkflowInstance _instance;
9-
private readonly Guid _id;
10-
119
public WorkflowInstancePipeBind()
1210
{
13-
_instance = null;
14-
_id = Guid.Empty;
11+
Instance = null;
12+
Id = Guid.Empty;
1513
}
1614

1715
public WorkflowInstancePipeBind(WorkflowInstance instance)
1816
{
19-
_instance = instance;
17+
Instance = instance;
2018
}
2119

2220
public WorkflowInstancePipeBind(Guid guid)
2321
{
24-
_id = guid;
22+
Id = guid;
2523
}
2624

2725
public WorkflowInstancePipeBind(string id)
2826
{
29-
_id = Guid.Parse(id);
27+
Id = Guid.Parse(id);
3028
}
3129

32-
public Guid Id => _id;
30+
public Guid Id { get; }
3331

34-
public WorkflowInstance Instance => _instance;
32+
public WorkflowInstance Instance { get; }
3533
}
3634
}
+162-107
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,162 @@
1-
using System;
2-
using System.Management.Automation;
3-
using Microsoft.SharePoint.Client;
4-
using PnP.PowerShell.CmdletHelpAttributes;
5-
using PnP.PowerShell.Commands.Base.PipeBinds;
6-
7-
namespace PnP.PowerShell.Commands.Workflows
8-
{
9-
[Cmdlet(VerbsCommon.Get, "PnPWorkflowInstance", DefaultParameterSetName = ParameterSet_BYLISTITEM)]
10-
[CmdletHelp("Gets SharePoint 2010/2013 workflow instances",
11-
DetailedDescription = "Gets all SharePoint 2010/2013 workflow instances",
12-
Category = CmdletHelpCategory.Workflows,
13-
SupportedPlatform = CmdletSupportedPlatform.All)]
14-
[CmdletExample(
15-
Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem $ListItem",
16-
Remarks = @"Retrieves workflow instances running against the provided item on list ""My Library""",
17-
SortOrder = 1)]
18-
[CmdletExample(
19-
Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem 2",
20-
Remarks = @"Retrieves workflow instances running against the provided item with 2 in the list ""My Library""",
21-
SortOrder = 2)]
22-
[CmdletExample(
23-
Code = @"PS:> Get-PnPWorkflowSubscription | Get-PnPWorkflowInstance",
24-
Remarks = @"Retrieves workflow instances from all subscriptions",
25-
SortOrder = 3)]
26-
27-
public class GetWorkflowInstance : PnPWebCmdlet
28-
{
29-
private const string ParameterSet_BYLISTITEM = "By List and ListItem";
30-
private const string ParameterSet_BYSUBSCRIPTION = "By WorkflowSubscription";
31-
32-
protected override void ExecuteCmdlet()
33-
{
34-
switch (ParameterSetName)
35-
{
36-
case ParameterSet_BYLISTITEM:
37-
ExecuteCmdletByListItem();
38-
break;
39-
case ParameterSet_BYSUBSCRIPTION:
40-
ExecuteCmdletBySubscription();
41-
break;
42-
default:
43-
throw new NotImplementedException($"{nameof(ParameterSetName)}: {ParameterSetName}");
44-
}
45-
}
46-
47-
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List for which workflow instances should be retrieved", Position = 0)]
48-
public ListPipeBind List;
49-
50-
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List Item for which workflow instances should be retrieved", Position = 1)]
51-
public ListItemPipeBind ListItem;
52-
53-
private void ExecuteCmdletByListItem()
54-
{
55-
List list = null;
56-
ListItem listitem = null;
57-
58-
if (List != null)
59-
{
60-
list = List.GetList(SelectedWeb);
61-
if (list == null)
62-
{
63-
throw new PSArgumentException($"No list found with id, title or url '{List}'", nameof(List));
64-
}
65-
}
66-
else
67-
{
68-
throw new PSArgumentException("List required");
69-
}
70-
71-
if (ListItem != null)
72-
{
73-
listitem = ListItem.GetListItem(list);
74-
if (listitem == null)
75-
{
76-
throw new PSArgumentException($"No list item found with id, or title '{ListItem}'", nameof(ListItem));
77-
}
78-
}
79-
else
80-
{
81-
throw new PSArgumentException("List Item required");
82-
}
83-
84-
var workflowServicesManager = new Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager(ClientContext, SelectedWeb);
85-
var workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService();
86-
var workflows = workflowInstanceService.EnumerateInstancesForListItem(list.Id, listitem.Id);
87-
ClientContext.Load(workflows);
88-
ClientContext.ExecuteQueryRetry();
89-
WriteObject(workflows, true);
90-
}
91-
92-
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTION, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0, ValueFromPipeline = true)]
93-
public WorkflowSubscriptionPipeBind WorkflowSubscription;
94-
95-
private void ExecuteCmdletBySubscription()
96-
{
97-
var workflowSubscription = WorkflowSubscription.GetWorkflowSubscription(SelectedWeb);
98-
if (workflowSubscription == null)
99-
{
100-
throw new PSArgumentException($"No workflow subscription found for '{WorkflowSubscription}'", nameof(WorkflowSubscription));
101-
}
102-
103-
var workflows = workflowSubscription.GetInstances();
104-
WriteObject(workflows, true);
105-
}
106-
}
107-
}
1+
using System;
2+
using System.Management.Automation;
3+
4+
using Microsoft.SharePoint.Client;
5+
using Microsoft.SharePoint.Client.WorkflowServices;
6+
7+
using PnP.PowerShell.CmdletHelpAttributes;
8+
using PnP.PowerShell.Commands.Base.PipeBinds;
9+
10+
namespace PnP.PowerShell.Commands.Workflows
11+
{
12+
[Cmdlet(VerbsCommon.Get, "PnPWorkflowInstance", DefaultParameterSetName = ParameterSet_BYSITE)]
13+
[CmdletHelp("Gets SharePoint 2010/2013 workflow instances",
14+
DetailedDescription = "Gets all SharePoint 2010/2013 workflow instances",
15+
Category = CmdletHelpCategory.Workflows,
16+
OutputType = typeof(WorkflowInstance),
17+
SupportedPlatform = CmdletSupportedPlatform.All)]
18+
[CmdletExample(
19+
Code = @"PS:> Get-PnPWorkflowInstance",
20+
Remarks = @"Retrieves workflow instances for site workflows",
21+
SortOrder = 1)]
22+
[CmdletExample(
23+
Code = @"PS:> $wfSubscriptions | Get-PnPWorkflowInstance",
24+
Remarks = @"Retrieves workflow instance(s) for specified subscription(s).",
25+
SortOrder = 2)]
26+
[CmdletExample(
27+
Code = @"PS:> Get-PnPWorkflowInstance ""ab77c32e-8b61-4fb4-bb41-be12193e9852""",
28+
Remarks = @"Retrieves workflow instance by workflow instance ID.",
29+
SortOrder = 4)]
30+
[CmdletExample(
31+
Code = @"PS:> Get-PnPWorkflowInstance $listItem",
32+
Remarks = @"Retrieves workflow instances on the provided list item",
33+
SortOrder = 5)]
34+
[CmdletExample(
35+
Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem 2",
36+
Remarks = @"Retrieves workflow instances on the provided item with 2 in list ""My Library""",
37+
SortOrder = 6)]
38+
[CmdletExample(
39+
Code = @"PS:> $listItems | Get-PnPWorkflowInstance",
40+
Remarks = @"Retrieves workflow instances on the provided items",
41+
SortOrder = 6)]
42+
[CmdletExample(
43+
Code = @"PS:> Get-PnPWorkflowInstance -WorkflowSubscription ""ab77c32e-8b61-4fb4-bb41-be12193e9852""",
44+
Remarks = @"Retrieves workflow instances by workflow subscription ID",
45+
SortOrder = 7)]
46+
[CmdletExample(
47+
Code = @"PS:> Get-PnPWorkflowSubscription | Get-PnPWorkflowInstance",
48+
Remarks = @"Retrieves workflow instances from all subscriptions",
49+
SortOrder = 8)]
50+
51+
public class GetWorkflowInstance : PnPWebCmdlet
52+
{
53+
private const string ParameterSet_BYSITE = "By Site";
54+
private const string ParameterSet_BYGUID = "By GUID";
55+
private const string ParameterSet_BYLISTITEM = "By ListItem";
56+
private const string ParameterSet_BYLISTITEMOBJECT = "By ListItem object";
57+
private const string ParameterSet_BYSUBSCRIPTION = "By WorkflowSubscription";
58+
private const string ParameterSet_BYSUBSCRIPTIONOBJECT = "By WorkflowSubscription object";
59+
60+
protected override void ExecuteCmdlet()
61+
{
62+
switch (ParameterSetName)
63+
{
64+
case ParameterSet_BYSITE:
65+
ExecuteCmdletBySite();
66+
break;
67+
68+
case ParameterSet_BYLISTITEM:
69+
case ParameterSet_BYLISTITEMOBJECT:
70+
ExecuteCmdletByListItem();
71+
break;
72+
case ParameterSet_BYSUBSCRIPTION:
73+
case ParameterSet_BYSUBSCRIPTIONOBJECT:
74+
ExecuteCmdletBySubscription();
75+
break;
76+
77+
case ParameterSet_BYGUID:
78+
ExecuteCmdletByIdentity();
79+
break;
80+
default:
81+
throw new NotImplementedException($"{nameof(ParameterSetName)}: {ParameterSetName}");
82+
}
83+
}
84+
85+
private void ExecuteCmdletBySite()
86+
{
87+
var instances = new WorkflowServicesManager(ClientContext, SelectedWeb)
88+
.GetWorkflowInstanceService()
89+
.EnumerateInstancesForSite();
90+
91+
ClientContext.Load(instances);
92+
ClientContext.ExecuteQueryRetry();
93+
94+
WriteObject(instances, true);
95+
}
96+
97+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List for which workflow instances should be retrieved", Position = 0)]
98+
public ListPipeBind List;
99+
100+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List Item for which workflow instances should be retrieved", Position = 1)]
101+
public ListItemPipeBind ListItem;
102+
103+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEMOBJECT, HelpMessage = "The ListItem for which workflow instances should be retrieved", Position = 0)]
104+
public ListItem ListItemObject;
105+
106+
private void ExecuteCmdletByListItem()
107+
{
108+
var list = ListItemObject?.ParentList
109+
?? List.GetList(SelectedWeb)
110+
?? throw new PSArgumentException($"No list found with id, title or url '{List}'", nameof(List));
111+
112+
var listId = list.EnsureProperty(x => x.Id);
113+
114+
var listItem = ListItemObject
115+
?? ListItem.GetListItem(list)
116+
?? throw new PSArgumentException($"No list item found with id, or title '{ListItem}'", nameof(ListItem));
117+
118+
var listItemId = listItem.EnsureProperty(x => x.Id);
119+
120+
var workflows = new WorkflowServicesManager(ClientContext, SelectedWeb)
121+
.GetWorkflowInstanceService()
122+
.EnumerateInstancesForListItem(listId, listItemId);
123+
124+
ClientContext.Load(workflows);
125+
ClientContext.ExecuteQueryRetry();
126+
WriteObject(workflows, true);
127+
}
128+
129+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTION, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0)]
130+
public WorkflowSubscriptionPipeBind WorkflowSubscription;
131+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTIONOBJECT, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0, ValueFromPipeline = true)]
132+
public WorkflowSubscription WorkflowSubscriptionObject;
133+
134+
private void ExecuteCmdletBySubscription()
135+
{
136+
var workflowSubscription = WorkflowSubscriptionObject
137+
?? WorkflowSubscription.GetWorkflowSubscription(SelectedWeb)
138+
?? throw new PSArgumentException($"No workflow subscription found for '{WorkflowSubscription}'", nameof(WorkflowSubscription));
139+
140+
var workflows = workflowSubscription.GetInstances();
141+
WriteObject(workflows, true);
142+
}
143+
144+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYGUID, HelpMessage = "The guid of the workflow instance to retrieved.", Position = 0, ValueFromPipeline = true, ValueFromRemainingArguments = true)]
145+
public WorkflowInstancePipeBind Identity;
146+
147+
private void ExecuteCmdletByIdentity()
148+
{
149+
var workflowInstanceId = Identity.Instance?.EnsureProperty(i => i.Id)
150+
?? Identity.Id;
151+
152+
var instance = new WorkflowServicesManager(ClientContext, SelectedWeb)
153+
.GetWorkflowInstanceService()
154+
.GetInstance(workflowInstanceId);
155+
156+
ClientContext.Load(instance);
157+
ClientContext.ExecuteQueryRetry();
158+
159+
WriteObject(instance, true);
160+
}
161+
}
162+
}
+23-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
22
using System.Linq;
33
using System.Management.Automation;
4+
45
using Microsoft.SharePoint.Client;
6+
using Microsoft.SharePoint.Client.WorkflowServices;
7+
58
using PnP.PowerShell.CmdletHelpAttributes;
69
using PnP.PowerShell.Commands.Base.PipeBinds;
710

@@ -12,31 +15,33 @@ namespace PnP.PowerShell.Commands.Workflows
1215
"Resumes a previously stopped workflow instance",
1316
Category = CmdletHelpCategory.Workflows)]
1417
[CmdletExample(
15-
Code = @"PS:> Resume-PnPWorkflowInstance -identity $wfInstance",
16-
Remarks = "Resumes the workflow instance, this can be the Guid of the instance or the instance itself.",
18+
Code = @"PS:> Resume-PnPWorkflowInstance ab77c32e-8b61-4fb4-bb41-be12193e9852",
19+
Remarks = "Resumes the workflow instance, this can be a instance ID (Guid) or the instance itself.",
1720
SortOrder = 1)]
21+
[CmdletExample(
22+
Code = @"PS:> Resume-PnPWorkflowInstance -Identity ""ab77c32e-8b61-4fb4-bb41-be12193e9852""",
23+
Remarks = "Resumes the workflow instance, this can be a instance ID (Guid) or the instance itself.",
24+
SortOrder = 2)]
25+
[CmdletExample(
26+
Code = @"PS:> $wfInstances | Resume-PnPWorkflowInstance",
27+
Remarks = "Resumes the workflow instance(s), either instance IDs or the instance objects",
28+
SortOrder = 3)]
1829
public class ResumeWorkflowInstance : PnPWebCmdlet
1930
{
20-
[Parameter(Mandatory = true, HelpMessage = "The instance to resume", Position = 0)]
31+
[Parameter(Mandatory = true, HelpMessage = "The instance to resume", Position = 0, ValueFromPipeline = true)]
2132
public WorkflowInstancePipeBind Identity;
2233

2334
protected override void ExecuteCmdlet()
2435
{
25-
if (Identity.Instance != null)
26-
{
27-
Identity.Instance.ResumeWorkflow();
28-
}
29-
else if (Identity.Id != Guid.Empty)
30-
{
31-
var allinstances = SelectedWeb.GetWorkflowInstances();
32-
foreach (var instance in allinstances.Where(instance => instance.Id == Identity.Id))
33-
{
34-
instance.ResumeWorkflow();
35-
break;
36-
}
37-
}
38-
}
39-
}
36+
var workflowInstanceService = new WorkflowServicesManager(ClientContext, SelectedWeb)
37+
.GetWorkflowInstanceService();
38+
39+
var instance = Identity.Instance
40+
?? workflowInstanceService.GetInstance(Identity.Id);
4041

42+
workflowInstanceService.ResumeWorkflow(instance);
4143

44+
ClientContext.ExecuteQueryRetry();
45+
}
46+
}
4247
}
+80-31
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,115 @@
1-
using System.Management.Automation;
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Management.Automation;
6+
27
using Microsoft.SharePoint.Client;
8+
using Microsoft.SharePoint.Client.WorkflowServices;
9+
310
using PnP.PowerShell.CmdletHelpAttributes;
411
using PnP.PowerShell.Commands.Base.PipeBinds;
5-
using Microsoft.SharePoint.Client.WorkflowServices;
6-
using System.Collections.Generic;
712

813
namespace PnP.PowerShell.Commands.Workflows
914
{
1015
[Cmdlet(VerbsLifecycle.Start, "PnPWorkflowInstance")]
1116
[CmdletHelp("Starts a SharePoint 2010/2013 workflow instance on a list item",
1217
DetailedDescription = "Allows starting a SharePoint 2010/2013 workflow on a list item in a list",
18+
OutputType = typeof(Guid),
19+
OutputTypeDescription = "Returns the GUID of the new workflow instance",
1320
Category = CmdletHelpCategory.Workflows,
1421
SupportedPlatform = CmdletSupportedPlatform.All)]
22+
[CmdletExample(
23+
Code = @"PS:> $wfSubscriptions | Start-PnPWorkflowInstance",
24+
Remarks = "Starts a workflow instance of the specified subscription(s)",
25+
SortOrder = 1)]
26+
[CmdletExample(
27+
Code = @"PS:> Get-PnPListItem -List MyList | Start-PnPWorkflowInstance -Subscription MyWorkflow",
28+
Remarks = "Starts a MyWorkflow instance on each item in the MyList list",
29+
SortOrder = 2)]
1530
[CmdletExample(
1631
Code = @"PS:> Start-PnPWorkflowInstance -Subscription 'WorkflowName' -ListItem $item",
1732
Remarks = "Starts a workflow instance on the specified list item",
18-
SortOrder = 1)]
33+
SortOrder = 3)]
1934
[CmdletExample(
2035
Code = @"PS:> Start-PnPWorkflowInstance -Subscription $subscription -ListItem 2",
2136
Remarks = "Starts a workflow instance on the specified list item",
22-
SortOrder = 2)]
37+
SortOrder = 4)]
38+
[CmdletExample(
39+
Code = @"PS:> Start-PnPWorkflowInstance ""MyWorkflow"" -Initiator 5",
40+
Remarks = "Starts a workflow instance as a specified user ID",
41+
SortOrder = 5)]
42+
[CmdletExample(
43+
Code = @"PS:> Start-PnPWorkflowInstance ""MyWorkflow"" -Initiator ""Jenny.Smith@contoso.com""",
44+
Remarks = "Starts a workflow instance as a specified user",
45+
SortOrder = 6)]
46+
[CmdletExample(
47+
Code = @"PS:> Start-PnPWorkflowInstance ""MyWorkflow"" -InitiationParameters @{ MyParameter = ""Hello""; SecondParameter = 100 }",
48+
Remarks = "Starts a workflow instance specifiying initiation parameters",
49+
SortOrder = 7)]
2350
public class StartWorkflowInstance : PnPWebCmdlet
2451
{
25-
[Parameter(Mandatory = true, HelpMessage = "The workflow subscription to start", Position = 0)]
52+
[Parameter(Mandatory = true, HelpMessage = "The workflow subscription to start", Position = 0, ValueFromPipeline = true)]
2653
public WorkflowSubscriptionPipeBind Subscription;
2754

28-
[Parameter(Mandatory = true, HelpMessage = "The list item to start the workflow against", Position = 1)]
55+
[Parameter(Mandatory = false, HelpMessage = "The list item or list item id to start the workflow on", Position = 1)]
2956
public ListItemPipeBind ListItem;
3057

58+
[Parameter(Mandatory = false, HelpMessage = "The list item to start the workflow on", ValueFromPipeline = true)]
59+
public ListItem ListItemObject;
60+
61+
[Parameter(Mandatory = false, HelpMessage = "The user who initiated the workflow")]
62+
public UserPipeBind Initiator;
63+
64+
[Parameter(Mandatory = false, HelpMessage = "Initiation properties are external variables whose values are set when the workflow is initiated.")]
65+
public Hashtable InitiationParameters;
66+
3167
protected override void ExecuteCmdlet()
3268
{
33-
int ListItemID;
34-
if (ListItem != null)
35-
{
36-
if (ListItem.Id != uint.MinValue)
37-
{
38-
ListItemID = (int)ListItem.Id;
39-
}
40-
else if (ListItem.Item != null)
41-
{
42-
ListItemID = ListItem.Item.Id;
43-
}
44-
else
45-
{
46-
throw new PSArgumentException("No valid list item specified.", nameof(ListItem));
47-
}
48-
}
49-
else
69+
var subscription = Subscription.GetWorkflowSubscription(SelectedWeb)
70+
?? throw new PSArgumentException($"No workflow subscription found for '{Subscription}'", nameof(Subscription));
71+
72+
// not much info available about theses
73+
// https://docs.microsoft.com/en-us/sharepoint/dev/general-development/workflow-initiation-and-configuration-properties#initiation-properties
74+
var inputParameters = InitiationParameters?.Cast<DictionaryEntry>()?.ToDictionary(e => e.Key.ToString(), e => e.Value)
75+
?? new Dictionary<string, object>();
76+
77+
if (Initiator is object)
5078
{
51-
throw new PSArgumentException("List Item is required", nameof(ListItem));
79+
var initiatorUser = Initiator.User is object ? Initiator.User
80+
: Initiator.Login is object ? SelectedWeb.EnsureUser(Initiator.Login) // this is really any string the user enters
81+
: SelectedWeb.SiteUsers.GetById(Initiator.Id);
82+
83+
var initiatorLoginName = initiatorUser.EnsureProperty(u => u.LoginName);
84+
85+
// yes it's called InitiatorUserId but it actually wants a login name (claims format)
86+
// maybe 2010 workflows are different?
87+
inputParameters.Add("Microsoft.SharePoint.ActivationProperties.InitiatorUserId", initiatorLoginName);
5288
}
5389

54-
var subscription = Subscription.GetWorkflowSubscription(SelectedWeb)
55-
?? throw new PSArgumentException($"No workflow subscription found for '{Subscription}'", nameof(Subscription));
90+
var instanceService = new WorkflowServicesManager(ClientContext, SelectedWeb)
91+
.GetWorkflowInstanceService();
5692

57-
var inputParameters = new Dictionary<string, object>();
93+
ClientResult<Guid> instanceResult;
5894

59-
WorkflowServicesManager workflowServicesManager = new WorkflowServicesManager(ClientContext, SelectedWeb);
60-
WorkflowInstanceService instanceService = workflowServicesManager.GetWorkflowInstanceService();
95+
if (ListItem is object)
96+
{
97+
int listItemID = ListItem?.Item?.EnsureProperty(li => li.Id) ?? (int)ListItem?.Id;
98+
instanceResult = instanceService.StartWorkflowOnListItem(subscription, listItemID, inputParameters);
99+
}
100+
else if (ListItemObject is object)
101+
{
102+
int listItemID = ListItemObject.EnsureProperty(li => li.Id);
103+
instanceResult = instanceService.StartWorkflowOnListItem(subscription, listItemID, inputParameters);
104+
}
105+
else
106+
{
107+
instanceResult = instanceService.StartWorkflow(subscription, inputParameters);
108+
}
61109

62-
instanceService.StartWorkflowOnListItem(subscription, ListItemID, inputParameters);
63110
ClientContext.ExecuteQueryRetry();
111+
112+
WriteObject(instanceResult.Value, true);
64113
}
65114
}
66115
}
+25-39
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,56 @@
11
using System;
22
using System.Linq;
33
using System.Management.Automation;
4+
45
using Microsoft.SharePoint.Client;
6+
using Microsoft.SharePoint.Client.WorkflowServices;
7+
58
using PnP.PowerShell.CmdletHelpAttributes;
69
using PnP.PowerShell.Commands.Base.PipeBinds;
7-
using Microsoft.SharePoint.Client.Workflow;
8-
using Microsoft.SharePoint.Client.WorkflowServices;
910

1011
namespace PnP.PowerShell.Commands.Workflows
1112
{
1213
[Cmdlet(VerbsLifecycle.Stop, "PnPWorkflowInstance")]
1314
[CmdletHelp("Stops a workflow instance",
1415
Category = CmdletHelpCategory.Workflows)]
1516
[CmdletExample(
16-
Code = @"PS:> Stop-PnPWorkflowInstance -identity $wfInstance",
17-
Remarks = "Stops the workflow Instance",
17+
Code = @"PS:> Stop-PnPWorkflowInstance -Identity $wfInstance",
18+
Remarks = "Stops the workflow instance",
1819
SortOrder = 1)]
20+
[CmdletExample(
21+
Code = @"PS:> $wfInstances | Stop-PnPWorkflowInstance -Force",
22+
Remarks = "Terminates the workflow instance(s)",
23+
SortOrder = 2)]
1924
public class StopWorkflowInstance : PnPWebCmdlet
2025
{
21-
[Parameter(Mandatory = true, HelpMessage = "The instance to stop", Position = 0)]
26+
[Parameter(Mandatory = true, HelpMessage = "The instance to stop", Position = 0, ValueFromPipeline = true)]
2227
public WorkflowInstancePipeBind Identity;
23-
28+
2429
//Cancel vs Terminate: https://support.office.com/en-us/article/Cancel-a-workflow-in-progress-096b7d2d-9b8d-48f1-a002-e98bb86bdc7f
25-
[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.")]
30+
[Parameter(Mandatory = false, HelpMessage = "Forcefully terminate the workflow instead of cancelling. Works on errored and non-responsive workflows. Does not notify participants.")]
2631
public SwitchParameter Force;
2732

2833
protected override void ExecuteCmdlet()
2934
{
35+
var instanceService = new WorkflowServicesManager(ClientContext, SelectedWeb)
36+
.GetWorkflowInstanceService();
3037

31-
WorkflowServicesManager workflowServicesManager = new WorkflowServicesManager(ClientContext, SelectedWeb);
32-
InteropService interopService = workflowServicesManager.GetWorkflowInteropService();
33-
WorkflowInstanceService instanceService = workflowServicesManager.GetWorkflowInstanceService();
38+
var instance = Identity.Instance
39+
?? instanceService.GetInstance(Identity.Id);
3440

35-
if (Identity.Instance != null)
41+
var instanceId = Identity.Instance?.Id ?? Identity.Id;
42+
if (Force)
3643
{
37-
WriteVerbose("Instance object set");
38-
WriteVerbose("Cancelling workflow with ID: " + Identity.Instance.Id);
39-
if(Force)
40-
{
41-
instanceService.TerminateWorkflow(Identity.Instance);
42-
}
43-
else
44-
{
45-
Identity.Instance.CancelWorkFlow();
46-
}
47-
ClientContext.ExecuteQueryRetry();
44+
WriteVerbose("Terminating workflow with ID: " + instanceId);
45+
instanceService.TerminateWorkflow(instance);
4846
}
49-
else if (Identity.Id != Guid.Empty)
47+
else
5048
{
51-
WriteVerbose("Instance object not set. Looking up site workflows by GUID: " + Identity.Id);
52-
var allinstances = SelectedWeb.GetWorkflowInstances();
53-
foreach (var instance in allinstances.Where(instance => instance.Id == Identity.Id))
54-
{
55-
WriteVerbose("Cancelling workflow with ID: " + Identity.Instance.Id);
56-
if (Force)
57-
{
58-
instanceService.TerminateWorkflow(instance);
59-
}
60-
else
61-
{
62-
instance.CancelWorkFlow();
63-
}
64-
ClientContext.ExecuteQueryRetry();
65-
break;
66-
}
49+
WriteVerbose("Cancelling workflow with ID: " + instanceId);
50+
instanceService.CancelWorkflow(instance);
6751
}
52+
53+
ClientContext.ExecuteQueryRetryAsync();
6854
}
6955
}
7056
}

0 commit comments

Comments
 (0)
This repository has been archived.