|
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 | +} |
0 commit comments