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

WorkflowInstance cmdlet improvements #2871

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions Commands/Base/PipeBinds/WorkflowInstancePipeBind.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
using System;

using Microsoft.SharePoint.Client.WorkflowServices;

namespace PnP.PowerShell.Commands.Base.PipeBinds
{
public sealed class WorkflowInstancePipeBind
{
private readonly WorkflowInstance _instance;
private readonly Guid _id;

public WorkflowInstancePipeBind()
{
_instance = null;
_id = Guid.Empty;
Instance = null;
Id = Guid.Empty;
}

public WorkflowInstancePipeBind(WorkflowInstance instance)
{
_instance = instance;
Instance = instance;
}

public WorkflowInstancePipeBind(Guid guid)
{
_id = guid;
Id = guid;
}

public WorkflowInstancePipeBind(string id)
{
_id = Guid.Parse(id);
Id = Guid.Parse(id);
}

public Guid Id => _id;
public Guid Id { get; }

public WorkflowInstance Instance => _instance;
public WorkflowInstance Instance { get; }
}
}
269 changes: 162 additions & 107 deletions Commands/Workflows/GetWorkflowInstances.cs
Original file line number Diff line number Diff line change
@@ -1,107 +1,162 @@
using System;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.PowerShell.CmdletHelpAttributes;
using PnP.PowerShell.Commands.Base.PipeBinds;

namespace PnP.PowerShell.Commands.Workflows
{
[Cmdlet(VerbsCommon.Get, "PnPWorkflowInstance", DefaultParameterSetName = ParameterSet_BYLISTITEM)]
[CmdletHelp("Gets SharePoint 2010/2013 workflow instances",
DetailedDescription = "Gets all SharePoint 2010/2013 workflow instances",
Category = CmdletHelpCategory.Workflows,
SupportedPlatform = CmdletSupportedPlatform.All)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem $ListItem",
Remarks = @"Retrieves workflow instances running against the provided item on list ""My Library""",
SortOrder = 1)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem 2",
Remarks = @"Retrieves workflow instances running against the provided item with 2 in the list ""My Library""",
SortOrder = 2)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowSubscription | Get-PnPWorkflowInstance",
Remarks = @"Retrieves workflow instances from all subscriptions",
SortOrder = 3)]

public class GetWorkflowInstance : PnPWebCmdlet
{
private const string ParameterSet_BYLISTITEM = "By List and ListItem";
private const string ParameterSet_BYSUBSCRIPTION = "By WorkflowSubscription";

protected override void ExecuteCmdlet()
{
switch (ParameterSetName)
{
case ParameterSet_BYLISTITEM:
ExecuteCmdletByListItem();
break;
case ParameterSet_BYSUBSCRIPTION:
ExecuteCmdletBySubscription();
break;
default:
throw new NotImplementedException($"{nameof(ParameterSetName)}: {ParameterSetName}");
}
}

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List for which workflow instances should be retrieved", Position = 0)]
public ListPipeBind List;

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List Item for which workflow instances should be retrieved", Position = 1)]
public ListItemPipeBind ListItem;

private void ExecuteCmdletByListItem()
{
List list = null;
ListItem listitem = null;

if (List != null)
{
list = List.GetList(SelectedWeb);
if (list == null)
{
throw new PSArgumentException($"No list found with id, title or url '{List}'", nameof(List));
}
}
else
{
throw new PSArgumentException("List required");
}

if (ListItem != null)
{
listitem = ListItem.GetListItem(list);
if (listitem == null)
{
throw new PSArgumentException($"No list item found with id, or title '{ListItem}'", nameof(ListItem));
}
}
else
{
throw new PSArgumentException("List Item required");
}

var workflowServicesManager = new Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager(ClientContext, SelectedWeb);
var workflowInstanceService = workflowServicesManager.GetWorkflowInstanceService();
var workflows = workflowInstanceService.EnumerateInstancesForListItem(list.Id, listitem.Id);
ClientContext.Load(workflows);
ClientContext.ExecuteQueryRetry();
WriteObject(workflows, true);
}

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTION, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0, ValueFromPipeline = true)]
public WorkflowSubscriptionPipeBind WorkflowSubscription;

private void ExecuteCmdletBySubscription()
{
var workflowSubscription = WorkflowSubscription.GetWorkflowSubscription(SelectedWeb);
if (workflowSubscription == null)
{
throw new PSArgumentException($"No workflow subscription found for '{WorkflowSubscription}'", nameof(WorkflowSubscription));
}

var workflows = workflowSubscription.GetInstances();
WriteObject(workflows, true);
}
}
}
using System;
using System.Management.Automation;

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WorkflowServices;

