Thursday, 29 October 2015

Migrate Custom View along with its Fields/Columns from one Sharepoint List to Other.

Add-PSSnapin Microsoft.SharePoint.PowerShell

$SPSourceWeb = Get-SPWeb "http://server:port/SourceSite" # Source Site
$SPSourceList = $SPSourceWeb.Lists["Source List Name"]
$SPSourceListViewSchema = $SPSourceList.Views
$SPSourceListViewSchema = $SPSourceList.Views["Custom View"].SchemaXml
Write-Output $SPSourceListViewSchema > D:\View\CustomView.xml

$ViewTemplatePath = "D:\AutoDeployment\CustomView.xml"
$ViewTemplateXML = [xml](get-content $ViewTemplatePath)

$SPDestSites = @("<Destination Site URL 1>","<Destination Site URL 2>","<Destination Site URL N>")
$Count = 0
Write-Output "WorkFlow Task View" > D:\View\CustomViewErrorLog.txt
foreach($SPDestSite in $SPDestSites)
{
    try
    {
        $SPDestWeb = Get-SPWeb -Identity $SPDestSite
    
        $SPDestList = $SPDestWeb.Lists["Destination List Name"]
        $Viewfields = $WFTaskViewTemplateXML.View.ViewFields.FieldRef.Name
        $viewRowLimit = "100"
        $viewPaged = $true
        $viewDefaultView = $false

        # Setting the Query for the View
         $viewQuery = ""
         $viewName = "Workflow Task View"

        # Finally – Provisioning the View
         $myListView = $SPDestList.Views.Add($viewName, $viewFields, $viewQuery, $viewRowLimit, 
         $viewPaged,    $viewDefaultView)

        # Update the View for changes made to the view
         $myListView.Update()
         $SPDestList.Update()
         $SPDestWeb.Dispose()
    }
    catch
    {
   Write-Output "$($SPDestSite) ==> Exception: $($_.Exception.Message)" > D:\View\CustomViewErrorLog.txt
    }

No comments:

Post a Comment