kwizcom banner advertisement

Copy List Items Between SharePoint Lists using PowerShell

Requirement: Copy List Items Between SharePoint Lists using PowerShell
One of our SharePoint list called "News" grown rapidly and wanted to archive its contents to an another list periodically.
Copy List Items Between SharePoint Lists using PowerShell

Solution: So the task is to copy list items between SharePoint Lists. Lets achieve it using PowerShell.
Important: Both lists should have same columns! So, have the source list saved as template and create a new list from it.

PowerShell Script to copy List Items:
#Configuration variables
$WebURL = "http://intranet.crescent.com"
$SourceListName = "News"
$TargetListName= "NewsArchive"

#Get Objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetList = $web.Lists[$TargetListName]

#Get all source items
$SourceColumns = $sourceList.Fields
$SourceItems = $SourceList.GetItems();

#Iterate through each item and add to target list
Foreach($SourceItem in $SourceItems)
{
    $TargetItem = $TargetList.AddItem()
    Foreach($column in $SourceColumns) 
    {
        if($column.ReadOnlyField -eq $False -and $column.InternalName -ne "Attachments")
        {
             $TargetItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)];
        }
    }
   $TargetItem.Update();
}

Copy Attachment between Lists:
If you want to copy list items with attachments, use this script:
#Configuration variables
$WebURL = "http://intranet.crescent.com"
$SourceListName = "News"
$TargetListName= "NewsArchive"

#Get Objects
$web = Get-SPWeb $WebURL
$SourceList = $web.Lists[$SourceListName]
$TargetList = $web.Lists[$TargetListName]

#Get all source items
$SourceColumns = $sourceList.Fields
$SourceItems = $SourceList.GetItems();

#Iterate through each item and add to target list
Foreach($SourceItem in $SourceItems)
{
    $TargetItem = $TargetList.AddItem()
    Foreach($column in $SourceColumns) 
    {
        if($column.ReadOnlyField -eq $False -and $column.InternalName -ne "Attachments")
        {
             $TargetItem[$($column.InternalName)] = $sourceItem[$($column.InternalName)];
        }
    }
    $TargetItem.Update()

    #Copy Attachments
    Foreach($Attachment in $SourceItem.Attachments)
    {
        $spFile = $SourceList.ParentWeb.GetFile($SourceItem.Attachments.UrlPrefix + $Attachment)
        $TargetItem.Attachments.Add($Attachment, $spFile.OpenBinary())
    }
}

What if the column Names are different?
$SourceItems = $SourceList.items
foreach ($SourceItem in $SourceItems) 
{
 write-host -foregroundcolor yellow Copying Item: $SourceItem["Title"]
 $TargetItem = $TargetList.AddItem()
 $TargetItem["Title"] = $SourceItem["Title-Column"]
 $TargetItem["Another-Field"] = $SourceItem["Another-Field"]
 $TargetItem.update()
}
Copy List Items Between SharePoint Lists using PowerShell Copy List Items Between SharePoint Lists using PowerShell Reviewed by Salaudeen Rajack on 2:27 PM Rating: 5

4 comments:

  1. Thank you for the update!

    ReplyDelete
  2. I am receiving error "Exception calling "Update" with "0" argument(s): "To add an item to a document library, use SPFileCollection.Add()" " Please help!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.