Flushing versions from SharePoint document libraries

Say a SharePoint document library has documents with over 10 versions each.

You set the version limit to 5 and now what to flush the versions past 5.

Setting the new limit doesn't flush the old versions.  Only making a new version will.  But that will disturb the metadata unnecessarily.

Is there a way to flush the extraneous versions?  Stsadm, webservice, or any other method?  I'm able to identify the guid for the doc/version.
OCSDTechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

liebrandCommented:
You could write a utility that goes via the object model and executes a SPListItem.SystemUpdate() method against each document.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.systemupdate.aspx

This will perform an update on the list item, purge the versions, but keep the metadata, modified by, etc intact.

Test this process in a sandbox environment and insure you get your desired results.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OCSDTechAuthor Commented:
I used some PowerShell from quihong and the SystemUpdate() method fro liebrand to come up with the following powershell script.

Set the $WebAppURL to your web app.

The script looks for items with greater than 5 versions.  Each item will be "touched" without updating the metadata to trigger the extraneous versions to be removed.

Removing the # and editing the $web.URL limits the script to a site.

Removing the ## activates the $item.SystemUpdate ().

Test before using.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null
# Create the stopwatch
[System.Reflection.Assembly]::LoadWithPartialName("System.Diagnostics.Stopwatch") > $null
$sw = New-Object System.Diagnostics.StopWatch
$sw.Start()

$startime = Get-Date

$WebAppURL =  "http://sharepoint"
$theSite = New-Object Microsoft.SharePoint.SPSite($webAppURL)
$webApp = $theSite.WebApplication
$allSiteCollections = $webApp.Sites

foreach($site in $allSiteCollections)
{
	foreach($web in $site.AllWebs)
	{
		foreach($list in $web.Lists)
		{  
			if($list.EnableVersioning -and $list.Title -ne "Master Page Gallery" -and $list.Title -ne "Workflows" -and $list.GetType().Name -eq"SPDocumentLibrary")
			{
#            if ($web.URL -eq"http://sharepoint/subsite" )# -and $list.Title -eq "My Library")
#                {
                foreach($item in $list.items)
                	{		
                     if ($item.Versions.Count -gt 5)
                     {
                     Write-Host $item.Name   $item.Versions.Count
##                     $item.SystemUpdate()
                     }
                     
                    }    
#                }
			}	
		}		
		$web.Dispose()
	}
	$site.Dispose()
}
$Thesite.Dispose()

Open in new window

0
OCSDTechAuthor Commented:
Added powershell script to complete the solution.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SharePoint

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.