SharePoint: Listing and Deleting SPListItemVersions Using PowerShell

SharePoint: Listing and Deleting SPListItemVersions Using PowerShell


 

Introduction


When versioning is turned on for a SharePoint list or document library, there are times when you may need to report on the number of list item versions, or delete versions that exceed a threshold. These scenarios can happen if;

1. Version limits (for the maximum number of major/minor versions) were not set, and the list has too many versions
2. Version limits are being applied or reduced, and you need to retrospectively delete all the versions in a list that exceed the new threshold*
3. You are performing an upgrade, and you want to prune the versions of pages in the pages library (on a publishing site)
4. You want to run a report to determine how many items in a list have more than "x" number of versions 

*When the version settings are changed on a list, existing versions on existing items in the list are not changed until the item is modified. Therefore, if you reduce the maximum number of versions to be kept for items in a list, the changes only take effect on new items, or when existing items are updated.

An easy way to accomplish these tasks is to use PowerShell to iterate over a lists items, inspecting each items versions. This article demostrates how to do this. 

Download The Script


There is a script that contains a number of functions for listing and deleting list item versions, and it can be downloaded from the Microsoft TechNet Gallery, here:  List or Delete excess ListItem Versions in a SharePoint List or Document Library

Example 1: Listing the versions of all list items in the http://corporate/pages library


1. Get the SPWeb that contains the list we want to insect
2. Get the SPList that we want to inspect from the SPWeb
3. Copy the SPListItemsCollection to a variable
4. Iterate over the SPListItemCollection, and list the number of versions of each item

$w = get-spweb "http://corporate"
$l = $w.Lists.TryGetList("Pages");
$items = $l.Items;
$f = $l.Fields["Check In Comment"];
$listType = $l.GetType().Name;
foreach($item in $items)
{
    $itemTitle = $item.Title;
    if($listType -eq "SPDocumentLibrary")
    {
        if($itemTitle -eq ""){$itemTitle = $item["Name"];}
    }  
    if($item.Versions.Count -gt 0){
        $vtr = $item.Versions.Count;
        Write-Host "$itemTitle, has $vtr versions" -foregroundcolor Green;
    }
}



Example 2: Delete all versions in excess of 5 (ignoring minor versions)


1. Get the SPWeb that contains the list we want to insect
2. Get the SPList that we want to inspect from the SPWeb
3. Copy the SPListItemsCollection to a variable
4. Iterate over the SPListItemCollection, and delete item versions that exceed 5 major versions

$w = get-spweb "http://corporate"
$l = $w.Lists.TryGetList("Pages");
$items = $l.Items;
$f = $l.Fields["Check In Comment"];
$listType = $l.GetType().Name;
foreach($item in $items)
{
    $itemTitle = $item.Title;
    if($listType -eq "SPDocumentLibrary")
    {
        if($itemTitle -eq ""){$itemTitle = $item["Name"];}
    }
    if($item.Versions.Count -gt 5){
        $vtr = $item.Versions.Count;
        Write-Host "$itemTitle, has $vtr versions" -foregroundcolor Green;
         
        while($vtr -gt 5){         
            $vtr--;
            [Microsoft.SharePoint.SPListItemVersion]$iv = $item.Versions[$vtr];
            $versionNumber = $iv.VersionLabel;
            if(!$iv.VersionLabel.EndsWith(".0"))
            {              
                continue;
            }          
            Write-Host "$itemTitle : Deleted version $versionNumber" -foregroundcolor Yellow;
            $iv.Delete();
        }
    }
}



Example 3: A more advanced example, listing the versions of all list items in the http://corporate/pages library including the comments, item author and version author


1. Get the SPWeb that contains the list we want to insect
2. Get the SPList that we want to inspect from the SPWeb
3. Get a reference to the "Check In Comments" field. This field is present on Document Libraries and will be used later to the value of each versions Check In Comment.
3. Copy the SPListItemsCollection to a variable
4. Iterate over the SPListItemCollection, and list the version information, by using the SPListItem.Versions property
5. For each version, cast the object returned from SPListItem.Version[index] to a Microsoft.SharePoint.SPListItemVersion object.
6. Using the Microsoft.SharePoint.SPListItemVersion object, get the version label (version number), the version author and the version comment. 
[Note] To get the version comment, you need to use the GetVersionFromID method of the SPListItem.File.Versions collection. This returns an SPFileVersion object, which contains the CheckInComment property.

$w = get-spweb "http://corporate"
$l = $w.Lists.TryGetList("Pages");
$items = $l.Items;
$f = $l.Fields["Check In Comment"];
$listType = $l.GetType().Name;
foreach($item in $items)
{
    $itemTitle = $item.Title;
    if($listType -eq "SPDocumentLibrary")
    {
        if($itemTitle -eq ""){$itemTitle = $item["Name"];}
    }  
    if($item.Versions.Count -gt 0){
        $vtr = $item.Versions.Count;
        $itemAuthor = ($item.Fields["Created By"]).GetFieldValueAsText($item["Created By"]);
        Write-Host "$itemTitle, has $vtr versions. Created By: $itemAuthor" -foregroundcolor Green;
         
        while($vtr -gt 0){         
            $vtr--;
            [Microsoft.SharePoint.SPListItemVersion]$iv = $item.Versions[$vtr];
            $versionNumber = $iv.VersionLabel;
            $versionAuthor = $iv.CreatedBy.User.DisplayName;
            $comment="";
            if($f -ne $null)
            {
                if($iv.IsCurrentVersion)
                {
                    $comment = "Comment: "+($f.GetFieldValueAsText($item.Versions.GetVersionFromID($iv.VersionId)["Check In Comment"])).Replace("`r`n"," ").Replace("`n"," ");             
                }
                else
                {
                    $comment = "Comment: "+($f.GetFieldValueAsText($item.File.Versions.GetVersionFromID($iv.VersionId).CheckInComment)).Replace("`r`n"," ").Replace("`n"," ");             
                }
            }                                  
            Write-Host ([String]::Format("$itemTitle, version: $versionNumber edited by: $versionAuthor {0}", $comment)) -foregroundcolor Cyan;
        }
    }
}



Questions were asked about managing list item versions in the following forum posts:
List Version History Programatically
Limit versioning is not effective

The original content for this article came from the blog of Matthew Yarlett, and can be viewed here: Listing and Deleting SharePoint SPListItem Versions Using PowerShell

Leave a Comment
  • Please add 1 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 3. Comment: Changed tag "Has Images" to "Has Image" (we want to standardize on mostly singular tags), removed extra space in tag "SharePoint  2010"

  • Matthew Yarlett edited Revision 1. Comment: Minor changes to wording. Added links.

  • Matthew Yarlett edited Original. Comment: Added images for the examples.

Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Matthew Yarlett edited Original. Comment: Added images for the examples.

  • Matthew Yarlett edited Revision 1. Comment: Minor changes to wording. Added links.

  • Nice article Matthew! Maybe adding a TOC?

  • Ed Price - MSFT edited Revision 2. Comment: Adding TOC

  • I added the TOC. Great article!

  • Thanks Ed, thanks Margriet!

  • Richard Mueller edited Revision 3. Comment: Changed tag "Has Images" to "Has Image" (we want to standardize on mostly singular tags), removed extra space in tag "SharePoint  2010"

Page 1 of 1 (7 items)