2

I need to copy a view from one library in a site to another site and add it to it's libraries. Is there an easy way to do this? Free program or simple script?

4
  • This is similar to what you want. This script gets a view from source list and copies it to all the libraries in the destination web. You just have to modify the part where it loops through each library and instead, specify a particular library. Link: rogierdijkman.wordpress.com/2013/12/16/…
    – Akhoy
    Commented Jul 24, 2015 at 14:20
  • Thanks for the link, little bit of a powershell novice. I have tried the script and I get this error.Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string. At C:\users\admingg\Desktop\Copy-SPView.ps1:21 char:15 + Write-Host & <<<< quot;Loading Sytem Modules &quot; + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : AmpersandNotAllowed
    – Mike Blair
    Commented Jul 24, 2015 at 16:27
  • I haven't tried it before, but maybe you could copy the code from SharePoint Designer for the existing view. Then for the new library that you want to add it, create a new view in SPD and paste the code. I'm assuming that you would have to have the same column names, or it may take a little bit of editing. Commented Jul 24, 2015 at 17:59
  • I have copied the views from SPD before and I think it didn't work. I think there is a GUID assigned to each list and the view is linked to it....?
    – Mike Blair
    Commented Jul 25, 2015 at 9:37

1 Answer 1

2

If your lists are in site collections which are within the same farm (or server), you can try this:

Function Copy-SPView([string]$WebURL,
  [string]$SourceList,
  [string]$SourceView,
  [string]$TargetURL,
  [string]$DestList  )
{
  Write-Host "Loading Sytem Modules";
  #Get-Module -listAvailable | import-module
  if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
  {
     Write-Host "Loading Sharepoint Module";
     [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
     Add-PSSnapin -Name Microsoft.SharePoint.PowerShell

     if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell) -eq $null )
        {
        Write-Host "Failed to load sharepoint snap-in. Could not proceed further, Aborting ...";   Exit
        }
   }
   Start-SPAssignment -Global
   $SPWeb  = Get-SPWeb -Identity $WebURL -ErrorAction SilentlyContinue
   $SPWebT = Get-SPWeb -Identity $TargetURL -ErrorAction SilentlyContinue  
   if($SPWeb -eq $null){ Write-Host "Unable to reach the provided URL, Aborting ..." ;Exit }
   if( ($SPWeb.Lists.TryGetList($SourceList) ) -eq $Null){ 
   Write-Host "The list $SourceList is not availible, Aborting ..."; Exit }
   $SourceLists=$SPweb.lists["$SourceList"]
   if( ($SourceLists.Views["$SourceView"]) -eq $Null ){  Write-Host "View not available, Aborting ..." ;Exit }
   if($SPWebT -ne $null)
   {
      $list=$SPWebT.lists["$DestList"]
    if($list.BaseType -eq "DocumentLibrary")
    {     
    $view = $list.Views[$SourceView]
    if ($view -ne $null)
    {
      Write-Host "Updating existing view" -foregroundcolor Yellow -backgroundcolor Black
      $list.views.delete($view.ID)
      $list.update()
    }

    $Viewfields = $Sourcelists.Views[$SourceView].ViewFields.ToStringCollection()
    $viewRowLimit="100";
    $viewPaged=$true
    $viewDefaultView=$AsDefault

    # Setting the Query for the View
    $viewQuery = $Sourcelists.Views[$SourceView].Query
    $viewName = $SourceView

    # Finally – Provisioning the View

    try
    {
      $myListView = $list.Views.Add($viewName, $viewFields, $viewQuery, 100, $True, $False, "HTML", $False)
    }
    catch
    {
      Write-Host "Not all columns are availible in the target library" -foregroundcolor Yellow
    }

    # You need to Update the View for changes made to the view
    # Updating the List is not enough
    $myListView.DefaultView = $AsDefault
    $myListView.Update()
    $list.Update()

    Write-Host "$viewName added to Library $list"
  }
  }
$SPWeb.Dispose()
$SPWebT.Dispose()
}

Usage:
Copy-SPView "SourceSite" "SourceList" "SourceView" "DestSite" "DestList"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.