SharePoint – Use PowerShell to copy Library items and column data to another Library


Hello everyone. A few days ago a colleague asked me if there is a quick way to copy Library items and column data to another Library. The reason he asked was because he wanted to create a new website to replace his old one which would satisfy the needs of his users better and he needed all the data stored in that Document Library. This Library contained about 2000 items so he asked me for help since moving them manually would be a real pain. I found some great posts online and came up with a PowerShell script as solution for his problem.

Here is my script:


Param([parameter(Mandatory=$true)][alias("SourceWebUrl")]$SPSourceWebUrl, [parameter(Mandatory=$true)][alias("SourceListName")]$SPSourceListName, [parameter(Mandatory=$true)][alias("DestinationWebUrl")]$SPDestinationWebUrl, [parameter(Mandatory=$true)][alias("DestinationListName")]$SPDestinationListName)

#Add SharePoint PowerShell SnapIn if not already added
if((Get-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

#Get data of SharePoint Source List
$SPSourceWeb = Get-SPWeb $SPSourceWebUrl
$SPSourceList = $SPSourceWeb.Lists["$SPSourceListName"]
$SPSourceListColumns = $SPSourceList.Fields
$SPSourceListItems = $SPSourceList.Items

#Get data of SharePoint Destination List
$SPDestinationWeb = Get-SPWeb $SPDestinationWebUrl
$SPDestinationList = $SPDestinationWeb.Lists["$SPDestinationListName"]

#Iterate through all SharePoint List items of Source
foreach($SPSourceListItem in $SPSourceListItems)
{
#Copy data from Source and create a new item in Destination
$SPNewListItem = $SPDestinationList.RootFolder.Files.Add($SPSourceListItem.Name, $SPSourceListItem.File.OpenBinary()).Item;;

#Iterate through each column in Source
foreach($SPSourceListColumn in $SPSourceListColumns)
{
#Ignore Read only fields like Created, Modified, etc.
if($SPSourceListColumn.ReadOnlyField -ne $True)
{
#Copy column data from Source to Destination
$SPNewListItem[$($SPSourceListColumn.InternalName)] = $SPSourceListItem[$($SPSourceListColumn.InternalName)];
}
}
#Update current item
$SPNewListItem.Update();
}

The script will do the following:

It will gather all data from the Source Library and create new items in the Destination List. After that the script will update the new items with the column data from the Source Library. The script will ignore Read only fields like Created, Modified, etc.

Important:

Please keep the following things in mind when you run this script:

  • The script will only copy the latest version of the items. For example: Only the latest Draft or Published Version. All previous versions will not be copied to the Destination Library. (This wasn´t a problem for me since my colleague did not need the old versions.)
  • If some columns do not exist at the Destination they will simply be ignored. You will not receive errors or notifications.
  • You need to run this script on a server of your SharePoint Farm and need to use an account with appropriate permissions. Otherwise you will not be able to run it. It is also important to know that the column values for the fields Created by and Modified by will be set to this account. This means you will lose the information who created or modified items when you copy them using this script.

As always you can get my script from here.

That´s it. I hope my post was useful for you.

Sources:

http://myspworld.wordpress.com/2012/06/21/copying-files-between-document-libraries/

http://letitknow.wordpress.com/2011/03/17/100/

http://www.sharepointed.com/2011/06/17/copy-files-to-another-document-library-maintain-metadata/

Advertisements
Tagged ,

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: