Wednesday, September 30, 2020

Copy all views from one SharePoint list to another via Powershell

 Here's a PnP Powershell script that copies all views from one SharePoint list to another list:


# Url to the site
$siteUrl = "https://vivity.sharepoint.com"

# The list where we want to copy all of the views
$listWithViewsToCopy = "Banana"

# The list name to copy the views to
$listToCopyTo = "Pineapple"

Write-Host "Connecting to PnP..."
Connect-PnPOnline -Url $siteUrl -Credential (Get-credential)

# Get all the views and properties for the list
$viewsToCopy = Get-PnPView -List $listWithViewsToCopy -Includes "ViewQuery","ViewType","ViewData","ViewJoins","ViewProjectedFields","RowLimit","Paged"

# Loop through each view and create it in the new list
foreach ($view in $viewsToCopy)
{
   Write-Host "Adding View: "$view.Title
   # Loop through all fields and create an array

   $fields = [System.Collections.ArrayList]::new()

   foreach ($field in $view.ViewFields)
   {
      [void]$fields.Add($field)
   }

   # Add the new view
   Add-PnPView -List $listToCopyTo -Title $view.Title -Fields $fields -Query $view.ViewQuery -ViewType $view.ViewType -RowLimit $view.RowLimit
}

1 comment:

ed pills online said...

It's perfect time to make some plans for the future and it is time to be happy. I have read this post and if I could I wish to suggest you few interesting things or suggestions. Maybe you could write next articles referring to this article. I desire to read even more things about it!