In my client’s SharePoint site there are a number of lists where multiple views with similar names have been created. (e.g. All Items and AllItems”). Rather than going through every list and checking where the double views have been created I used the PowerShell commands from the PnP framework to list all the views inย all theย sites.

To collect the credentials for the user that I’m running as I run the following command:

$cred = Get-Credential -Message "Please Supply password" -UserName "Pieter.Veenstra@myportal.onmicrosoft.com"
GetCred

To connect to SharePoint online:

$siteUrl = "https://myportal.sharepoint.com"
Connect-SPOnline -Url $siteUrl -Credentials $cred

Ok, so now I’m connected to my SharePoint online root site.

First of all I’m going to get the root web and then I’m going to call a function Get-AllViews. The Get-AllViews function will list all views for a siteย and all its sub sites. The Get-AllViews doesn’t exist yet, but I’m going to give the details of that function later on in this post.

$rootWeb = Get-SPOWeb
Get-AllViews($rootWeb)

Get-AllViews

I’m starting with an empty function. I’m going for the simple function style rather than the more complicated style as described in my other post

function Get-AllViews ($myWeb)
{}

I want the Get-AllViews function to display

The site, List and view names in different colours.

I’m going to start with displaying the site’s Title and Url.

function Get-AllViews ($myWeb)
{
$webUrl = $myWeb.Url
$webTitle = $myWeb.Title
Write-Host -ForegroundColor Cyan "$WebTitle($WebUrl)"
}

Now I’m getting the lists within my web and I’m going to display the list Titles in yellow

$lists = Get-SPOList -Web $myWeb

foreach ($list in $lists)
{

Write-Host -ForegroundColor Yellowย  $list.Title

}

Now I’m picking up the context with Get-SPOContext and loading the views in my lists. Displaying the list names is now easy:

$lists = Get-SPOList -Web $myWeb

foreach ($list in $lists)
{
Write-Host -ForegroundColor Yellowย  $list.Title

$ctx = Get-SPOContext
$ctx.Load($list.Views)
Execute-SPOQuery

$views = $list.Views
foreach ($view in $views)
{
$ctx = Get-SPOContext
$ctx.Load($view)
$ctx.ExecuteQuery()
$view.Title
}
}

Then when I listed all the views for the site I’m repeating the same process for all the sub sites:

$subwebs = Get-SPOSubWebs

foreach ($subweb in $subwebs)
{
Get-AllViews $subweb
}

The full script

Clear-Host

$cred = Get-Credential -Message "Please Supply password" -UserName "Pieter.Veenstra@myportal.onmicrosoft.com"

$siteUrl = "https:/ /myportal.sharepoint.com"

Connect-SPOnline -Url $siteUrl -Credentials $cred

$rootWeb = Get-SPOWeb

Get-AllViews($rootWeb)
function Get-AllViews ($myWeb)
{
$webUrl = $myWeb.Url
$webTitle = $myWeb.Title
Write-Host -ForegroundColor Cyan "$WebTitle($WebUrl)"

$lists = Get-SPOList -Web $myWeb
foreach ($list in $lists)
{
Write-Host -ForegroundColor Yellowย  $list.Title

$ctx = Get-SPOContext
$ctx.Load($list.Views)
Execute-SPOQuery

$views = $list.Views
foreach ($view in $views)
{
$ctx = Get-SPOContext
$ctx.Load($view)
$ctx.ExecuteQuery()
$view.Title
}
}
$subwebs = Get-SPOSubWebs

foreach ($subweb in $subwebs)
{
Get-AllViews $subweb
}

}

Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Related Posts

2 thoughts on “Get all views of SharePoint lists using PnP PowerShell

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.