Copy or Replace list items in PowerShell

Introduction

Based on the request from one of the audience of adicodes, I am providing one post which copies items from one list to another.

If the destination list already has the item of the source list, the following code will update the item. If the item does not exist it will create a new item

Copying/replacing items from source list to destination list

Following code copies/replace items from the list “SourceList” to the list “DestinationList”. Both lists are having same schema.

The key reference column for both the list is ‘Title’ column.









It checks in the ‘DestinationList’ if the ‘Title’ value is already exists or not. If exists item will updated if not item will be created

Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue try { $srcListSiteUrl = "http://adisgroup/myteamsite" $SourceListName = "SourceList" $dstListSiteUrl = "http://adisgroup/myteamsite" $DestinationListName = "DestinationList" $keyColumnInternalName = "Title" $sourceListWeb = Get-SPWeb -identity $srcListSiteUrl $sourceListUrl = $sourceListWeb.ServerRelativeUrl + "/lists/" + $SourceListName; $dstListWeb = Get-SPWeb -identity $dstListSiteUrl $destinationListUrl = $dstListWeb.ServerRelativeUrl + "/lists/" + $DestinationListName; $SourceList = $sourceListWeb.GetList($sourceListUrl); $DestinationList = $dstListWeb.GetList($destinationListUrl); $sourceSPListItemCollection = $SourceList.GetItems(); foreach($srcListItem in $sourceSPListItemCollection) { #CAML query of the common column (generally the title column or any unique column) $keyValue = $srcListItem[$keyColumnInternalName] $camlQuery = "<Where> <Eq> <FieldRef Name=" + $keyColumnInternalName + " /> <Value Type='Text'>" + $keyValue + "</Value> </Eq> </Where>" $spQuery = new-object Microsoft.SharePoint.SPQuery $spQuery.Query = $camlQuery $spQuery.RowLimit = 1 #check if the item is already present in destination list $destItemCollection = $DestinationList.GetItems($spQuery) if($destItemCollection.Count -gt 0) { write-host "list item already exists, updating " foreach($dstListItem in $destItemCollection) { foreach($spField in $dstListItem.Fields) { if ($spField.ReadOnlyField -ne $True -and $spField.InternalName -ne "Attachments") { $dstListItem[$spField.InternalName] = $srcListItem[$spField.InternalName]; } } # Handle Attachments foreach($leafName in $srcListItem.Attachments) { $spFile = $SourceList.ParentWeb.GetFile($srcListItem.Attachments.UrlPrefix + $leafName) $dstListItem.Attachments.Add($leafName, $spFile.OpenBinary()); } $dstListItem.Update() } } else { write-host "adding new item" $newSPListItem = $DestinationList.AddItem(); foreach($spField in $srcListItem.Fields) { if ($spField.ReadOnlyField -ne $True -and $spField.InternalName -ne "Attachments") { $newSPListItem[$spField.InternalName] = $srcListItem[$spField.InternalName]; } } # Handle Attachments foreach($leafName in $srcListItem.Attachments) { $spFile = $SourceList.ParentWeb.GetFile($srcListItem.Attachments.UrlPrefix + $leafName) $newSPListItem.Attachments.Add($leafName, $spFile.OpenBinary()); } $newSPListItem.Update() } } } catch { write-host $_.exception } finally { if($sourceListWeb -ne $null){$sourceListWeb.Dispose()} if($dstListWeb -ne $null){$dstListWeb.Dispose()} }

Usage

In the above code, there are five custom variables

$srcListSiteUrl : Site url where the source list is present

$SourceListName : Name of the source list

$dstListSiteUrl : Site url where the destination list is present (In the above code both lists are in the same site)

$DestinationListName : Name of the destination list

$keyColumnInternalName : Column name with which we have to check whether to insert or update.

Note, in the above example $keyColumnInternalName value is ‘Title’. It is Text type. So, the CAML query variable $camlQuery is constructed in that way.

If you have different data type column which you want to use as a reference column, you have to change the variable accordingly as the column type.

Conclusion

Hope you understand the usage of the above code for copying and replacing items from source list to destination list.

Let me know if you have any comments