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!

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
#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
#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
SharePoint Online: PowerShell to Disable Versioning SharePoint Online: PowerShell to Disable Versioning Reviewed by Salaudeen Rajack on August 11, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.