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

Commit 713b52e

Browse files
James MayJames May
James May
authored and
James May
committed
WorkflowInstance cmdlet improvements
1 parent ba7615b commit 713b52e

7 files changed

+248
-204
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
}

Commands/PnP.PowerShell.Commands.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@
11541154
<Compile Include="Web\SetWebPermission.cs" />
11551155
<Compile Include="Web\SetRequestAccessEmails.cs" />
11561156
<Compile Include="Workflows\AddWorkflowDefinition.cs" />
1157-
<Compile Include="Workflows\GetWorkflowInstances.cs" />
1157+
<Compile Include="Workflows\GetWorkflowInstance.cs" />
11581158
<Compile Include="Workflows\ResumeWorkflowInstance.cs" />
11591159
<Compile Include="Workflows\StartWorkflowInstance.cs" />
11601160
<Compile Include="Workflows\StopWorkflowInstance.cs" />
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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")]
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 site workflow instances",
21+
SortOrder = 1)]
22+
[CmdletExample(
23+
Code = @"PS:> Start-PnPWorkflowInstance - | Get-PnPWorkflowInstance",
24+
Remarks = @"Retrieves workflow instance by workflow instance ID.",
25+
SortOrder = 2)]
26+
[CmdletExample(
27+
Code = @"PS:> Get-PnPWorkflowInstance B50B39CD-B89C-42FE-AC5F-F97BB7632D74",
28+
Remarks = @"Retrieves workflow instance by workflow instance ID.",
29+
SortOrder = 4)]
30+
[CmdletExample(
31+
Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem $ListItem",
32+
Remarks = @"Retrieves workflow instances on the provided item in list ""My Library""",
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:> Get-PnPWorkflowInstance -WorkflowSubscription D71FA124-9E6D-4382-AE8A-91B4C041B23E",
40+
Remarks = @"Retrieves workflow instances by workflow subscription ID",
41+
SortOrder = 7)]
42+
[CmdletExample(
43+
Code = @"PS:> Get-PnPWorkflowSubscription | Get-PnPWorkflowInstance",
44+
Remarks = @"Retrieves workflow instances from all subscriptions",
45+
SortOrder = 8)]
46+
47+
public class GetWorkflowInstance : PnPWebCmdlet
48+
{
49+
private const string ParameterSet_BYSITE = "By Site";
50+
private const string ParameterSet_BYGUID = "By GUID";
51+
private const string ParameterSet_BYLISTITEM = "By List and ListItem";
52+
private const string ParameterSet_BYSUBSCRIPTION = "By WorkflowSubscription";
53+
private const string ParameterSet_BYSUBSCRIPTIONPIPE = "By WorkflowSubscription (Pipe)";
54+
55+
protected override void ExecuteCmdlet()
56+
{
57+
switch (ParameterSetName)
58+
{
59+
case ParameterSet_BYSITE:
60+
ExecuteCmdletBySite();
61+
break;
62+
case ParameterSet_BYLISTITEM:
63+
ExecuteCmdletByListItem();
64+
break;
65+
case ParameterSet_BYSUBSCRIPTION:
66+
case ParameterSet_BYSUBSCRIPTIONPIPE:
67+
ExecuteCmdletBySubscription();
68+
break;
69+
70+
case ParameterSet_BYGUID:
71+
ExecuteCmdletByGuid();
72+
break;
73+
default:
74+
throw new NotImplementedException($"{nameof(ParameterSetName)}: {ParameterSetName}");
75+
}
76+
}
77+
78+
private void ExecuteCmdletBySite()
79+
{
80+
var instances = new WorkflowServicesManager(ClientContext, SelectedWeb)
81+
.GetWorkflowInstanceService()
82+
.EnumerateInstancesForSite();
83+
84+
ClientContext.Load(instances);
85+
ClientContext.ExecuteQueryRetry();
86+
87+
WriteObject(instances, true);
88+
}
89+
90+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List for which workflow instances should be retrieved", Position = 0)]
91+
public ListPipeBind List;
92+
93+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List Item for which workflow instances should be retrieved", Position = 1)]
94+
public ListItemPipeBind ListItem;
95+
96+
private void ExecuteCmdletByListItem()
97+
{
98+
var list = List.GetList(SelectedWeb)
99+
?? throw new PSArgumentException($"No list found with id, title or url '{List}'", nameof(List));
100+
101+
var listitem = ListItem.GetListItem(list)
102+
?? throw new PSArgumentException($"No list item found with id, or title '{ListItem}'", nameof(ListItem));
103+
104+
var workflows = new WorkflowServicesManager(ClientContext, SelectedWeb)
105+
.GetWorkflowInstanceService()
106+
.EnumerateInstancesForListItem(list.Id, listitem.Id);
107+
108+
ClientContext.Load(workflows);
109+
ClientContext.ExecuteQueryRetry();
110+
WriteObject(workflows, true);
111+
}
112+
113+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTION, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0)]
114+
public WorkflowSubscriptionPipeBind WorkflowSubscription;
115+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTIONPIPE, ValueFromPipeline = true, DontShow = true)]
116+
public WorkflowSubscription WorkflowSubscriptionPipe;
117+
118+
private void ExecuteCmdletBySubscription()
119+
{
120+
var workflowSubscription = WorkflowSubscriptionPipe
121+
?? WorkflowSubscription.GetWorkflowSubscription(SelectedWeb)
122+
?? throw new PSArgumentException($"No workflow subscription found for '{WorkflowSubscription}'", nameof(WorkflowSubscription));
123+
124+
var workflows = workflowSubscription.GetInstances();
125+
WriteObject(workflows, true);
126+
}
127+
128+
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYGUID, HelpMessage = "The guid of the workflow instance to retrieved.", Position = 0, ValueFromPipeline = true, ValueFromRemainingArguments = true)]
129+
public WorkflowInstancePipeBind Identity;
130+
131+
private void ExecuteCmdletByGuid()
132+
{
133+
var workflowInstanceId = Identity.Instance?.EnsureProperty(i => i.Id) ?? Identity.Id;
134+
135+
var instance = new WorkflowServicesManager(ClientContext, SelectedWeb)
136+
.GetWorkflowInstanceService()
137+
.GetInstance(workflowInstanceId);
138+
139+
ClientContext.Load(instance);
140+
ClientContext.ExecuteQueryRetry();
141+
142+
WriteObject(instance, true);
143+
}
144+
}
145+
}

