How to delete versions without having column name in sharepoint list

走远了吗. 提交于 2019-12-13 09:27:56

问题


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..

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

回答1:


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;

    }


来源:https://stackoverflow.com/questions/7144633/how-to-delete-versions-without-having-column-name-in-sharepoint-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!