Skip to content

Commit

Permalink
Merge pull request #3176 from gautamdsheth/feature/refactor-delete-li…
Browse files Browse the repository at this point in the history
…stitem-version

Refactor Remove-PnPListItemVersion to use PnP Core SDK
  • Loading branch information
gautamdsheth authored Jun 11, 2023
2 parents 7dc080f + c50e10b commit 301329b
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/Commands/Lists/RemoveListItemVersion.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Linq;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.Core.Model.SharePoint;
using PnP.Core.QueryModel;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Extensions;
using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.Lists
Expand All @@ -12,53 +12,57 @@ public class RemoveListItemVersion : PnPWebCmdlet
{
[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0)]
public ListPipeBind List;

[Parameter(Mandatory = true, ValueFromPipeline = true)]
public ListItemPipeBind Identity;

[Parameter(Mandatory = true, ValueFromPipeline = true)]
public ListItemVersionPipeBind Version;

[Parameter(Mandatory = false)]
public SwitchParameter Force;

protected override void ExecuteCmdlet()
{
var list = List.GetList(CurrentWeb);
var list = List.GetList(PnPContext);

if (list is null)
{
throw new PSArgumentException($"Cannot find the list provided through -{nameof(List)}", nameof(List));
}

var item = Identity.GetListItem(list);

if (item is null)
{
throw new PSArgumentException($"Cannot find the list item provided through -{nameof(Identity)}", nameof(Identity));
}

item.LoadProperties(i => i.Versions);
item.EnsureProperties(i => i.All, i => i.Versions);

var itemVersionCollection = item.Versions.AsRequested();

ListItemVersion version = null;
IListItemVersion version = null;
if (!string.IsNullOrEmpty(Version.VersionLabel))
{
version = item.Versions.FirstOrDefault(v => v.VersionLabel == Version.VersionLabel);
version = itemVersionCollection.FirstOrDefault(v => v.VersionLabel == Version.VersionLabel);
}
else if (Version.Id != -1)
{
version = item.Versions.FirstOrDefault(v => v.VersionId == Version.Id);
version = itemVersionCollection.FirstOrDefault(v => v.Id == Version.Id);
}

if (version is null)
{
throw new PSArgumentException($"Cannot find the list item version provided through -{nameof(Version)}", nameof(Version));
}

if(Force || ShouldContinue(string.Format(Resources.Delete0, version.VersionLabel), Resources.Confirm))
if (Force || ShouldContinue(string.Format(Resources.Delete0, version.VersionLabel), Resources.Confirm))
{
WriteVerbose($"Trying to remove version {Version.VersionLabel}");

version.DeleteObject();
ClientContext.ExecuteQueryRetry();


version.Delete();

WriteVerbose($"Removed version {Version.VersionLabel} of list item {item.Id} in list {list.Title}");
}
}
Expand Down

0 comments on commit 301329b

Please sign in to comment.