Home > SharePoint 2010, SharePoint Administration > Importing SharePoint 2010 Lists/Document Libraries from backup directory using PowerShell.

Importing SharePoint 2010 Lists/Document Libraries from backup directory using PowerShell.

Previously I wrote a blog about how to Export all the lists and document libraries from a specific site collection in SharePoint 2010 –> http://jshidell.com/2012/04/26/export-all-listdocument-libraries-of-a-specific-site-collection-in-sharepoint-2010-using-powershell/

I’m now going to blog about how to Import these lists and document libraries back into SharePoint.  Now In most cases you would just import these lists and document libraries into a new Site Collection on the same farm, but for whatever reason such as mine I had to import these lists and document libraries into a different site collection on a different farm (Staging farm).

For me to do this I had to move over all my “.cmp” files into a directory on my staging farm server so it would be accessible for the import.  Granted you could possibly set up a share between both farms servers and just map to the location to pull the files.  Regardless the script below should work either way.

——————————————————————————————————————————————————

#Location of files to Import
$path = Read-Host “Location of Import Files”

#This is the Destination Site that will be importing the exported sites and libraries
$destinationWebURL = Read-Host “Enter in the destination site to import lists”

#This will iterate through the Import directory location, select all files with the extension of .cmp and store it in the $List variable
$Dir = get-childitem $path -recurse
$Lists = $Dir | Where-Object {$_.extension -eq “.cmp”} | Select-Object Name

Write-Host “Importing Lists & Document Libraries into  ” $destinationWebURL -ForegroundColor Cyan

#a foreach loop to loop through the Lists, check for files that may contain multiple split .cmp files (i.e documents1.cmp, documents2.cmp) and only selecting the first instance (i.e. documents.cmp), and then importing the list.

foreach($list in $Lists)
{
 if ($list.Name -notlike ‘*[a-z][1-9]*’)
{
 write-host “Importing ” $list.Name
 Import-SPWeb -Identity $destinationWebURL -path ($path + “\” + $list.Name) -UpdateVersions Overwrite-IncludeUserSecurty
}
}

write-host “Import Complete” -ForgroundColor Cyan

—————————————————————————————————————————————————–

