How to delete the Minor Versions of a Document Programmatically by C# CSOM


Sathish Nadarajan
SharePoint MVP
Published On :   06 Oct 2017
Visit Count
Today :  4    Total :   694
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


We were doing some migration and after the Migration, we were end up with a lot of Minor versions, which we did not create but the Migration tool created them. To remove the Minor versions, came up with a piece of script, thought of sharing to the community as well.

The below is the sample scenario.

clip_image002

I need to make sure that all the files are having only the Major Version. i.e., the File1.txt should be deleted with 1.1 and 1.2 Versions.

 using Microsoft.SharePoint.Client;
 using System;
 
 namespace Office365.Console
 {
     class Program
     {
         static void Main(string[] args)
         {
             DeleteMinorVersions();
         }
 
         public static void DeleteMinorVersions()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
             
             //Site URL
             string siteUrl = "https://*****.sharepoint.com/sites/communitysite";
             // UserName 
             string userName = "sathish@*******.onmicrosoft.com";
             //Password
             string password = "*********";
 
             
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 try
                 {
                     Site site = clientContext.Site;
                     Web web = clientContext.Web;
                     
                     clientContext.Load(web);
                     clientContext.Load(web.Lists);
                     clientContext.ExecuteQuery();
                     //Get the Tasks List
                     List list = web.Lists.GetByTitle("MyDocumentsLibrary");
 
                     //Get the Child Item - The IDs were Hard coded for the demo purpose
                     ListItem listItem = list.GetItemById(20);
                     clientContext.Load(listItem);
                     clientContext.Load(listItem.File);
                     clientContext.Load(listItem.File.Versions);
                     clientContext.ExecuteQuery();
                     
                     if (listItem.File.Versions.Count > 0)
                     {
                         foreach (var ver in listItem.File.Versions)
                         {
                             if (!ver.VersionLabel.Contains(".0"))
                             {
                                 listItem.File.Versions.DeleteByLabel(ver.VersionLabel);
                                 clientContext.ExecuteQuery();
                             }
 
                         }
                     }
 
                 }
                 catch (Exception ex)
                 {
                     System.Console.ForegroundColor = ConsoleColor.Red;
                     System.Console.WriteLine("Exception Occured : " + ex.Message);
                     System.IO.File.AppendAllText("C:\\Temp\\Exception.txt", ex.Message + " - " + siteUrl + Environment.NewLine);
                 }
 
 
             }
 
 
             System.Console.WriteLine("Completed....");
             System.Console.WriteLine("Press Any Key to Exit ....");
             System.Console.ReadLine();
         }
          
 
          
     }
 }
 
 

 

 

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Protect Your SharePoint