@@ -109,9 +109,9 @@ internal static List<RecycleBinItemCollection> GetRecycleBinItemCollection(Clien
109109 return recycleBinItems ;
110110 }
111111
112- internal static void RestoreRecycleBinItemInBulk ( HttpClient httpClient , ClientContext ctx , string [ ] idsList )
112+ internal static void RestoreRecycleBinItemInBulk ( HttpClient httpClient , ClientContext ctx , string [ ] idsList , RecycleBin . RestoreRecycleBinItem restoreRecycleBinItem )
113113 {
114-
114+ //restoreRecycleBinItem provides us the reference to the instance of RestoreRecycleBinItem object. We use this object to log key information as verbose
115115 Uri currentContextUri = new Uri ( ctx . Url ) ;
116116 string apiCall = $ "{ currentContextUri } /_api/site/RecycleBin/RestoreByIds";
117117
@@ -120,26 +120,43 @@ internal static void RestoreRecycleBinItemInBulk(HttpClient httpClient, ClientCo
120120 try
121121 {
122122 string requestBody = $ "{{'ids':['{ idsString } ']}}";
123- REST . RestHelper . Post ( httpClient , apiCall , ctx , requestBody , "application/json" , "application/json" ) ;
123+ REST . RestHelper . Post ( httpClient , apiCall , ctx , requestBody , "application/json" , "application/json" ) ;
124+ restoreRecycleBinItem . WriteVerbose ( "Whole batch restored successfuly." ) ;
124125 }
125- catch
126+ catch ( Exception ex )
126127 {
127- //fall back logic
128- //Unable to process as batch because of an error in restoring one of the ids in batch, processing individually
129- foreach ( string id in idsList )
130128 {
131- try
132- {
133- string requestBody = $ "{{' ids':[' { id } ']}}" ;
134- REST . RestHelper . Post ( httpClient , apiCall , ctx , requestBody , "application/json" , "application/json ") ;
129+ //fall back logic
130+ //Unable to process as batch because of an error in restoring one of the ids in batch, processing individually
131+ restoreRecycleBinItem . WriteVerbose ( $ "Unable to process as batch because of an error in restoring one of the ids in batch. Error: { ex . Message } " ) ;
132+ restoreRecycleBinItem . WriteVerbose ( $ "Switching to individul restore of items ... ") ;
135133
136- }
137- catch
134+ foreach ( string id in idsList )
138135 {
139- //Digest errors because they can be due to to the following two reasons and we cannot do anything
140- //1. Item with the same name already exists
141- //2. Item is no longer in recycle bin / Previously restored";
142-
136+ try
137+ {
138+ string requestBody = $ "{{'ids':['{ id } ']}}";
139+ REST . RestHelper . Post ( httpClient , apiCall , ctx , requestBody , "application/json" , "application/json" ) ;
140+ restoreRecycleBinItem . WriteVerbose ( $ "Item - { id } restored successfuly.") ;
141+
142+ }
143+ catch ( Exception e )
144+ {
145+ var odataError = e . Message ;
146+ if ( odataError != null )
147+ {
148+ if ( odataError . Contains ( "Value does not fall within the expected range." ) )
149+ {
150+ restoreRecycleBinItem . WriteVerbose ( $ "Item - { id } already restored.") ;
151+ }
152+ else
153+ {
154+ //Most common reason is that an item with the same name already exists. To restore the item, rename the existing item and try again
155+ restoreRecycleBinItem . WriteVerbose ( $ "Item - { id } restore failed. Error:{ odataError } ") ;
156+ }
157+ }
158+ //Digest errors because we cannot do anything
159+ }
143160 }
144161 }
145162 }
0 commit comments