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

Commit 4e75e55

Browse files
Merge pull request #2760 from Professr/issue#2315
Added a RowLimit parameter to Clear-PnPRecycleBinItem and Restore-PnPRecycleBinItem
2 parents b92745a + 8676c6c commit 4e75e55

File tree

4 files changed

+115
-55
lines changed

4 files changed

+115
-55
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
88
## [3.23.2007.0] (not yet released)
99

1010
### Added
11+
- Added a `-RowLimit` parameter to `Clear-PnPRecycleBinItem` and `Restore-PnPRecycleBinItem` so that it can be used on recycle bins which hold more than 5000 items [PR #2760](https://github.com/pnp/PnP-PowerShell/pull/2760)
1112
- Added connection option to `Connect-PnPOnline` taking `-Scopes` and `-Credentials` to allow setting up a delegated permission token for use with Microsoft Graph and the Office 365 Management API. See [this wiki page](https://github.com/pnp/PnP-PowerShell/wiki/Connect-options#connect-using-scopes-and-credentials) for more details. [PR #2746](https://github.com/pnp/PnP-PowerShell/pull/2746)
1213
- Added Add-PnPTeamsChannel, Get-PnPTeamsApp, Get-PnPTeamsChannel, Get-PnPTeamsTab, Get-PnPTeamsTeam, Remove-PnPTeamsChannel, Remove-PnPTeamsTab, Remove-PnPTeamsTeam cmdlets
1314

@@ -19,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
1920
- Erwin van Hunen [erwinvanhunen]
2021
- Gautam Sheth [gautamdsheth]
2122
- Koen Zomers [koenzomers]
23+
- Ellie Hussey [Professr]
2224
- Todd Klindt [ToddKlindt]
2325

2426
## [3.22.2006.2]

Commands/RecycleBin/ClearRecycleBinItem.cs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
namespace SharePointPnP.PowerShell.Commands.RecycleBin
88
{
9-
[Cmdlet(VerbsCommon.Clear, "PnPRecycleBinItem", DefaultParameterSetName = "All")]
9+
[Cmdlet(VerbsCommon.Clear, "PnPRecycleBinItem", DefaultParameterSetName = PARAMETERSET_ALL)]
1010
[CmdletHelp("Permanently deletes all or a specific recycle bin item",
11+
SupportedPlatform = CmdletSupportedPlatform.All,
1112
Category = CmdletHelpCategory.RecycleBin)]
1213
[CmdletExample(
1314
Code = @"PS:> Get-PnPRecycleBinItem | ? FileLeafName -like ""*.docx"" | Clear-PnpRecycleBinItem",
@@ -21,52 +22,85 @@ namespace SharePointPnP.PowerShell.Commands.RecycleBin
2122
Code = @"PS:> Clear-PnpRecycleBinItem -Identity $item -Force",
2223
Remarks = "Permanently deletes the recycle bin item stored under variable $item from the recycle bin without asking for confirmation from the end user first",
2324
SortOrder = 3)]
25+
#if !SP2013
26+
[CmdletExample(
27+
Code = @"PS:> Clear-PnPRecycleBinItem -All -RowLimit 10000",
28+
Remarks = "Permanently deletes up to 10,000 items in the recycle bin",
29+
SortOrder = 4)]
30+
#endif
31+
2432
public class ClearRecycleBinItem : PnPSharePointCmdlet
2533
{
26-
[Parameter(Mandatory = true, HelpMessage = "Id of the recycle bin item or the recycle bin item itself to permanently delete", ValueFromPipeline = true, ParameterSetName = "Identity")]
34+
const string PARAMETERSET_ALL = "All";
35+
const string PARAMETERSET_IDENTITY = "Identity";
36+
37+
[Parameter(Mandatory = true, HelpMessage = "Id of the recycle bin item or the recycle bin item itself to permanently delete", ValueFromPipeline = true, ParameterSetName = PARAMETERSET_IDENTITY)]
2738
public RecycleBinItemPipeBind Identity;
2839

29-
[Parameter(Mandatory = false, ParameterSetName = "All", HelpMessage = "Clears all items")]
40+
[Parameter(Mandatory = false, ParameterSetName = PARAMETERSET_ALL, HelpMessage = "Clears all items")]
3041
public SwitchParameter All;
3142

3243
#if !ONPREMISES
33-
[Parameter(Mandatory = false, HelpMessage = "If provided, only all the items in the second stage recycle bin will be cleared", ParameterSetName = "All")]
44+
[Parameter(Mandatory = false, HelpMessage = "If provided, only all the items in the second stage recycle bin will be cleared", ParameterSetName = PARAMETERSET_ALL)]
3445
public SwitchParameter SecondStageOnly = false;
3546
#endif
36-
[Parameter(Mandatory = false, HelpMessage = "If provided, no confirmation will be asked to permanently delete the recycle bin item")]
47+
[Parameter(Mandatory = false, HelpMessage = "If provided, no confirmation will be asked to restore the recycle bin item", ParameterSetName = PARAMETERSET_IDENTITY)]
48+
[Parameter(Mandatory = false, HelpMessage = "If provided, no confirmation will be asked to restore the recycle bin item", ParameterSetName = PARAMETERSET_ALL)]
3749
public SwitchParameter Force;
3850

51+
#if !SP2013
52+
[Parameter(Mandatory = false, HelpMessage = "Limits deletion to specified number of items", ParameterSetName = PARAMETERSET_ALL)]
53+
public int RowLimit;
54+
#endif
3955

4056
protected override void ExecuteCmdlet()
4157
{
4258
switch (ParameterSetName)
4359
{
44-
case "Identity":
60+
case PARAMETERSET_IDENTITY:
4561
var recycleBinItem = Identity.GetRecycleBinItem(ClientContext.Site);
4662

47-
if (Force ||
48-
ShouldContinue(string.Format(Resources.ClearRecycleBinItem, recycleBinItem.LeafName), Resources.Confirm))
63+
if (Force || ShouldContinue(string.Format(Resources.ClearRecycleBinItem, recycleBinItem.LeafName), Resources.Confirm))
4964
{
5065
recycleBinItem.DeleteObject();
5166
ClientContext.ExecuteQueryRetry();
5267
}
5368
break;
54-
case "All":
55-
#if !ONPREMISES
56-
if (SecondStageOnly)
69+
case PARAMETERSET_ALL:
70+
#if !SP2013
71+
if (ParameterSpecified(nameof(RowLimit)))
5772
{
58-
if (Force || ShouldContinue(Resources.ClearSecondStageRecycleBin, Resources.Confirm))
59-
{
60-
ClientContext.Site.RecycleBin.DeleteAllSecondStageItems();
73+
if (Force || ShouldContinue(SecondStageOnly ? Resources.ClearSecondStageRecycleBin : Resources.ClearBothRecycleBins, Resources.Confirm))
74+
{
75+
RecycleBinItemState recycleBinStage = SecondStageOnly ? RecycleBinItemState.SecondStageRecycleBin : RecycleBinItemState.None;
76+
77+
RecycleBinItemCollection items = ClientContext.Site.GetRecycleBinItems(null, RowLimit, false, RecycleBinOrderBy.DeletedDate, recycleBinStage);
78+
ClientContext.Load(items);
79+
ClientContext.ExecuteQueryRetry();
80+
81+
items.DeleteAll();
6182
ClientContext.ExecuteQueryRetry();
6283
}
6384
}
6485
else
86+
#endif
6587
{
66-
if (Force || ShouldContinue(Resources.ClearBothRecycleBins, Resources.Confirm))
88+
#if !ONPREMISES
89+
if (SecondStageOnly)
6790
{
68-
ClientContext.Site.RecycleBin.DeleteAll();
69-
ClientContext.ExecuteQueryRetry();
91+
if (Force || ShouldContinue(Resources.ClearSecondStageRecycleBin, Resources.Confirm))
92+
{
93+
ClientContext.Site.RecycleBin.DeleteAllSecondStageItems();
94+
ClientContext.ExecuteQueryRetry();
95+
}
96+
}
97+
else
98+
{
99+
if (Force || ShouldContinue(Resources.ClearBothRecycleBins, Resources.Confirm))
100+
{
101+
ClientContext.Site.RecycleBin.DeleteAll();
102+
ClientContext.ExecuteQueryRetry();
103+
}
70104
}
71105
}
72106
break;
@@ -76,6 +110,7 @@ protected override void ExecuteCmdlet()
76110
ClientContext.Site.RecycleBin.DeleteAll();
77111
ClientContext.ExecuteQueryRetry();
78112
}
113+
}
79114
break;
80115
#endif
81116
}

Commands/RecycleBin/GetRecycleBinItem.cs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace SharePointPnP.PowerShell.Commands.RecycleBin
1414
Category = CmdletHelpCategory.RecycleBin,
1515
OutputType = typeof(RecycleBinItem),
1616
SupportedPlatform = CmdletSupportedPlatform.All,
17-
OutputTypeLink = "https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-server/ee541897(v=office.15)")]
17+
OutputTypeLink = "https://docs.microsoft.com/previous-versions/office/sharepoint-server/ee541897(v=office.15)")]
1818
[CmdletExample(
1919
Code = @"PS:> Get-PnPRecycleBinItem",
2020
Remarks = "Returns all items in both the first and the second stage recycle bins in the current site collection",
@@ -57,7 +57,7 @@ public class GetRecycleBinItems : PnPRetrievalsCmdlet<RecycleBinItem>
5757
[Parameter(Mandatory = false, HelpMessage = "Limits return results to specified amount", ParameterSetName = ParameterSet_FIRSTSTAGE)]
5858
[Parameter(Mandatory = false, HelpMessage = "Limits return results to specified amount", ParameterSetName = ParameterSet_SECONDSTAGE)]
5959
[Parameter(Mandatory = false, HelpMessage = "Limits return results to specified amount", ParameterSetName = ParameterSet_ALL)]
60-
public int RowLimit = -1;
60+
public int RowLimit;
6161
#endif
6262
protected override void ExecuteCmdlet()
6363
{
@@ -73,7 +73,7 @@ protected override void ExecuteCmdlet()
7373
else
7474
{
7575
#if !SP2013
76-
if (HasRowLimit())
76+
if (ParameterSpecified(nameof(RowLimit)))
7777
{
7878
RecycleBinItemState recycleBinStage;
7979
switch (ParameterSetName)
@@ -89,14 +89,12 @@ protected override void ExecuteCmdlet()
8989
break;
9090
}
9191

92-
RecycleBinItemCollection items = ClientContext.Site.GetRecycleBinItems(null, RowLimit, false, RecycleBinOrderBy.DeletedDate,
93-
recycleBinStage);
92+
RecycleBinItemCollection items = ClientContext.Site.GetRecycleBinItems(null, RowLimit, false, RecycleBinOrderBy.DeletedDate, recycleBinStage);
9493
ClientContext.Load(items);
9594
ClientContext.ExecuteQueryRetry();
9695

9796
List<RecycleBinItem> recycleBinItemList = items.ToList();
9897
WriteObject(recycleBinItemList, true);
99-
10098
}
10199
else
102100
{
@@ -107,7 +105,6 @@ protected override void ExecuteCmdlet()
107105

108106
switch (ParameterSetName)
109107
{
110-
111108
case ParameterSet_FIRSTSTAGE:
112109
WriteObject(
113110
recycleBinItemList.Where(i => i.ItemState == RecycleBinItemState.FirstStageRecycleBin), true);
@@ -121,7 +118,6 @@ protected override void ExecuteCmdlet()
121118
WriteObject(recycleBinItemList, true);
122119
break;
123120
}
124-
125121
}
126122
#else
127123
ClientContext.Site.Context.Load(ClientContext.Site.RecycleBin, r => r.IncludeWithDefaultProperties(RetrievalExpressions));
@@ -148,12 +144,5 @@ protected override void ExecuteCmdlet()
148144
#endif
149145
}
150146
}
151-
152-
#if !SP2013
153-
private bool HasRowLimit()
154-
{
155-
return RowLimit > 0;
156-
}
157-
#endif
158147
}
159148
}

