Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor RecycleBin cmdlets to use PnP Core SDK where possible #4393

Merged
merged 1 commit into from
Oct 6, 2024
Merged
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
25 changes: 22 additions & 3 deletions src/Commands/Base/PipeBinds/RecycleBinItemPipeBind.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
using System;
using Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client;
using PnP.Core.Model.SharePoint;
using PnP.PowerShell.Commands.Model.SharePoint;
using System;

namespace PnP.PowerShell.Commands.Base.PipeBinds
{
public sealed class RecycleBinItemPipeBind
{
private RecycleBinItem _item;
private IRecycleBinItem _recycleBinItem;
private readonly Guid? _id;

public RecycleBinItemPipeBind()
{
_item = null;
_id = null;
_recycleBinItem = null;
}

public RecycleBinItemPipeBind(RecycleBinItem item)
Expand All @@ -26,6 +28,11 @@ public RecycleBinItemPipeBind(RecycleResult result)
_id = result.RecycleBinItemId;
}

public RecycleBinItemPipeBind(IRecycleBinItem result)
{
_recycleBinItem = result;
}

public RecycleBinItemPipeBind(string id)
{
Guid guid;
Expand All @@ -42,6 +49,8 @@ public RecycleBinItemPipeBind(string id)

public RecycleBinItem Item => _item;

public IRecycleBinItem RecycleBinItem => _recycleBinItem;

public Guid? Id => _id;

internal RecycleBinItem GetRecycleBinItem(Microsoft.SharePoint.Client.Site site)
Expand All @@ -54,5 +63,15 @@ internal RecycleBinItem GetRecycleBinItem(Microsoft.SharePoint.Client.Site site)
site.Context.ExecuteQueryRetry();
return Item;
}

internal IRecycleBinItem GetRecycleBinItem(Core.Services.PnPContext context)
{
if (RecycleBinItem != null) return RecycleBinItem;
if (!_id.HasValue) return null;

_recycleBinItem = context.Site.RecycleBin.GetById(_id.Value, r => r.LeafName);

return RecycleBinItem;
}
}
}
16 changes: 6 additions & 10 deletions src/Commands/RecycleBin/ClearRecycleBinItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Utilities;
using System.Management.Automation;
using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.RecycleBin
Expand Down Expand Up @@ -34,12 +33,11 @@ protected override void ExecuteCmdlet()
switch (ParameterSetName)
{
case PARAMETERSET_IDENTITY:
var recycleBinItem = Identity.GetRecycleBinItem(ClientContext.Site);
var recycleBinItem = Identity.GetRecycleBinItem(Connection.PnPContext);

if (Force || ShouldContinue(string.Format(Resources.ClearRecycleBinItem, recycleBinItem.LeafName), Resources.Confirm))
{
recycleBinItem.DeleteObject();
ClientContext.ExecuteQueryRetry();
recycleBinItem.Delete();
}
break;
case PARAMETERSET_ALL:
Expand All @@ -64,16 +62,14 @@ protected override void ExecuteCmdlet()
{
if (Force || ShouldContinue(Resources.ClearSecondStageRecycleBin, Resources.Confirm))
{
ClientContext.Site.RecycleBin.DeleteAllSecondStageItems();
ClientContext.ExecuteQueryRetry();
Connection.PnPContext.Site.RecycleBin.DeleteAllSecondStageItems();
}
}
else
{
if (Force || ShouldContinue(Resources.ClearBothRecycleBins, Resources.Confirm))
{
ClientContext.Site.RecycleBin.DeleteAll();
ClientContext.ExecuteQueryRetry();
Connection.PnPContext.Site.RecycleBin.DeleteAll();
}
}
}
Expand Down
11 changes: 3 additions & 8 deletions src/Commands/RecycleBin/MoveRecycleBinItem.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using PnP.PowerShell.Commands.Base.PipeBinds;
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using PnP.PowerShell.Commands.Base.PipeBinds;
using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.RecycleBin
Expand All @@ -20,19 +17,17 @@ protected override void ExecuteCmdlet()
{
if (ParameterSpecified(nameof(Identity)))
{
var item = Identity.GetRecycleBinItem(ClientContext.Site);
var item = Identity.GetRecycleBinItem(Connection.PnPContext);
if (Force || ShouldContinue(string.Format(Resources.MoveRecycleBinItemWithLeaf0ToSecondStage, item.LeafName), Resources.Confirm))
{
item.MoveToSecondStage();
ClientContext.ExecuteQueryRetry();
}
}
else
{
if (Force || ShouldContinue(Resources.MoveFirstStageRecycleBinItemsToSecondStage, Resources.Confirm))
{
ClientContext.Site.RecycleBin.MoveAllToSecondStage();
ClientContext.ExecuteQueryRetry();
Connection.PnPContext.Site.RecycleBin.MoveAllToSecondStage();
}
}
}
Expand Down
12 changes: 4 additions & 8 deletions src/Commands/RecycleBin/RestoreRecycleBinItem.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Management.Automation;
using Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client;
using PnP.PowerShell.Commands.Base.PipeBinds;
using PnP.PowerShell.Commands.Utilities;
using System.Management.Automation;
using Resources = PnP.PowerShell.Commands.Properties.Resources;

namespace PnP.PowerShell.Commands.RecycleBin
Expand All @@ -25,12 +23,11 @@ protected override void ExecuteCmdlet()
{
if (ParameterSpecified(nameof(Identity)))
{
var recycleBinItem = Identity.GetRecycleBinItem(ClientContext.Site);
var recycleBinItem = Identity.GetRecycleBinItem(Connection.PnPContext);

if (Force || ShouldContinue(string.Format(Resources.RestoreRecycleBinItem, recycleBinItem.LeafName), Resources.Confirm))
{
recycleBinItem.Restore();
ClientContext.ExecuteQueryRetry();
}
}
else
Expand All @@ -52,8 +49,7 @@ protected override void ExecuteCmdlet()
{
if (Force || ShouldContinue(Resources.RestoreRecycleBinItems, Resources.Confirm))
{
ClientContext.Site.RecycleBin.RestoreAll();
ClientContext.ExecuteQueryRetry();
Connection.PnPContext.Site.RecycleBin.RestoreAll();
}
}
}
Expand Down
Loading