Commands/Workflows/GetWorkflowInstances.cs

-107
This file was deleted.
+20-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using System;
22
using System.Linq;
33
using System.Management.Automation;
4+
using System.Management.Instrumentation;
5+
46
using Microsoft.SharePoint.Client;
7+
using Microsoft.SharePoint.Client.WorkflowServices;
8+
59
using PnP.PowerShell.CmdletHelpAttributes;
610
using PnP.PowerShell.Commands.Base.PipeBinds;
711

@@ -12,31 +16,29 @@ namespace PnP.PowerShell.Commands.Workflows
1216
"Resumes a previously stopped workflow instance",
1317
Category = CmdletHelpCategory.Workflows)]
1418
[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.",
19+
Code = @"PS:> Resume-PnPWorkflowInstance -Identity $wfInstance",
20+
Remarks = "Resumes the workflow instance, this can be a instance ID (Guid) or the instance itself.",
1721
SortOrder = 1)]
22+
[CmdletExample(
23+
Code = @"PS:> $wfInstances | Resume-PnPWorkflowInstance",
24+
Remarks = "Resumes the workflow instance(s), either instance IDs or the instance objects",
25+
SortOrder = 2)]
1826
public class ResumeWorkflowInstance : PnPWebCmdlet
1927
{
20-
[Parameter(Mandatory = true, HelpMessage = "The instance to resume", Position = 0)]
28+
[Parameter(Mandatory = true, HelpMessage = "The instance to resume", Position = 0, ValueFromPipeline = true)]
2129
public WorkflowInstancePipeBind Identity;
2230

2331
protected override void ExecuteCmdlet()
2432
{
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-
}
33+
var workflowInstanceService = new WorkflowServicesManager(ClientContext, SelectedWeb)
34+
.GetWorkflowInstanceService();
35+
36+
var instance = Identity.Instance
37+
?? workflowInstanceService.GetInstance(Identity.Id);
4038

39+
workflowInstanceService.ResumeWorkflow(instance);
4140

41+
ClientContext.ExecuteQueryRetry();
42+
}
43+
}
4244
}

0 commit comments

Comments
 (0)