Copy a library view across multiple libraries in destination site
Summary
I had a requirement to create a flat view "checked out files by me" across all libraries to enable users to easily spot checked out files by them within folders. I created the view in one library using the SharePoint Online UI and I used the script to copy the view to all libraries in another site. The url of the view needed to be without spaces so I created the view with a name without spaces and amended the title of the view using the script.
The sample script using PnP PowerShell to copy a library view from the source site and create it in libraries present in destination site.
Please refactor according to requirements as in the sample given only fields, views, query, item limit and certain settings are copied across.
$SourceSite = Read-Host "Enter the source site url from which to copy the view from" #e.g.https://contose.sharepoint.com/sites/test
$SourceList = Read-Host "Enter the source Library from which to copy the view from" #Documents
$SourceViewName = Read-Host "Enter the view name to be copied from" #Checked Out FlatView
$destSiteUrl = Read-Host "Enter the destination site url to which to copy the view to" #e.g.https://contose.sharepoint.com/sites/testClone2
$dateTime = (Get-Date).toString("dd-MM-yyyy")
$invocation = (Get-Variable MyInvocation).Value
$directorypath = Split-Path $invocation.MyCommand.Path
$fileName = "CheckedOutViewCreationReport-" + $dateTime + ".csv"
$OutPutView = $directorypath + $fileName
#Arry to Skip System Lists and Libraries
$SystemLists = @("Converted Forms", "Master Page Gallery", "Customized Reports", "Form Templates", "List Template Gallery", "Theme Gallery",
"Reporting Templates", "Solution Gallery", "Style Library", "Web Part Gallery","Site Assets", "wfpub", "Site Pages", "Images", "MicroFeed","Pages")
#remove any spaces from view
$SourceInternalName = $SourceViewName -replace '\s',''
$m365Status = m365 status
if ($m365Status -eq "Logged Out") {
# Connection to Microsoft 365
m365 login
}
$CheckedOutView = m365 spo list view get --webUrl $SourceSite --listTitle $SourceList --viewTitle $SourceViewName |ConvertFrom-Json
[xml] $listViewXML = $CheckedOutView.ListViewXml
#Array to Hold Result - PSObjects
$ViewCollection = @()
$fieldsArr = @();
$listViewXML.View.ViewFields.FieldRef | ForEach-Object {
$fieldsArr +=$_.Name;
}
$viewScope=1 #enum recursive = 1 , RecursiveAll = 2, FilesOnly = 3
#retrieving only document libraries from destination libraries
$lists= m365 spo list list --webUrl $destSiteUrl | ConvertFrom-Json
foreach ($list in ($lists | Where-Object {$_.BaseTemplate -eq 101 -and $_.Hidden -eq $false -and $SystemLists -notcontains $_.Title})) {
$viewInL = m365 spo list view get --webUrl $destSiteUrl --listTitle $list.Title --viewTitle $SourceViewName
#create view only if not present
if(!$viewInl)
{
$ExportVw = New-Object PSObject
$ExportVw | Add-Member -MemberType NoteProperty -name "Site URL" -value $destSiteUrl
$ExportVw | Add-Member -MemberType NoteProperty -name "Library Name" -value $list.Title
$ExportVw | Add-Member -MemberType NoteProperty -name "View Name" -value $SourceViewName
#create view
m365 spo list view add --webUrl $destSiteUrl --listTitle $list.Title --title $SourceInternalName --fields ($fieldsArr -join ",") --rowLimit $CheckedOutView.RowLimit
$viewInL = m365 spo list view get --webUrl $destSiteUrl --listTitle $list.Title --viewTitle $SourceInternalName
if($viewInL)
{
# Update the list view name to the display name and change the scope to recursive so that all files are displayed without any folders.
m365 spo list view set --webUrl $destSiteUrl --listTitle $list.Title --viewTitle $SourceInternalName --Title $SourceViewName --ViewQuery $CheckedOutView.ViewQuery.Replace('"','\"') --Scope $viewScope
#m365 spo list view set --webUrl https://reshmeeauckloo.sharepoint.com/sites/TestClone2 --listTitle "Documents" --viewTitle "FlatView" --Title "FlatView1"
#Set-PnPView -List $list.Title -Identity $SourceInternalName -Values @{Scope=$viewScope;Title=$SourceViewName}
}
$ViewCollection += $ExportVw
}
}
#Export the result Array to CSV file
$ViewCollection | Export-CSV $OutPutView -Force -NoTypeInformation
m365 logout
Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365
Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365
Contributors
Author(s) |
---|
Reshmee Auckloo |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.