Pages

Wednesday, February 1, 2012

SharePoint 2010: How to Copy a List within a Site Collection?

At times you may have to copy a list in a site collection for archiving purposes. The out-of-the-box options to achieve this are following:

  1. Save List as Template with Content: This option allows you to save list as template including content. After the template is created you can create a new list using the list template.

    Note:
    This option is not guaranteed to work all the time specially when there are too many items in the list.
  2. Take Granular Backup Using SharePoint Central Administration: This option is new in SharePoint 2010. However, it only allows you to backup the list. In order to restore the list, you will have to use Import-SPWeb cmdlet. However, you cannot use this option to copy a list within site collection.
  3. PowerShell: This is the best option when it comes to copying list in your SharePoint site. Here is what you need to do.
    • Get an instance of the SharePoint Site Collection using SharePoint SPSite object.
    • Open the root web.
    • Save the list that you want to copy as a template using SaveAsTemplate method of SPList.
    • Get a reference to the list template that is created.
    • Add a list using SPListCollection.Add Method (String, String, SPListTemplate). When successful, this method will display the list GUID.
    • Update the root web using SPWeb.Update

The PowerShell script is provided below.

#//Replace the URL with your real site collection URL
$siteURL = "http://sitecollectionURL"
#//Replace the list title with your list title in site collection
$sourceListTitle = "My List Title"
$targetListTitle = "My Target List Title"
 
$site = New-Object Microsoft.SharePoint.SPSite($siteURL)
$web = $site.OpenWeb()
$sourceList = $web.Lists[$sourceListTitle]
$sourceList.SaveAsTemplate($sourceListTitle,$sourceListTitle,$true)
$listTemplate = $site.GetCustomListTemplates($web)[$sourceListTitle]
$web.Lists.Add($targetListTitle, $targetListTitle, $listTemplate)
 
#//if successfull you will see a GUID output
$web.Update()
$web.Dispose()
$site.Dispose()

No comments:

Post a Comment