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

How to Enable Versioning on a List or Library in SharePoint?

The Version History feature in SharePoint allows you to access a previously 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 a 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 the Versioning Settings link.
  • From the Versioning Settings page, choose “Create major versions”. Optionally, you can set a limit on the number of versions.
    sharepoint online enable version history
  • This enables SharePoint online document library versioning. Once the 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 a given list or library.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.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:

Let’s activate version history on all lists and libraries of a SharePoint Online site.

#Load SharePoint Online Assemblies
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll"
Add-Type -Path "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions16ISAPIMicrosoft.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)

#Get List
$List = Get-PnPList $ListName 

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

Similarly, you can configure versioning settings for all lists on 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

Enable Versioning with Major and Minor versions for All Document Libraries in a site

Here is the PnP PowerShell to enable minor versions for all libraries:

#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Neo"
 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get all document libraries
$Libraries =  Get-PnPList | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False}

ForEach($Library in $Libraries)
{ 
    #sharepoint online enable versioning powershell
    Set-PnPList -Identity $Library -EnableVersioning $True -EnableMinorVersions $True
}

How long does SharePoint Online keep version history? Forever!

Source link