3
Answers

Enable versioning on site collection and subsites

mgm tech

mgm tech

7y
253
1
I have a script that enable a site collection library versioning. However, I need help on enable versioning not just at the site collection level but subsite and sub-subsite. Please help.
 
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()
$ctx.Load($ctx.Web.Lists)
$ctx.Load($ctx.Web)
$ctx.Load($ctx.Web.Webs)
$ctx.ExecuteQuery()
Foreach($ll in $ctx.Web.Lists)
{
if ($ll.BaseType -eq "DocumentLibrary")
{
$ll.EnableVersioning = $Versioning
$ll.MajorVersionLimit = 200
$ll.Update()
}
try
{
$ctx.ExecuteQuery()
if ($ll.BaseType -eq "DocumentLibrary") {
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="[email protected]"
$AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString
$Url="https://test.sharepoint.com/sites/test1"
$Versioning=$true
Set-SPOListsVersioning -Username $Username -password $AdminPassword -Url $Url -Versioning $Versioning
 
Answers (3)