SharePoint Online: How to Hide a List or Library using PowerShell?

Requirement: Hide a list or library from SharePoint Online site.
hide list or document library in sharepoint online using powershell

SharePoint Online: Hide Document Library using PowerShell
For a custom application, we wanted to hide the config list in SharePoint Online, to prevent edit or deletions. There are many ways to hide a SharePoint online list. Here is the PowerShell CSOM script to create hidden lists in SharePoint online.  
#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"
  
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/sales/"
$LibraryName="ProjectConfig"

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

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
  
    #Get the web and Library
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($LibraryName)

    #Hide the list
    $List.Hidden = $True
    $List.Update()
    $Ctx.ExecuteQuery()
     
    Write-host -f Green "List hidden Successfully!"
}
Catch {
    write-host -f Red "Error hiding List: " $_.Exception.Message
}
Although the list is hidden from the site contents page, Everything will work as usual in the list and you can still access it via list URL.

SharePoint Online: Hide List using PnP PowerShell
Once you execute this script, the given list goes hidden from the site contents page immediately. However, The list can be accessed via URL and all the operations on that list will work as usual.
#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$ListName ="AppConfig"

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

#SharePoint Online Hide a List
Set-PnPList -Identity $ListName -Hidden $True

To make the list visible again, just set the value of the "hidden" flag to "False".  we can't directly create a hidden list in SharePoint Online, but we can set its property hidden once added.

SharePoint Online Hidden List using SharePoint Designer
We can also use SharePoint Designer to mark a list hidden from the browser. Read more here: How to hide a list in SharePoint?
how to hide list in sharepoint online

2 comments:

  1. I wonder, If there is a way to get all hidden lists in a SharePoint Online site..

    ReplyDelete
    Replies
    1. Sure, You can use: Get-PnPList | Where { $_.Hidden -EQ $True}

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.