Versioning and SharePoint: the Powershell perspective (part 2)

Enable/disable versioning for all lists and libraries in one site

Part 1 Overview and Step-by-step commentary                                                                                                                                                                                    Part 3 Tweaks and variations

The previous examples illustrated how to enable major and minor or only major versioning using Powershell and CSOM. The whole point of automation, however, is when we can do a lot of something. It hardly makes sense to go into all that trouble of writing a function if we intend to use it only once for one library. A faster and more efficient way would be through the User Interface. It DOES, however, make sense to write a few lines if they will handle a hundred lists or libraries that we have created in our site collection. That goes both for disabling or enabling the option, enabling the major versioning or major and minor. 

For detailed script description please refer here. The following snippets require Powershell ISE and SharePoint Online SDK. For detailed instruction with screenshots on how to install SharePoint Online SDK and run Powershell ISE please refer here.

Enable major versions for all lists

The data for our function. Notice that we no longer use the name of the list. It is because this time the script targets all the lists:

# Paths to SDK. Please verify location on your computer.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
# Insert the credentials and the name of the site and the desired setting: $true or $false
$Username="trial@trialtrial123.onmicrosoft.com"
$AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString
$Url="https://trialtrial123.sharepoint.com/sites/teamsitewithlists"
$Versioning=$true

The function itself, parameters + the initial .ExecuteQuery() to verify the credentials

