I have the below script that works for moving list data between two lists on sites next to each other that have the same schema. Everything works perfectly bar a column of type LOOKUP named "Name of Project" which steadfastly remains empty. The code works I can write the lookup value ID or Title to the screen or log but cannot make it stick to the target list row.
I have checked the following:
- The lookup list being used for the problem column has the same IDs as the one being used for the source list.
- The internal and external column names on both lists match. External = "Name of Project" Internal = "Project"
Is my code correct? Is there a way to get a more verbose output to see WHY the field fails? -Verbose does naught. (I have even tried manually writing ID;#Text without luck.)
Is there a way to make this dynamic so that it can be run with minimal configuration against lists that have many lookups?
$srcListSiteUrl = "http://intranet/site1";
$SourceListName = "Tasks";
$dstListSiteUrl = "http://intranet/site2";
$DestinationListName = "Tasks";
Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
try
{
$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) {
Write-host "Adding new item $srcListItem";
$newSPListItem = $DestinationList.AddItem();
#Write-Host "- Working with the items column values...";
foreach($spField in $srcListItem.Fields) {
if ($spField.Title -ieq "Name of Project") {
Write-Host "Found lookup column.";
Write-Host "$srcListItem[$spField] is type $spField.Type";
$lookupfieldvalue = $srcListItem[$spField] -as [Microsoft.SharePoint.SPFieldLookupValue];
$newSPListItem[$spField] = $lookupfieldvalue;
} elseif ($spField.Type -ieq "User") {
$newSPListItem[$spField] = $srcListItem[$spField];
} elseif ($spField.Title -ne "Attachments") {
if ($spField.ReadOnlyField -ne $True -and $spField -ne "Attachments") {
$newSPListItem[$spField] = $srcListItem[$spField];
}
}
}
$newSPListItem.Update();
}
} catch {
Write-host $_.exception
} finally {
if($sourceListWeb -ne $null){$sourceListWeb.Dispose()}
if($dstListWeb -ne $null){$dstListWeb.Dispose()}
}
function Get-SPList($webUrl, $lstUrl)
{
$webObj = Get-SPWeb -identity $webUrl;
$lstObj = $webObj.GetList($lstUrl);
return $lstObj;
}