SharePoint Online: Enable Versioning on All List and Libraries using PowerShell

Requirement: Enable Versioning on all lists in SharePoint online site.

How to Enable Versioning on a List or Library in SharePoint?
Version History feature in SharePoint allows you to access previous saved copy of a document or list item. By default versioning is enabled on your document library in SharePoint Online. When a save operation happens, SharePoint creates new version along with metadata such as, who saved the item, when it was created, etc. To enable versioning for a list or library in SharePoint online,
  • Go to your document library >> Click the Library tab on the Ribbon >> Click on Library Settings.
  • Under general Settings, click Versioning Settings link.
  • From the Versioning Settings page, choose "Create major versions". Optionally, you can set a limit on number of versions.
    sharepoint online enable version history
  • This enables SharePoint online document library versioning. Once versioning feature is enabled, you can select a document or a list item and click on "Version History" from the context menu or from the Ribbon to view versions created.

SharePoint Online: PowerShell to Enable Versioning
Here is the PowerShell to enable version history on given list or library.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Enable-SPOVersioning()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )
    Try {
        #Setup Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
         
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        
        #sharepoint online powershell enable versioning
        $List.EnableVersioning = $True
        $List.MajorVersionLimit = 50
        $List.Update()
        $Ctx.ExecuteQuery() 
        Write-host -f Green "Versioning has been turned ON at $ListName"
 
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL="https://crescenttech.sharepoint.com"
$ListName="Projects"

#Call the function 
Enable-SPOVersioning -SiteURL $SiteURL -ListName $ListName

PowerShell script to enable versioning on all lists and libraries in SharePoint Online:
Lets activate version history on all lists and libraries of a SharePoint Online site.
#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
   
##Variables for Processing
$SiteUrl = "https://portal.crescent.com.com/sites/Sales/"
$UserName="[email protected]"
$Password ="Password goes here"

try{  
    #Setup the context
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $credentials
    
    #get the site and lists
    $web = $context.Web
    $context.Load($web)
    $context.load($web.lists)
    $Context.executeQuery() 
 
    #Iterate through each list in the web
    foreach($list in $web.lists){
    if ($list.hidden -eq $false)
    {
        #Enable versioning
        $list.EnableVersioning = $true
        $lIST.MajorVersionLimit = 50
        $list.Update()
        $Context.ExecuteQuery() 
        Write-host "Versioning has been turned ON at :"$List.title
    }
  }
}
catch{
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
} 
This script turns on versioning in SharePoint online for all lists and libraries in the given site. If you want to filter and enable versioning on document libraries alone, use:
If($List.BaseType -eq "DocumentLibrary")
{
   #Enable version history
}

SharePoint Online: Enable Versioning using PnP PowerShell 
To Check if versioning feature is enabled for the List, use: $List.EnableVersioning
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"
$ListName ="Projects"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

If($List)
{ 
    #sharepoint online enable versioning powershell
    Set-PnPList -Identity $ListName -EnableVersioning $True -MajorVersions 20
}

Similarly, you can configure versioning settings for all lists in a site
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com"

#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get All Lists from the web
$Lists = Get-PnPList | Where {$_.Hidden -eq $false}
ForEach($List in $Lists)
{ 
    #Enable versioning and set Number of versions to 50
    Set-PnPList -Identity $List -EnableVersioning $True -MajorVersions 50
    Write-host -f Yellow "Configured Versioning on List:"$List.Title
}
Likewise, To enable minor versions, use:
Set-PnPList -Identity $ListName -EnableVersioning $True -EnableMinorVersions $True -MajorVersions 25 -MinorVersions 25
How long does SharePoint Online keep version history? Forever!

1 comment:

  1. Amazing Article and indeed very very helpful in disabling the versioning of the DLs and sites. I am interested in knowing how can we disable the same for the new sites that are being created. This script would work only on the current sites.

    Did someone try that

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.