Advertisement
PtiTom

Purge Lists CSOM SharePoint

Nov 5th, 2015 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. private void Purge()
  2. {
  3.     try
  4.     {
  5.         using (ClientContext ctx = new ClientContext(sharePointSiteUrl))
  6.         {
  7.             ctx.Credentials = new SharePointOnlineCredentials(userName, password);
  8.  
  9.             var web = ctx.Web;
  10.             ctx.Load(web, w => w.Title);
  11.             ctx.ExecuteQuery();
  12.             this.statusLabel.Text = string.Format("Purge '{0}'...", web.Title);
  13.             var listsToPurge = <Ensemble des listes à purger>;
  14.             int totalItemCount = this.spListsList.CheckedItems.Cast<MyListItem>().Sum(i => i.ItemCount);
  15.             this.progressBar.Value = 0;
  16.             this.progressBar.Maximum = totalItemCount;
  17.             foreach (MyListItem listToPurge in listsToPurge)
  18.             {
  19.                 this.PurgeList(ctx, listToPurge);
  20.             }
  21.  
  22.             var lists = web.Lists;
  23.             ctx.Load(lists, ls => ls.Include(l => l.Title), ls => ls.Include(l => l.ItemCount));
  24.             ctx.ExecuteQuery();
  25.  
  26.             this.spListsList.Items.Clear();
  27.             foreach (List list in lists)
  28.             {
  29.                 this.spListsList.Items.Add(new MyListItem() { Title = list.Title, ItemCount = list.ItemCount });
  30.             }
  31.         }
  32.     }
  33.     catch (Exception e)
  34.     {
  35.         MessageBox.Show(string.Format("Exception has occured : {0}", e), "Exception in purging", MessageBoxButtons.OK, MessageBoxIcon.Error);
  36.     }
  37. }
  38.  
  39. private void PurgeList(ClientContext ctx, MyListItem listToPurge)
  40. {
  41.     try
  42.     {
  43.         List spList = ctx.Web.Lists.GetByTitle(listToPurge.Title);
  44.         ctx.Load(spList);
  45.         CamlQuery itemsIdQuery = CamlQuery.CreateAllItemsQuery(50, new string[] { "Id" });
  46.  
  47.         int lastIdWithException = -1;
  48.         int maxBatchNumber = 1000;
  49.  
  50.         while (--maxBatchNumber > 0)
  51.         {
  52.             var itemBatch = spList.GetItems(itemsIdQuery);
  53.             ctx.Load(itemBatch);
  54.             ctx.ExecuteQuery();
  55.  
  56.             if (itemBatch.Count == 0)
  57.             {
  58.                 return;
  59.             }
  60.  
  61.             foreach (var item in itemBatch)
  62.             {
  63.                 try
  64.                 {
  65.                     this.DeleteItemAndAttachments(ctx, spList, item.Id);
  66.                 }
  67.                 catch (Exception)
  68.                 {
  69.                     if (item.Id == lastIdWithException)
  70.                     {
  71.                         throw;
  72.                     }
  73.  
  74.                     lastIdWithException = item.Id;
  75.                 }
  76.  
  77.                 progressBar.PerformStep();
  78.                 Application.DoEvents();
  79.             }
  80.         }
  81.  
  82.         //ctx.ExecuteQuery();
  83.     }
  84.     catch (Exception e)
  85.     {
  86.         MessageBox.Show(string.Format("Exception has occured : {0}", e), "Exception in purging list", MessageBoxButtons.OK, MessageBoxIcon.Error);
  87.     }
  88. }
  89.  
  90. private void DeleteItemAndAttachments(ClientContext ctx, List spList, int id)
  91. {
  92.     ListItem itemToDelete = spList.GetItemById(id);
  93.     ctx.Load(itemToDelete, i => i.AttachmentFiles);
  94.     ctx.ExecuteQuery();
  95.  
  96.     for (int attachmentIndex = itemToDelete.AttachmentFiles.Count - 1; attachmentIndex >= 0; attachmentIndex--)
  97.     {
  98.         itemToDelete.AttachmentFiles[attachmentIndex].DeleteObject();
  99.     }
  100.  
  101.     itemToDelete.DeleteObject();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement