- 
                Notifications
    You must be signed in to change notification settings 
- Fork 391
Extended Restore-PnPRecycleBinItem functionality to efficiently restore selected set of items in bulk #4705
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
base: dev
Are you sure you want to change the base?
Changes from 17 commits
8876941
              105c5cc
              92e9aa0
              3cd6226
              b2f2915
              b6e02c4
              3b0d48c
              6ad632a
              7b4a585
              c4cbe36
              aa6dd0e
              6c4fbef
              57392bb
              eb87645
              86edc17
              f535379
              7a5f74d
              d2db153
              6fa0f3c
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| { | ||
| "profiles": { | ||
| "profiles": { | ||
| "PnP.PowerShell-Module": { | ||
| "commandName": "Executable", | ||
| "executablePath": "pwsh", | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,7 +1,13 @@ | ||
| using Microsoft.SharePoint.Client; | ||
| using Newtonsoft.Json; | ||
| using PnP.PowerShell.Commands.Model.Mail; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Management.Automation; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| using System.Xml.Linq; | ||
|  | ||
| namespace PnP.PowerShell.Commands.Utilities | ||
| { | ||
|  | @@ -99,8 +105,61 @@ internal static List<RecycleBinItemCollection> GetRecycleBinItemCollection(Clien | |
| } | ||
| } | ||
| while (items?.Count == 5000); | ||
|  | ||
| return recycleBinItems; | ||
| } | ||
|  | ||
| internal static void RestoreRecycleBinItemInBulk(HttpClient httpClient, ClientContext ctx, string[] idsList, RecycleBin.RestoreRecycleBinItem restoreRecycleBinItem) | ||
| { | ||
| //restoreRecycleBinItem provides us the reference to the instance of RestoreRecycleBinItem object. We use this object to log key information as verbose | ||
| Uri currentContextUri = new Uri(ctx.Url); | ||
| string apiCall = $"{currentContextUri}/_api/site/RecycleBin/RestoreByIds"; | ||
|  | ||
| string idsString = string.Join("','", idsList); // Convert array to a comma-separated string | ||
|  | ||
| try | ||
| { | ||
| string requestBody = $"{{'ids':['{idsString}']}}"; | ||
| REST.RestHelper.Post(httpClient, apiCall, ctx, requestBody, "application/json", "application/json"); | ||
| 
      Comment on lines
    
      +122
     to 
      +123
    
   
     | ||
| restoreRecycleBinItem.WriteVerbose("Whole batch restored successfuly."); | ||
| 
     | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| { | ||
| //fall back logic | ||
| //Unable to process as batch because of an error in restoring one of the ids in batch, processing individually | ||
| restoreRecycleBinItem.WriteVerbose($"Unable to process as batch because of an error in restoring one of the ids in batch. Error:{ex.Message}"); | ||
| restoreRecycleBinItem.WriteVerbose($"Switching to individul restore of items ..."); | ||
|         
                  KoenZomers marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
|  | ||
| foreach (string id in idsList) | ||
| { | ||
| try | ||
| { | ||
| string requestBody = $"{{'ids':['{id}']}}"; | ||
| REST.RestHelper.Post(httpClient, apiCall, ctx, requestBody, "application/json", "application/json"); | ||
| 
      Comment on lines
    
      +138
     to 
      +139
    
   
     | ||
| restoreRecycleBinItem.WriteVerbose($"Item - {id} restored successfuly."); | ||
| 
     | ||
|  | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| var odataError = e.Message; | ||
| if (odataError != null) | ||
| { | ||
| if (odataError.Contains("Value does not fall within the expected range.")) | ||
| { | ||
| restoreRecycleBinItem.WriteVerbose($"Item - {id} already restored."); | ||
| } | ||
| else | ||
| { | ||
| //Most common reason is that an item with the same name already exists. To restore the item, rename the existing item and try again | ||
|         
                  namwar marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
| restoreRecycleBinItem.WriteVerbose($"Item - {id} restore failed. Error:{odataError}"); | ||
| } | ||
| } | ||
| //Digest errors because we cannot do anything | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.