How to Hide a List View in SharePoint using PowerShell?

Requirement: Hide a List View in SharePoint
sharepoint hide a list view

How to Hide a SharePoint List View?
To hide a list view in SharePoint, use this PowerShell script.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Config variables
$SiteURL="http://intranet.crescent.com"
$ListName="Projects"
$ViewName="All Projects"

#Get the View
$Web = Get-SPWeb $SiteURL
$List = $Web.Lists[$ListName]
$View = $List.Views[$ViewName]

#Hide the list view
$View.Hidden=$true
$View.Update()

Write-host "List View Hidden from users!"
This hides the list view from users by hiding it from view drop down and from list settings. However, if you know the URL, you can directly hit and get the view page!

SharePoint Online: Hide a List View using PowerShell
Similarly, in SharePoint Online hide list view, here is the PowerShell script.
#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/"
$ListName="Projects"
$ViewName ="All Projects" 

#Get 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)
    
    #Get the List View
    $ListView = $List.views.GetByTitle($ViewName)
    $Ctx.load($ListView)
    $Ctx.executeQuery()

    #Hide SharePoint Online List view
    $ListView.Hidden = $True
    $ListView.Update()
    $Ctx.ExecuteQuery()

    Write-host -f Green "List View Hidden from users!"
}
Catch {
    write-host -f Red "Error Hiding List View!" $_.Exception.Message
}
This hides list view in SharePoint Online from all users. If you want to restrict a view to specific users or groups, use my another article: How to Set permission to SharePoint List View?
How to Hide a List View in SharePoint using PowerShell? How to Hide a List View in SharePoint using PowerShell? Reviewed by Salaudeen Rajack on January 06, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.