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:

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

GetCred

To connect to SharePoint online:

Connect-SPOnline -Url https://myportal.sharepoint.com -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 fucntion will list all views for a site and all its subsites. 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”

Connect-SPOnline -Url https://myportal.sharepoint.com -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
}

}

Advertisements