Commands/RecycleBin/RestoreRecycleBinItem.cs

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
using System.Management.Automation;
1+
using System;
2+
using System.Management.Automation;
23
using Microsoft.SharePoint.Client;
34
using SharePointPnP.PowerShell.CmdletHelpAttributes;
45
using SharePointPnP.PowerShell.Commands.Base.PipeBinds;
56
using Resources = SharePointPnP.PowerShell.Commands.Properties.Resources;
67

78
namespace SharePointPnP.PowerShell.Commands.RecycleBin
89
{
9-
[Cmdlet(VerbsData.Restore, "PnPRecycleBinItem")]
10+
[Cmdlet(VerbsData.Restore, "PnPRecycleBinItem", DefaultParameterSetName = PARAMETERSET_ALL)]
1011
[CmdletHelp("Restores the provided recycle bin item to its original location",
12+
SupportedPlatform = CmdletSupportedPlatform.All,
1113
Category = CmdletHelpCategory.RecycleBin)]
1214
[CmdletExample(
1315
Code = @"PS:> Restore-PnpRecycleBinItem -Identity 72e4d749-d750-4989-b727-523d6726e442",
@@ -17,40 +19,72 @@ namespace SharePointPnP.PowerShell.Commands.RecycleBin
1719
Code = @"PS:> Get-PnPRecycleBinItem | ? -Property LeafName -like ""*.docx"" | Restore-PnpRecycleBinItem",
1820
Remarks = "Restores all the items in the first and second stage recycle bins to their original location of which the filename ends with the .docx extension",
1921
SortOrder = 2)]
22+
#if !SP2013
23+
[CmdletExample(
24+
Code = @"PS:> Restore-PnPRecycleBinItem -All -RowLimit 10000",
25+
Remarks = "Permanently restores up to 10,000 items in the recycle bin",
26+
SortOrder = 4)]
27+
#endif
2028

2129
public class RestoreRecycleBinItem : PnPSharePointCmdlet
2230
{
23-
[Parameter(Mandatory = true, HelpMessage = "Id of the recycle bin item or the recycle bin item object itself to restore", ValueFromPipeline = true, ParameterSetName = "Identity")]
31+
const string PARAMETERSET_ALL = "All";
32+
const string PARAMETERSET_IDENTITY = "Identity";
33+
34+
[Parameter(Mandatory = true, HelpMessage = "Id of the recycle bin item or the recycle bin item object itself to restore", ValueFromPipeline = true, ParameterSetName = PARAMETERSET_IDENTITY)]
2435
public RecycleBinItemPipeBind Identity;
2536

26-
[Parameter(Mandatory = true, HelpMessage = "If provided all items will be stored ", ValueFromPipeline = true, ParameterSetName = "All")]
37+
[Parameter(Mandatory = false, HelpMessage = "If provided all items will be stored ", ValueFromPipeline = true, ParameterSetName = PARAMETERSET_ALL)]
38+
[Obsolete("No need to add the -All parameter anymore")]
2739
public SwitchParameter All;
2840

29-
[Parameter(Mandatory = false, HelpMessage = "If provided, no confirmation will be asked to restore the recycle bin item")]
41+
[Parameter(Mandatory = false, HelpMessage = "If provided, no confirmation will be asked to restore the recycle bin item", ParameterSetName = PARAMETERSET_IDENTITY)]
42+
[Parameter(Mandatory = false, HelpMessage = "If provided, no confirmation will be asked to restore the recycle bin item", ParameterSetName = PARAMETERSET_ALL)]
3043
public SwitchParameter Force;
3144

45+
#if !SP2013
46+
[Parameter(Mandatory = false, HelpMessage = "Limits restoration to specified number of items", ParameterSetName = PARAMETERSET_ALL)]
47+
public int RowLimit;
48+
#endif
3249

3350
protected override void ExecuteCmdlet()
3451
{
35-
if (ParameterSetName == "Identity")
36-
{
37-
var recycleBinItem = Identity.GetRecycleBinItem(ClientContext.Site);
38-
39-
if (Force ||
40-
ShouldContinue(string.Format(Resources.RestoreRecycleBinItem, recycleBinItem.LeafName),
41-
Resources.Confirm))
42-
{
43-
recycleBinItem.Restore();
44-
ClientContext.ExecuteQueryRetry();
45-
}
46-
}
47-
else
52+
switch (ParameterSetName)
4853
{
49-
if (Force || ShouldContinue(Resources.RestoreRecycleBinItems, Resources.Confirm))
50-
{
51-
ClientContext.Site.RecycleBin.RestoreAll();
52-
ClientContext.ExecuteQueryRetry();
53-
}
54+
case PARAMETERSET_IDENTITY:
55+
var recycleBinItem = Identity.GetRecycleBinItem(ClientContext.Site);
56+
57+
if (Force || ShouldContinue(string.Format(Resources.RestoreRecycleBinItem, recycleBinItem.LeafName), Resources.Confirm))
58+
{
59+
recycleBinItem.Restore();
60+
ClientContext.ExecuteQueryRetry();
61+
}
62+
break;
63+
64+
case PARAMETERSET_ALL:
65+
#if !SP2013
66+
if (ParameterSpecified(nameof(RowLimit)))
67+
{
68+
if (Force || ShouldContinue(Resources.RestoreRecycleBinItems, Resources.Confirm))
69+
{
70+
RecycleBinItemCollection items = ClientContext.Site.GetRecycleBinItems(null, RowLimit, false, RecycleBinOrderBy.DeletedDate, RecycleBinItemState.None);
71+
ClientContext.Load(items);
72+
ClientContext.ExecuteQueryRetry();
73+
74+
items.RestoreAll();
75+
ClientContext.ExecuteQueryRetry();
76+
}
77+
}
78+
else
79+
#endif
80+
{
81+
if (Force || ShouldContinue(Resources.RestoreRecycleBinItems, Resources.Confirm))
82+
{
83+
ClientContext.Site.RecycleBin.RestoreAll();
84+
ClientContext.ExecuteQueryRetry();
85+
}
86+
}
87+
break;
5488
}
5589
}
5690
}

0 commit comments

Comments
 (0)