function Set-SPOListsVersioning
{
param (
  [Parameter(Mandatory=$true,Position=1)]
        [string]$Username,
        [Parameter(Mandatory=$true,Position=2)]
        $password,
        [Parameter(Mandatory=$true,Position=3)]
        [string]$Url,
        [Parameter(Mandatory=$true,Position=4)]
        [bool]$Versioning
)
 
 
  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
  $ctx.ExecuteQuery()

Now the tricky part. We need to retrieve all the lists, so we cannot use .GetbyTitle() method. Instead let's just save the lists from the site in a variable and load that variable.

$Lists=$ctx.Web.Lists
$ctx.Load($Lists)
$ctx.ExecuteQuery()

It has loaded. All the lists and libraries in the site are stored in the $lists variable (Powershell is not case-sensitive, so it makes no difference whether I call it $lists or $Lists variable). You want to change the $versioning setting for each of them and for looping through each of something there is foreach loop  (for loop also possible here).

Foreach($ll in $Lists)
{

The rest of the code follows as described here:

Foreach($ll in $Lists)
{
    $ll.EnableVersioning = $Versioning
    $ll.Update()
     
 
    try
    {
        $ctx.ExecuteQuery()
        Write-Host $ll.Title "   Done" -ForegroundColor Green
       }
 
       catch [Net.WebException]
        {
             
            Write-Host "Failed" $_.Exception.ToString() -ForegroundColor Red
        }
 
}
Set-SPOListsVersioning -Username $Username -password $AdminPassword -Url $Url -Versioning $Versioning

Full script is available for download at TechNet Gallery.

Enable minor versions

List vs Library

Let me briefly divagate here and, since the series deals with lists' and libraries' setting, describe shortly why it does not matter whether only the word "list" is used for the code below and why it does.

The library is a type of list.1 As another form of a list, it has many similar properties to a standard list, but it also has built-in capabilities to handle storing files, such as additional functions to enable document uploads, retrieval, and other functions to support document management and collaboration. This includes version and new file template features.2

From a programmatic perspective, there is not much difference between a list and a library up to a certain point. While creating a list we use :

 

$lci =New-Object Microsoft.SharePoint.Client.ListCreationInformation
  $lci.Description="Some Description"
  $lci.Title="MyTitle"
  $lci.Templatetype=100
$list = $ctx.Web.Lists.Add($lci)
$ctx.ExecuteQuery()

when creating a document library

 

$lci =New-Object Microsoft.SharePoint.Client.ListCreationInformation
  $lci.Description="Some Description"
  $lci.Title="MyTitle"
  $lci.Templatetype=101
$list = $ctx.Web.Lists.Add($lci)
$ctx.ExecuteQuery()

while a task list will be

 

$lci =New-Object Microsoft.SharePoint.Client.ListCreationInformation
  $lci.Description="Some Description"
  $lci.Title="MyTitle"
  $lci.Templatetype=107
$list = $ctx.Web.Lists.Add($lci)
$ctx.ExecuteQuery()

As you can see the only difference is the Template type property.

 

Both lists and libraries can be retrieved by id using the same method

 

$lists=$ctx.Web.Lists.GetByID("26534EF9-AB3A-4770-AE56-EFF168BE562F")

A very interesting article on how to find the list GUID can be found here 

 

or by title

 

$MyList=$ctx.Web.Lists.GetByTitle("Title")

Therefore we can establish that programmatically there is no difference between list and library and the same properties and methods can be applied to all of them.

 

Really?

 

Well, no. Every library is a list, not every list is a library. While picture library is a type of list, template 109, a task list will never be a library. That means that the properties and methods which are available in general for the lists, such as retrieving list by title or id, adding or removing items, will also be available for the document library. Actions typical for the document library, such as file upload will not be available for the list. One of such methods or properties is minor versioning and here is where my little divagation plays an important role.

Script

The script below targets all lists in a site, which means lists and libraries. Only libraries support minor versioning. That means that all other list types will throw us an error.

There are several ways to deal with the issue. Firstly we can check the template type before trying to enable the minor versioning so that we enable it only for supported libraries. Secondly, we can use -SilentlyContinue parameter and ask Powershell not to notify us about any errors.

Personally, I do not like not being informed about the errors, I even enjoy them and find Powershell error messages particularly descriptive and useful (plus the red color adds vividity to the black console :) ), so I am going to say no to -SilentlyContinue. While checking for template type we need to be careful to retrieve the property correctly and not to omit any of the supported libraries, so for the purposes of my script, I will say no as well. Every script has a slightly different purpose though, so feel free to tweak it to your needs.

#
# Created by Someone Awesome, 2015 
#
 
function Set-SPOListsVersioning
{
param (
  [Parameter(Mandatory=$true,Position=1)]
        [string]$Username,
        [Parameter(Mandatory=$true,Position=2)]
        $password,
        [Parameter(Mandatory=$true,Position=3)]
        [string]$Url,
        [Parameter(Mandatory=$true,Position=4)]
        [bool]$Versioning
)
 
 
  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
  $ctx.ExecuteQuery() 
 
$Lists=$ctx.Web.Lists
$ctx.Load($Lists)
$ctx.ExecuteQuery()
 
Foreach($ll in $Lists)
{
    $ll.EnableMinorVersions = $Versioning
    $ll.Update()
     
 
    try
    {
        $ctx.ExecuteQuery()
        Write-Host $ll.Title  "   Done" -ForegroundColor Green
       }
 
       catch [Net.WebException] 
        {
             
            Write-Host "Failed" $_.Exception.ToString() -ForegroundColor Red
        }
 
}
}
 
 
 
 
 
 
 
 
 
 
# Paths to SDK. Please verify location on your computer.
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
# Insert the credentials and  the name of the site and the desired setting: $true or  $false
$Username="trial@trialtrial125.onmicrosoft.com"
$AdminPassword=Read-Host -Prompt "Enter password"  -AsSecureString
$Url="https://trialtrial125.sharepoint.com/sites/teamsitewithlibraries"
$Versioning=$true
 
 
 
 
Set-SPOListsVersioning -Username $Username -password $AdminPassword  -Url $Url -Versioning $Versioning

You can find the full script available for download in TechNet Gallery here.

As mentioned above you need to expect some errors during execution (though not terminating errors) thrown by the lists which do not support minor versions.

For visibility, we can include the list title in the catch statement

try
    {
        $ctx.ExecuteQuery()
        Write-Host $ll.Title  "   Done" -ForegroundColor Green
       }
 
       catch [Net.WebException] 
        {
             
            Write-Host $ll.Title  "Failed" $_.Exception.ToString() -ForegroundColor Red
        }

Part 1 Overview and step-by-step commentary
Part 3 Tweaks and variations

Other Languages

SharePoint I historia wersji z perspektywy Powershell'a (pl-PL) (część 2)

http://c.statcounter.com/10342265/0/2c6600ab/1/