Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit aab2ac8

Browse files
James MayJames May
James May
authored and
James May
committed
WorkflowInstance cmdlet improvements
Get: * 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 withouth specifying a list pipebind * added extra examples Resume: * improved implemention retrieve just the requested instance instead of all * added examples Start: * 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 Intiator and InitiationParameters parameters Stop: * added extra example * positional and piped instance parameter * verbose message now indicates cancel vs terminate
1 parent 172d641 commit aab2ac8

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
}

0 commit comments

Comments
 (0)