在版本历史中,我得到了一些重复的版本,没有任何字段更改....所以对于这些版本,列名称将为空。以编程方式我想删除版本历史中没有指定列名的版本... 请帮忙..
在图像中,您可以看到空白版本......我需要删除这些版本
答案 0 :(得分:3)
以下代码从列表项中删除版本。您可以重复使用它并将检查名称的条件添加为空。
///
/// 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;
}