SharePoint Online: PowerShell to Disable Versioning

Requirement: Disable Versioning in SharePoint Online using PowerShell

How to Disable Versioning in SharePoint Online?
Versioning feature in SharePoint Online creates a copy of items when someone changes a document, so that we can track every change to the document and revert to old version if needed. Although it's a nifty feature, in some cases you may need to turn it OFF. While its possible to turn OFF versioning in SharePoint Online lists by setting "Create a version each time you edit an item in this list?" option to "No", The "No versioning" option is removed in document library's versioning settings! By default, versioning is enabled in SharePoint Online document libraries.
sharepoint online powershell disable versioning
There is no way to disable version history of a document library from web user interface. So, we are left with PowerShell! Let's see how to turn off versioning in SharePoint Online.

SharePoint Online: PowerShell to Disable Versioning
Let's disable versioning in a SharePoint Online document library using PowerShell.
#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 Disable-SPOVersioning()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
    )
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
  
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
          
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
         
        #sharepoint online powershell disable versioning
        $List.EnableVersioning = $False
        $List.Update()
        $Ctx.ExecuteQuery() 
        Write-host -f Green "Versioning has been turned OFF at $ListName"
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}
 
#Set Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName="Migration Documents"
 
#Call the function 
Disable-SPOVersioning -SiteURL $SiteURL -ListName $ListName
This PowerShell disables version history on the given document library! How about disabling version history in all document libraries of a SharePoint Online site collection?

PnP PowerShell to Disable Versioning on a List
How about disable version history in a SharePoint Online document library using PnP PowerShell?
#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migration Documents"

#Connect to the site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)

#Set versioning settings of the List
Set-PnPList -Identity $ListName -EnableVersioning $False

Disable Versioning in All Document Libraries of a SharePoint Online Site
Let's turn off version history in SharePoint Online site collection:
#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 to Disable Versioning on All Document Libraries in a SharePoint Online Site
Function Disable-SPOVersionHistory()
{
    param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL
    )
    Try {
        Write-host -f Yellow "Processing site:"$SiteURL

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Global:Credentials
  
        #Get the site, subsites and lists from given site
        $Web = $Ctx.web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Lists)
        $Ctx.Load($web.Webs)
        $Ctx.executeQuery()

        #Array to exclude system libraries
        $SystemLibraries = @("Form Templates", "Pages", "Preservation Hold Library","Site Assets", "Site Pages", "Images",
                            "Site Collection Documents", "Site Collection Images","Style Library")
        
        #Get All document libraries
        $DocLibraries = $Web.Lists | Where {$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $False -and $_.Title -notin $SystemLibraries}
        ForEach($Library in $DocLibraries)
        {
            #disable versioning in each document library
            $Library.EnableVersioning = $False
            $Library.Update()
            $Ctx.ExecuteQuery() 
            Write-host -f Green "`tVersioning has been turned OFF at '$($Library.Title)'"
        }
 
        #Iterate through each subsite
        ForEach ($Subweb in $Web.Webs)
        {
            #Call the function recursively
            Disable-SPOVersionHistory($Subweb.url)
        }
    }
    Catch {
        write-host -f Red "Error:" $_.Exception.Message
    }
}

#Set Parameters
$SiteURL="https://crescent.sharepoint.com/sites/marketing"

#Get Credentials to connect
$Cred= Get-Credential
$Global:Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Call the function to disable versions on all document libraries
Disable-SPOVersionHistory -SiteURL $SiteURL
This disables versioning in all document libraries (Except: Hidden and excluded system libraries) of the given site collection. Here is my another post on configuring version history settings SharePoint Online: SharePoint Online: Enable Versioning using PowerShell

Enable "No versioning" and remove Minimum Versioning Limit at Tenant Level
You can enable "No versioning" and disable minimum version requirement of 100 at the tenant level using this PowerShell cmdlet:
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Parameters
$AdminCenterURL="https://CrescentIntranet-Admin.SharePoint.com"

#Setup Credentials to connect
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

Set-SPOTenant -EnableMinimumVersionRequirement $False
It takes a while to reflect this change (It took me 5 min for me) and enables "No versioning" option under versioning settings and removes the minimum version of 100 requirement (So that you won't get "You must enter a number between 100 and 50000." validation message!)
disable versioning in sharepoint online


Create new document library with Version history turned OFF
Here is an another trick to create a document library with versioning disabled. 

  • Go to: Site Contents page >> Click on "Add an App" >> Pick "Document Library" 
  • Click on "Advanced Options" link. Now, you'll get an option to disable versioning on the new document library you gonna create. 
  • Just choose "No" for "Create a version each time you edit a file in this document library"?
    create document library with version history disabled

This creates a new document library without versioning feature enabled.

1 comment:

  1. There seems to be a lot of people still stuck unable to turn off the forced docLib versioning. Thanks for sharing this!!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.