question

sureshp-5798 avatar image
0 Votes"
sureshp-5798 asked MichaelHan-MSFT commented

Delete the File old version history using CSOM in Sharepoint 2013

Hi,

I used this code for deleting the file old version history,


    public static void DeleteMinorVersions()
     {
         var siteUrl = "SiteURL";
         // string username = "username";
         //Console.WriteLine("Enter password");
         // SecureString password = GetPasswordOfYourSite();
         using (var clientContext = new ClientContext(siteUrl))
         {
             try
             {
                 // clientContext.Credentials = new SharePointOnlineCredentials(username, password);

                 List list = clientContext.Web.Lists.GetByTitle("DocuemntLibName");

                 //Get the Child Item - The IDs were Hard coded for the demo purpose
                 ListItem listItem = list.GetItemById(9);
                 clientContext.Load(list);
                 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("1.3"))
                         {
                             listItem.File.Versions.DeleteByLabel(ver.VersionLabel);
                             clientContext.ExecuteQuery();
                             //Console.ReadKey(ver.VersionLabel);
                             string versionno = ver.VersionLabel.ToString();
                             Console.WriteLine(versionno);
                             System.Console.ReadLine();

                         }

                     }
                 }

             }
             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();
     }
sharepoint-dev
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @sureshp-5798 ,

Did you have some issues with the code? Or you just shared your code to help others?

0 Votes 0 ·

0 Answers