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

Sathish Nadarajan
 
Solution Architect
October 6, 2017
 
Rate this article
 
Views
4607

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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment