打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何删除SharePoint文件的某个版本

原文地址:

https://blogs.msdn.microsoft.com/chjain/2012/10/05/sharepointhow-to-delete-versions-of-documents-in-a-document-library-keeping-top-n-versions/


I have this requirement coming to me multiple times where people enable versioning on the Document Library, but never keep an upper limit of number of versions to be retained. This results in an unmanageable number of versions created for the document in the SharePoint library. If the number is too high, deleting all the versions through the UI also results in time out. Hence the recommendation is to keep a check on the number of versions you want to retain and configure that in the Versioning Settings for the library.

In this post, I’ll share the code to delete the old versions of a Document in a Document library and retaining the Top N versions.

1. Create a new Console Application Project using Visual Studio. You can use either 2008 or 2010 depending on the SharePoint version.

2. Add the reference to the Microsoft.SharePoint.dll

3. Make Sure to change the Platform target to “Any CPU”. You can do that by going to Build tab in the Project Properties window.

4. In the class where the Main Method is defined, add the following code below and call the method from the Main method.

static void Main(string[] args)
        {
            ProcessRequest(args);
            Console.ReadLine();
        }
        private static void ProcessRequest(string[] args)
        {
            string siteURL;
            string webURL;
            string ListName;
            string versionsToKeep;
            getValues(args, out siteURL, out webURL, out ListName, out versionsToKeep);
            Console.WriteLine("Site URL: " + siteURL);
            Console.WriteLine("Web URL: " + webURL);
            Console.WriteLine("List Name: " + ListName);
            Console.WriteLine("Versions To Keep: " + versionsToKeep);
            FileStream fs1 = new FileStream("Log.txt", FileMode.Append);
            // First, save the standard output.
            TextWriter tmp = Console.Out;
            StreamWriter sw1 = new StreamWriter(fs1);
            sw1.AutoFlush = true;
            TextWriter dual = new DualWriter(sw1, tmp);
            Console.SetOut(dual);
            try
            {
                DeleteVersionHistory(siteURL, webURL, ListName, int.Parse(versionsToKeep));
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception Occurred during processing...");
                Console.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
            }
            finally
            {
                sw1.Close();
                Console.SetOut(tmp);
            }
        }
        private static void getValues(string[] args, out string siteURL, out string webURL, out string ListName, out string versionsToKeep)
        {
            siteURL = string.Empty;
            webURL = string.Empty;
            ListName = string.Empty;
            versionsToKeep = string.Empty;
            if (args.Length == 4)
            {
                siteURL = args[0];
                webURL = args[1];
                ListName = args[2];
                versionsToKeep = args[3];
            }
            else
            {
                Console.WriteLine("Arguments are missing ...");
                Console.WriteLine("Please provide the values below...");
                Console.Write("Site URL: ");
                siteURL = Console.ReadLine();
                Console.WriteLine();
                Console.Write("Web URL: ");
                webURL = Console.ReadLine();
                Console.WriteLine();
                Console.Write("List Name: ");
                ListName = Console.ReadLine();
                Console.WriteLine();
                Console.Write("Versions To Keep: ");
                versionsToKeep = Console.ReadLine();
            }
        }
        
            
                
        private static void DeleteVersionHistory(string siteURL, string webURL, string listname, int versionToKeep)
        {
            Console.WriteLine("Started the code to delete the versions.. ");
            #region SPSite
            using (SPSite spSite = new SPSite(siteURL))
            {
                Console.WriteLine("Found the Site instance..");
                #region SPWeb
                using (SPWeb spWeb = spSite.OpenWeb(webURL))
                {
                    Console.WriteLine("Found the Web instance..");
                    SPList list = spWeb.Lists[listname];
                    Console.WriteLine("Found the List instance..");
                    #region DocumentLibrary
                    if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
                    {
                        SPDocumentLibrary library = (SPDocumentLibrary)list;
                        foreach (SPListItem item in list.Items)
                        {
                            SPFile file = item.File;
                            Console.WriteLine("Deleting versions for Item with ID: {0} Name {1}", item.ID, item.Name);
                            int verCount = item.Versions.Count;
                            if (verCount - versionToKeep < 0)
                            {
                                Console.WriteLine("Versions Count is Less than the specified versions to keep ");
                            }
                            else
                            {
                                do
                                {
                                    try
                                    {
                                        Console.WriteLine("File version {0}, Item Version {1} ", file.Versions[0].VersionLabel, item.Versions[item.Versions.Count - 1].VersionLabel);
                                        item.Versions[item.Versions.Count – 1].Delete();
                                        file.Versions[0].Delete();
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.WriteLine("Exception occured deleting.......");
                                        Console.WriteLine(ex.Message + System.Environment.NewLine + ex.StackTrace);
                                    }
                                } while (file.Versions.Count > versionToKeep || item.Versions.Count > versionToKeep + 1);
                            }
                        }
                    }
                    #endregion DocumentLibrary
                   
                }
                #endregion SPWeb
            }
            #endregion SPSite
            Console.WriteLine("Exiting the code to delete the versions.. ");
        }

5. In the same project Add a new class file and add the class code below:

public class DualWriter : TextWriter
    {
        private TextWriter one;
        private TextWriter two;
        public DualWriter(TextWriter A, TextWriter B)
        {
            one = A;
            two = B;
        }
        public override System.Text.Encoding Encoding
        {
            get
            {
                return one.Encoding;
            }
        }
        public override void Write(char c)
        {
            one.Write(c);
            two.Write(c);
        }
        public override void WriteLine(string value)
        {
            one.WriteLine(value);
            two.WriteLine(value);
        }
    }

6. The above class file is required as I am writing the Console Output to the console window as well as to a Log file which would be created if not present where the .exe for the console application would reside.

7. The Input parameters for the code to delete the versions are:

a. The Site Collection URL

b. The Sub Site relative URL

c. The Document Library Name

d. The number of latest versions to keep

8. The parameters can be passed as argument line parameters and if not supplied, the code would ask for those parameter values at run time.

9. Build the project and run the .exe to delete the old versions of each items in the Document Library

10. Test it on a Development Server before running it on any production Server.

 

Quote of the Day:

The company’s most urgent task is to learn to welcome, beg for, demand – innovation from everyone

–Tom Peters

Disclaimer: The code sample above is provided is as is and the owner holds no responsibility for any issues if any arrive.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#中的深复制和浅复制(在C#中克隆对象)
[C#]Main(String[] args)参数输入问题
Task类使用总结
C# Lambda表达式详解,及Lambda表达式树的创建
C#入门代码 - C Programmer's Cookbook - C 博客
C#字符串操纵总结
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服