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