24 June 2011

Enable Versioning on SharePoint List via PowerShell

In this example all Lists in all SubWebs titled with "Important Information" should be versioned.

In this case a little PowerShell Script will help:
<#  
.SYNOPSIS  
    Enable Versioning in a specific List all SubWebs under a SiteCollection
.DESCRIPTION  
    Enable Versioning in a specific List all SubWebs under a SiteCollection
.NOTES  
    Author     : Daniel Sirz
.LINK  
    http://www.enterprisebugs.com  
#>  

$siteCollection = Get-SPSite 
$specificList   = "Important Information"

# Iterate over all Webs in the SiteCollection
foreach($web in $siteCollection.AllWebs) {
 # Look in all Lists for the title
 foreach ($list in $web.Lists) {
  # When the List is found
  if($list.Title -eq $specificList) {
   # Enable Versioning
   $list.EnableVersioning = $true
   $list.Update()
  }
 }
}

If we browse to on of the lists and have a look at the Versioning Setting we see that Versioning is now enabled.

Enabling minor versions in a website

If you want to turn on Minor Versions in a specific web you may use this piece of code:
$web = Get-SPWeb 
$list = $web.Lists["Important Information"]

# Enable Minor Versioning
$list.EnableMinorVersions = $true
$list.Update()   

This should look something like this in the list settings page:

Limit the number of versions to retain

Last but not least if you want to specify the number of versions to retain you can use this piece of code and combine it like you want.
$web = Get-SPWeb 
$list = $web.Lists["Important Information"]

$list.EnableMinorVersions = $true
$list.MajorVersionLimit = 5
$list.MajorWithMinorVersionsLimit = 10
$list.Update()   

This looks like this:

0 comments:

Post a Comment