Delete the File old version history using CSOM in Sharepoint 2013

suresh p 1 Reputation point
2021-03-23T18:19:41.207+00:00

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 Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,675 questions
{count} votes