SharePoint Online: How to Disable "Delete this list"?

Requirement: Disable delete on a SharePoint Online List or Document Library.

How to Disable Delete options for List or Document Library in SharePoint Online?
We have a requirement to disable any accidental deletes on certain SharePoint Online lists and document libraries that are provisioned as part of a custom application. If you have noticed, in some of the SharePoint lists & libraries (E.g. "Farm Templates" library), "Delete this List" or "Delete this Document Library" links are missing by default to prevent delete option. So, How do we disable delete options?

Well, if you want to disable the delete list option from list settings in SharePoint (and in other places too!), you can set "AllowDeletion" property of the List or Library to "False". Once this is set, delete options go hidden!
sharepoint online disable delete this list

PowerShell to Disable Delete in SharePoint Online List
Let's turn "Allow Delete" flag to false to prevent deletion.
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

#Set Parameters for Site URL and List Name
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName= "config"
 
#Setup Credentials to connect
$Cred = Get-Credential
 
Try {
    #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)
 
    #disable delete in sharepoint online list
    $List.AllowDeletion = $False
    $List.Update()
    $Ctx.ExecuteQuery()
         
    Write-host -f Green "List Settings Updated Successfully!"
}
Catch {
    write-host -f Red "Error Updating List Settings!" $_.Exception.Message
}
This script hides delete options from the given list in places such as list settings, etc. Similarly, we can disable delete options for SharePoint Online document libraries as well. Let's use PnP PowerShell this time:
#Set Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Projects"
$ListName = "Inventory"

#Connect to SharePoint Online site
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Disable delete in sharepoint online document library
$List = Get-PnPList -Identity $ListName -Includes AllowDeletion
$List.AllowDeletion = $False
$List.Update()
Invoke-PnPQuery
SharePoint Online Can't Delete List?
Once the "AllowDeletion" flag is set to false, we can't delete the list neither through web UI nor PowerShell, until we revert the flag to "True"! You will get "This list cannot be deleted." error if you try to delete it. if you must delete a list that doesn't provide "Delete this list" link.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.