In my customers 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:

[code lang=text]
$cred = Get-Credential -Message “Please Supply password” -UserName “Pieter.Veenstra@myportal.onmicrosoft.com”
[/code]

GetCred

To connect to SharePoint online:

[code lang=text]
$siteUrl = “https://myportal.sharepoint.com”
Connect-SPOnline -Url $siteUrl -Credentials $cred
[/code]

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.

[code lang=text]
$rootWeb = Get-SPOWeb
Get-AllViews($rootWeb)
[/code]

Get-AllViews

Table of Contents

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

[code lang=text]
function Get-AllViews ($myWeb)
{}
[/code]

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.

[code lang=text]
function Get-AllViews ($myWeb)
{
$webUrl = $myWeb.Url
$webTitle = $myWeb.Title
Write-Host -ForegroundColor Cyan “$WebTitle($WebUrl)”
}
[/code]

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

[code lang=text]
$lists = Get-SPOList -Web $myWeb

foreach ($list in $lists)
{

Write-Host -ForegroundColor Yellow  $list.Title

}
[/code]

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

[code lang=text]
$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
}
}
[/code]

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

[code lang=text]
$subwebs = Get-SPOSubWebs

foreach ($subweb in $subwebs)
{
Get-AllViews $subweb
}
[/code]

The full script

[code lang=text]
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
}

}
[/code]

By Pieter Veenstra

Business Applications and Office Apps & Services Microsoft MVP working as a Microsoft Productivity Principal Consultant at HybrIT Services. You can contact me using contact@veenstra.me.uk.

2 thoughts on “Office 365 – SharePoint Online – Get all views of all lists using PnP PowerShell”

Leave a Reply

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