Advertisements
  1. Marco
    March 12, 2013 at 7:19 pm

    Hi James,

    I tweaked your sample code to get a more detailed output on the Windows PowerShell console.

    #Add-PSSnapin Microsoft.Sharepoint.Powershell

    #Location of files to Import
    #$path = Read-Host “Enter the location of the .cmp files to import:”

    #This is the Destination Site that will be importing the exported sites and libraries
    #$destinationWebURL = Read-Host “Enter in the destination site to import lists”

    #This will iterate through the Import directory location, select all files with the extension of .cmp and store it in the $List variable
    $Dir = get-childitem $path -recurse
    $Lists = $Dir | Where-Object {$_.extension -eq “.cmp”} | Select-Object Name
    $CountOflists = @(get-childitem $path *.cmp -recurse).Count
    $listItem = 1

    Write-Host “Importing Lists & Document Libraries into: ” $destinationWebURL -ForegroundColor Blue

    #a foreach loop to loop through the Lists, check for files that may contain
    #multiple split .cmp files (i.e documents1.cmp, documents2.cmp) and only
    #selecting the first instance (i.e. documents.cmp), and then importing the list.

    foreach($list in $Lists)
    {
    if ($list.Name -notlike ‘*[a-z][1-9]*’)
    {
    Write-Host “» Importing:” $list.Name “(” $listItem “of” $CountOflists “)” -ForegroundColor Green

    Import-SPWeb -Identity $destinationWebURL -path ($path + “\” + $list.Name) -UpdateVersions Overwrite -IncludeUserSecurity -NoLogFile

    $listItem++
    }
    }

    Write-Host “Import Complete” -ForegroundColor Blue
    ______________________

    The script works really cool, however, I am getting an error on the “Shared Documents” importing. Here the output:

    Importing Lists & Document Libraries into: http://sptest-oklus:29078/sites/projects/NPD/sylvester/
    » Importing: Announcements.cmp ( 1 of 63 )
    » Importing: Calendar.cmp ( 2 of 63 )
    » Importing: Core Team.cmp ( 3 of 63 )
    » Importing: fpdatasources.cmp ( 4 of 63 )
    » Importing: Links.cmp ( 5 of 63 )
    » Importing: Master Page Gallery.cmp ( 6 of 63 )
    Import-SPWeb : There can only be one instance of this list type in a web.

    An instance already exists.
    At E:\Scripts\Import-SPWeb.ps1:30 char:15
    + Import-SPWeb <<<< -Identity $destinationWebURL -path ($path + “\” + $list.Name) -UpdateVersions Overwrite -IncludeUserSecurity -NoLogFile
    + CategoryInfo : InvalidData: (Microsoft.Share…CmdletImportWeb:SPCmdletImportWeb) [Import-SPWeb], SPException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletImportWeb

    » Importing: NPD Milestones.cmp ( 7 of 63 )
    » Importing: Project Information..cmp ( 8 of 63 )
    » Importing: Project Status.cmp ( 9 of 63 )
    » Importing: Shared Documents.cmp ( 10 of 63 )
    Import-SPWeb : A duplicate field name "05b87cf9-063e-45e6-992f-cb3648a1d116" was found.
    At E:\Scripts\Import-SPWeb.ps1:30 char:15
    + Import-SPWeb <<<< -Identity $destinationWebURL -path ($path + “\” + $list.Name) -UpdateVersions Overwrite -IncludeUserSecurity -NoLogFile
    + CategoryInfo : InvalidData: (Microsoft.Share…CmdletImportWeb:SPCmdletImportWeb) [Import-SPWeb], SPException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletImportWeb

    » Importing: Site Assets.cmp ( 11 of 63 )
    » Importing: Site Pages.cmp ( 12 of 63 )
    » Importing: Sylvester Expense Tracking.cmp ( 13 of 63 )
    » Importing: Tasks.cmp ( 14 of 63 )
    » Importing: Team Discussion.cmp ( 15 of 63 )
    Import Complete
    _______________________________________

    I am not concerned about the Master Page Gallery, but the Shared Documents issue has me stuck on this.

    Now that I know that the culprit is on the Shared Documents component, I am unable to locate the troublemaker object.

    Any clue?

    • March 12, 2013 at 7:36 pm

      Hi Marcos

      It’s complaining about a Duplicate Field in your Shared Documents.

      I’ve blogged about this as well.

      http://jshidell.com/2012/01/18/finding-a-duplicate-field-name-was-found-error-in-sharepoint-2010/

      Try to execute this script and let me know what you discover.

      • March 12, 2013 at 7:37 pm

        Sorry Marco not Marcos

      • Marco
        March 12, 2013 at 9:07 pm

        Hi James

        I am using your sample but it returns noting, even when not filtering by Id.

        Here my code:

        $web = Get-SPWeb “http://mysite/sites/projects/npd/Sylver”
        foreach ($field in $web.Fields)
        {
        #if($field.Id -eq “05b87cf9-063e-45e6-992f-cb3648a1d116”)
        #{
        Write-Host “Title: $($field.Title) | ID: $($field.Id)”
        #}
        }
        $web.Dispose()

        If I change the first line to http://mysite, it returns something, but is not what I expected.

        Any idea?

      • March 13, 2013 at 9:58 am

        Hi Marco,

        If you change the script as so, what does is returned? This will loop through all the Webs (Subsites) of the Site Collection (MySites) and searches for this GUID.

        $site = Get-SPSite “http://mysite”
        $webs = $site.AllWebs

        foreach($web in $webs)
        {
        foreach($field in $web.Fields)
        {
        if ($field.id -eq “05b87cf9-063e-45e6-992f-cb3648a1d116”)
        {
        Write-output “Title: $($field.Title) | ID: $($field.id)”
        }
        }
        }
        $site.Dispose()

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: