kwizcom banner advertisement

SharePoint Online: Get List Views using PowerShell

Requirement: Get SharePoint Online List Views using PowerShell.

PowerShell to Get All Views from a List:
This PowerShell script gets all views from a SharePoint Online list or library.
#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"
 
#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 List
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
    #$Ctx.Load($List)

    #Get All List Views of the List
    $ctx.Load($List.Views)
    $Ctx.ExecuteQuery()

    Write-host "Total Views in the List:"$List.Views.Count
    #Iterate through each View
    ForEach($View in $List.Views)
    {
        #Get the View
        $ctx.Load($View)
        $Ctx.ExecuteQuery()

        Write-Host -f Yellow $View.Title
    }    
}
Catch {
    write-host -f Red "Error Getting List Views!" $_.Exception.Message
}
Get the Default View of the List:
#Get the Default View of the List
$DefaultView = $List.DefaultView
$Ctx.Load($DefaultView)
$Ctx.ExecuteQuery()
    
Write-Host $DefaultView.Title

Get a Specific View from the List:
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$ListName="Projects"
$ViewTitle ="All Items" 

#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 List
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
    
    #Get the List View
    $ListView = $List.views.GetByTitle($ViewTitle)
    $Ctx.load($ListView)
    $Ctx.executeQuery()
    
    #Get the View URL
    Write-host "URL of the List View:"$ListView.ServerRelativeUrl
}
Catch {
    write-host -f Red "Error Getting List Views!" $_.Exception.Message
}
SharePoint Online: Get List Views using PowerShell SharePoint Online: Get List Views using PowerShell Reviewed by Salaudeen Rajack on July 28, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.