using PnP.PowerShell.CmdletHelpAttributes;
using PnP.PowerShell.Commands.Base.PipeBinds;

namespace PnP.PowerShell.Commands.Workflows
{
[Cmdlet(VerbsCommon.Get, "PnPWorkflowInstance", DefaultParameterSetName = ParameterSet_BYSITE)]
[CmdletHelp("Gets SharePoint 2010/2013 workflow instances",
DetailedDescription = "Gets all SharePoint 2010/2013 workflow instances",
Category = CmdletHelpCategory.Workflows,
OutputType = typeof(WorkflowInstance),
SupportedPlatform = CmdletSupportedPlatform.All)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowInstance",
Remarks = @"Retrieves workflow instances for site workflows",
SortOrder = 1)]
[CmdletExample(
Code = @"PS:> $wfSubscriptions | Get-PnPWorkflowInstance",
Remarks = @"Retrieves workflow instance(s) for specified subscription(s).",
SortOrder = 2)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowInstance ""ab77c32e-8b61-4fb4-bb41-be12193e9852""",
Remarks = @"Retrieves workflow instance by workflow instance ID.",
SortOrder = 4)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowInstance $listItem",
Remarks = @"Retrieves workflow instances on the provided list item",
SortOrder = 5)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowInstance -List ""My Library"" -ListItem 2",
Remarks = @"Retrieves workflow instances on the provided item with 2 in list ""My Library""",
SortOrder = 6)]
[CmdletExample(
Code = @"PS:> $listItems | Get-PnPWorkflowInstance",
Remarks = @"Retrieves workflow instances on the provided items",
SortOrder = 6)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowInstance -WorkflowSubscription ""ab77c32e-8b61-4fb4-bb41-be12193e9852""",
Remarks = @"Retrieves workflow instances by workflow subscription ID",
SortOrder = 7)]
[CmdletExample(
Code = @"PS:> Get-PnPWorkflowSubscription | Get-PnPWorkflowInstance",
Remarks = @"Retrieves workflow instances from all subscriptions",
SortOrder = 8)]

public class GetWorkflowInstance : PnPWebCmdlet
{
private const string ParameterSet_BYSITE = "By Site";
private const string ParameterSet_BYGUID = "By GUID";
private const string ParameterSet_BYLISTITEM = "By ListItem";
private const string ParameterSet_BYLISTITEMOBJECT = "By ListItem object";
private const string ParameterSet_BYSUBSCRIPTION = "By WorkflowSubscription";
private const string ParameterSet_BYSUBSCRIPTIONOBJECT = "By WorkflowSubscription object";

protected override void ExecuteCmdlet()
{
switch (ParameterSetName)
{
case ParameterSet_BYSITE:
ExecuteCmdletBySite();
break;

case ParameterSet_BYLISTITEM:
case ParameterSet_BYLISTITEMOBJECT:
ExecuteCmdletByListItem();
break;
case ParameterSet_BYSUBSCRIPTION:
case ParameterSet_BYSUBSCRIPTIONOBJECT:
ExecuteCmdletBySubscription();
break;

case ParameterSet_BYGUID:
ExecuteCmdletByIdentity();
break;
default:
throw new NotImplementedException($"{nameof(ParameterSetName)}: {ParameterSetName}");
}
}

private void ExecuteCmdletBySite()
{
var instances = new WorkflowServicesManager(ClientContext, SelectedWeb)
.GetWorkflowInstanceService()
.EnumerateInstancesForSite();

ClientContext.Load(instances);
ClientContext.ExecuteQueryRetry();

WriteObject(instances, true);
}

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List for which workflow instances should be retrieved", Position = 0)]
public ListPipeBind List;

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEM, HelpMessage = "The List Item for which workflow instances should be retrieved", Position = 1)]
public ListItemPipeBind ListItem;

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYLISTITEMOBJECT, HelpMessage = "The ListItem for which workflow instances should be retrieved", Position = 0)]
public ListItem ListItemObject;

private void ExecuteCmdletByListItem()
{
var list = ListItemObject?.ParentList
?? List.GetList(SelectedWeb)
?? throw new PSArgumentException($"No list found with id, title or url '{List}'", nameof(List));

var listId = list.EnsureProperty(x => x.Id);

var listItem = ListItemObject
?? ListItem.GetListItem(list)
?? throw new PSArgumentException($"No list item found with id, or title '{ListItem}'", nameof(ListItem));

var listItemId = listItem.EnsureProperty(x => x.Id);

var workflows = new WorkflowServicesManager(ClientContext, SelectedWeb)
.GetWorkflowInstanceService()
.EnumerateInstancesForListItem(listId, listItemId);

ClientContext.Load(workflows);
ClientContext.ExecuteQueryRetry();
WriteObject(workflows, true);
}

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTION, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0)]
public WorkflowSubscriptionPipeBind WorkflowSubscription;
[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYSUBSCRIPTIONOBJECT, HelpMessage = "The workflow subscription for which workflow instances should be retrieved", Position = 0, ValueFromPipeline = true)]
public WorkflowSubscription WorkflowSubscriptionObject;

private void ExecuteCmdletBySubscription()
{
var workflowSubscription = WorkflowSubscriptionObject
?? WorkflowSubscription.GetWorkflowSubscription(SelectedWeb)
?? throw new PSArgumentException($"No workflow subscription found for '{WorkflowSubscription}'", nameof(WorkflowSubscription));

var workflows = workflowSubscription.GetInstances();
WriteObject(workflows, true);
}

[Parameter(Mandatory = true, ParameterSetName = ParameterSet_BYGUID, HelpMessage = "The guid of the workflow instance to retrieved.", Position = 0, ValueFromPipeline = true, ValueFromRemainingArguments = true)]
public WorkflowInstancePipeBind Identity;

private void ExecuteCmdletByIdentity()
{
var workflowInstanceId = Identity.Instance?.EnsureProperty(i => i.Id)
?? Identity.Id;

var instance = new WorkflowServicesManager(ClientContext, SelectedWeb)
.GetWorkflowInstanceService()
.GetInstance(workflowInstanceId);

ClientContext.Load(instance);
ClientContext.ExecuteQueryRetry();

WriteObject(instance, true);
}
}
}
41 changes: 23 additions & 18 deletions Commands/Workflows/ResumeWorkflowInstance.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Linq;
using System.Management.Automation;

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WorkflowServices;

using PnP.PowerShell.CmdletHelpAttributes;
using PnP.PowerShell.Commands.Base.PipeBinds;

Expand All @@ -12,31 +15,33 @@ namespace PnP.PowerShell.Commands.Workflows
"Resumes a previously stopped workflow instance",
Category = CmdletHelpCategory.Workflows)]
[CmdletExample(
Code = @"PS:> Resume-PnPWorkflowInstance -identity $wfInstance",
Remarks = "Resumes the workflow instance, this can be the Guid of the instance or the instance itself.",
Code = @"PS:> Resume-PnPWorkflowInstance ab77c32e-8b61-4fb4-bb41-be12193e9852",
Remarks = "Resumes the workflow instance, this can be a instance ID (Guid) or the instance itself.",
SortOrder = 1)]
[CmdletExample(
Code = @"PS:> Resume-PnPWorkflowInstance -Identity ""ab77c32e-8b61-4fb4-bb41-be12193e9852""",
Remarks = "Resumes the workflow instance, this can be a instance ID (Guid) or the instance itself.",
SortOrder = 2)]
[CmdletExample(
Code = @"PS:> $wfInstances | Resume-PnPWorkflowInstance",
Remarks = "Resumes the workflow instance(s), either instance IDs or the instance objects",
SortOrder = 3)]
public class ResumeWorkflowInstance : PnPWebCmdlet
{
[Parameter(Mandatory = true, HelpMessage = "The instance to resume", Position = 0)]
[Parameter(Mandatory = true, HelpMessage = "The instance to resume", Position = 0, ValueFromPipeline = true)]
public WorkflowInstancePipeBind Identity;

protected override void ExecuteCmdlet()
{
if (Identity.Instance != null)
{
Identity.Instance.ResumeWorkflow();
}
else if (Identity.Id != Guid.Empty)
{
var allinstances = SelectedWeb.GetWorkflowInstances();
foreach (var instance in allinstances.Where(instance => instance.Id == Identity.Id))
{
instance.ResumeWorkflow();
break;
}
}
}
}
var workflowInstanceService = new WorkflowServicesManager(ClientContext, SelectedWeb)
.GetWorkflowInstanceService();

var instance = Identity.Instance
?? workflowInstanceService.GetInstance(Identity.Id);

workflowInstanceService.ResumeWorkflow(instance);

ClientContext.ExecuteQueryRetry();
}
}
}
Loading