Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

c# - How to delete versions without having column name in sharepoint list

In version history i am getting some duplicate versions with out any field changed....So for those versions the column name will be empty. Programmatically i would like to delete the versions in the version history where no column name specified... Please help..

Version History In the image you can see the blank versions...I need to remove those versions

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The code below deletes versions from the list item. You can re-use it and add the condition of checking the name as empty.

///

    /// Removes unneeded versions from a sharepoint list item

    /// </summary>

    /// <param name="item">The SPListItem that needs some versions removed</param>

    /// <param name="minVersions">The minimum number of versions to keep</param>

    /// <param name="savedVersions">A collection of important version labels (or null)</param>

    /// <returns>The number of versions deleted</returns>

    internal static int RemoveVersions(SPListItem item, int minVersions, ICollection<string> savedVersions)

    {

        //  Homework for the reader: validate the input arguments.

        //  if item is null, throw an ArgumentNullException

        //  if minVersions < 0 throw an ArgumentOutOfRangeException



        int deletedCount = 0;

        int i = minVersions;    // start looking for old versions after skipping minVersions



        while (i < item.Versions.Count)

        {

            SPListItemVersion itemVersion = item.Versions[i];

            string versionLabel = itemVersion.VersionLabel;



            if (!itemVersion.IsCurrentVersion &&    // Not "current" according to SharePoint (e.g. last-published major version, moderated version)

                (savedVersions == null || !savedVersions.Contains(versionLabel)))  // not one of our "saved" versions

            {

                itemVersion.Delete();

                ++deletedCount;

            }

            else

            {

                ++i;

            }

        }



        return deletedCount;

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...