SharePoint Online: Copy List Views using PowerShell

Requirement: Copy a View in SharePoint Online using PowerShell.

How to Copy a List View in SharePoint Online?

Do you need to create a copy of a list view in SharePoint Online? Maybe you need to make some changes to the view and don’t want to affect the original. Or maybe you simply want to have a backup copy of the view in case something happens to the original. In any case, copying a list view is easy to do. This article will show you how to copy a list view in SharePoint Online.

Copying a view in SharePoint Online is a simple process that can be done using the user interface. Here are the steps:

  1. Navigate to the list or library for which you want to copy a view.
  2. Under the “Views” section, click on the view you want to copy to open its settings.
  3. Click the “Save View As” button in the views drop-down menu.
    sharepoint online copy view
  4. In the “Save View As” dialog box, enter a name for the new view and choose whether to make it the default view.
  5. Click “OK” to save the new view.

The new view will be created as a copy of the original view, with the same settings and filters. You can modify the new view as needed without affecting the original view. If you are using classic experience, To copy a view in the SharePoint list through UI, follow these steps:

  1. Navigate to the List Settings >> Scroll down to the Views section >> Click on the “Create View” link.
  2. Under the “Start from an existing view” section, pick any existing view. 
    copy list view in sharepoint online using PowerShell CSOM

Provide a name for your new view, modify it as needed, and click OK to copy the view.

How to Copy SharePoint view using PowerShell CSOM?

To copy a view in SharePoint Online, use this PowerShell script. This is a handy way to duplicate a view or create a new one based on an existing one.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Function to copy list view
Function Copy-SPOListView()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ViewName,
        [Parameter(Mandatory=$true)] [string] $NewViewName
    )
    Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the list by title
        $List=$Ctx.web.Lists.GetByTitle($ListName)
        
        #Get the View to Copy 
        $View = $List.Views.GetByTitle($ViewName)
        $Ctx.Load($view)
        $Ctx.Load($View.ViewFields)
        $Ctx.ExecuteQuery()

        #Copy the List View
        $ViewCreationInfo = New-Object Microsoft.SharePoint.Client.ViewCreationInformation
        $ViewCreationInfo.Paged = $View.Paged
        $ViewCreationInfo.PersonalView = $View.PersonalView
        $ViewCreationInfo.Query = $View.ViewQuery
        $ViewCreationInfo.RowLimit = $View.RowLimit
        $ViewCreationInfo.Title = $NewViewName
        $ViewCreationInfo.SetAsDefaultView = $View.DefaultView 
        $ViewCreationInfo.ViewFields = $View.ViewFields
        $ViewCreationInfo.ViewTypeKind = $View.ViewType
 
        $List.views.Add($ViewCreationInfo)  | Out-Null
        $Ctx.ExecuteQuery()

        write-host -f Green "List View has been Copied Successfully!"
     }
    Catch {
        write-host -f Red "Error Copying List View!" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops/"
$ListName="Projects"
$ViewName="All Items"
$NewViewName="All Projects"

#Call the function to copy view
Copy-SPOListView -SiteURL $SiteURL -ListName $ListName -ViewName $ViewName -NewViewName $NewViewName

PnP PowerShell to Clone a View in SharePoint Online

Alternatively, you can also copy a view using PnP PowerShell. Here are the script:

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"
$ViewName = "Active Projects"
$NewViewName = "Current Projects"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the Source View
    $View = Get-PnPView -List $ListName -Identity $ViewName -Includes ViewType, ViewFields, Aggregations, Paged, ViewQuery, RowLimit
    
    #Get Properties of the source View
    $ViewProperties = @{
        "List" =  $ListName
        "Title" = $NewViewName
        "Paged" = $View.Paged
        "Personal" = $View.PersonalView
        "Query" = $View.ViewQuery
        "RowLimit" = $View.RowLimit
        "SetAsDefault" = $View.DefaultView 
        "Fields" = @($View.ViewFields)
        "ViewType" = $View.ViewType
        "Aggregations" = $View.Aggregations
    }
    #Create a New View
    Add-PnPView @ViewProperties
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

With these simple steps, you can easily create a copy of an existing view using the user interface or PowerShell, and customize it to meet your needs. Here is another article on SharePoint On-premises PowerShell copy list view: SharePoint 2013: PowerShell to Copy View

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Online: Copy List Views using PowerShell

  • # assumes column names of source list/view exist in target list

    [CmdletBinding()]
    Param(
    [string]$domainRoot = “https://cresent.sharepoint.com”
    ,[string]$collName = “RecordsCenter”
    ,[string]$sourceSiteName = “”
    ,[string]$sourceListName = “”
    ,[string]$sourceViewName = “”
    ,[string]$targetSiteName = “”
    ,[string]$targetListName = “”
    ,[string]$TargetViewName = “”
    )
    $sourceSiteUrl = “$domainRoot/sites/$collName/$sourceSiteName”
    $targetSiteUrl = “$domainRoot/sites/$collName/$targetSiteName”

    [hashtable]$ViewProperties = @{}

    function Get_Views
    {
    #Param( [hashtable]$ViewProperties )

    Try {
    Connect-PnPOnline -Url $sourceSiteURL -UseWebLogin

    $View = Get-PnPView -List $sourceListName -Identity $sourceViewName -Includes ViewType, ViewFields, Aggregations, Paged, ViewQuery, RowLimit

    $script:ViewProperties = @{
    “List” = $targetListName
    “Title” = $targetViewName
    “Paged” = $View.Paged
    “Personal” = $View.PersonalView
    “Query” = $View.ViewQuery
    “RowLimit” = $View.RowLimit
    “SetAsDefault” = $View.DefaultView
    “Fields” = @($View.ViewFields)
    “ViewType” = $View.ViewType
    “Aggregations” = $View.Aggregations
    }
    Disconnect-PnPOnline
    }
    catch {
    write-host “Get_Views Error: $($_.Exception.Message)” -foregroundcolor Red
    }
    }

    function Set_Views
    {
    Param( [hashtable]$ViewProperties )

    Try {

    Connect-PnPOnline -Url $using:targetSiteURL -UseWebLogin
    Add-PnPView @ViewProperties
    Disconnect-PnPOnline
    }
    catch {
    write-host “Set_Views Error: $($_.Exception.Message)” -foregroundcolor Red
    }
    } # function Set_Views

    Get_Views ; “Get_Views Finished” | Out-Host
    Start-Job -ScriptBlock ${Function:Set_Views} -ArgumentList $ViewProperties | Wait-Job | Receive-Job ; “Set-Views Finished” | Out-Host

    # call like:
    clear ; $error.Clear() ; & $scriptDir\Clone_PnPViewToTargetList.ps1 -sourceSiteName 200000_CC-9 -sourceListName ‘Official Files’ -sourceViewName ‘Documents in Official File’ -targetSiteName 200000_CC-E -targetListName ‘Official Files’ -targetViewName ‘Documents in Official File H’

    Reply
  • How to copy from SP 2010 to SP Online?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *