kwizcom banner advertisement

SharePoint Online: Copy List Views using PowerShell-CSOM

Requirement: Copy a List View in SharePoint Online using PowerShell.

How to Copy a List View in SharePoint Online?
To copy view in SharePoint list through UI, follow these steps:
  • Navigate to the List  List Settings >> Scroll down to Views section >> Click on "Create View" link
  • Under "Start from an existing view" section pick any existing view. 
    copy list view in sharepoint online using PowerShell CSOM
  • Provide a name to your New View 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.
#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"

#To call a non-generic method Load
Function Invoke-LoadMethod() {
    param(
            [Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),
            [string]$PropertyName
        ) 
   $ctx = $Object.Context
   $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") 
   $type = $Object.GetType()
   $clientLoad = $load.MakeGenericMethod($type)
 
   $Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name)
   $Expression = [System.Linq.Expressions.Expression]::Lambda([System.Linq.Expressions.Expression]::Convert([System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),[System.Object] ), $($Parameter))
   $ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1)
   $ExpressionArray.SetValue($Expression, 0)
   $clientLoad.Invoke($ctx,@($Object,$ExpressionArray))
}

#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.ExecuteQuery()

        #Get the View Fields
        Invoke-LoadMethod -Object $View -PropertyName "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.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

Here is my another article on SharePoint On-premises PowerShell copy list view: SharePoint 2013: PowerShell to Copy View
SharePoint Online: Copy List Views using PowerShell-CSOM SharePoint Online: Copy List Views using PowerShell-CSOM Reviewed by Salaudeen Rajack on August 30, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.