I'm working on a Powershell script where I need to copy some list items from one site collection to another (the list/column settings match on both lists). The list item contains two fields of the people picker type. As the source/destination lists are in separate site collections, whereby the users have different ID's I'm trying to retrieve the login name of the users in the people picker column from the source list, hold this as a string in an array, and when creating a new list item I am trying to create an SPUser object based on the login name.
Here's my code to store the original list item in an array:
$aList.Items | foreach {
$SOSUser = $_["Service Owner"]
$SOSUserObject = New-Object Microsoft.SharePoint.SPFieldUserValue ($SourceSite, $SOSUser)
$SOSLoginName = $SOSUserObject.User.UserLogin
$SOSUsername = $SOSLoginName.substring($SOSLoginName.IndexOf("\")+1)
The last 4 lines of the above code is then repeated (changing variable names) for the second people picker field called Service Lead. This information, along with other fields are then copied to an array ...
$aProperties = @{
"ID #" = $_["ID"]
"Service Component" = $_["Service Component"]
"Description" = $_["Service Description"]
"Service Owner" = $SOSUsername
"Service Lead" = $SLSUsername
"Match" = ""
}
$aExport = New-Object PSObject -Property $aProperties
$arrA += $aExport
}
I have a write-host statement that prints the contents of the array $arrA and the correct login names are being stored, so the above code works fine.
I then try to write the contents of $arrA into my destination list as follows, as mentioned have two people picker fields and my array is storing just their login names, in the script below I have tried two slightly different ways of creating an SPUser object to populate the people picker fields in the destination list, neither are working ...
foreach($iC in $arrA)
{
$iCSOLoginname = $iC."Service Owner"
$iCuserSO = Get-SPUser -Web $DestSite -Limit All | Where { $_.UserLogin -eq "i:0#.w|domain\$iCSOLoginname" -and $_.UserLogin.substring(0,7) -eq "i:0#.w|" }
SPFieldUserValue userValueOwner = new SPFieldUserValue($DestSite, $iCuserSO.ID, $iCuserSO.LoginName);
$iCSLLoginname = $iC."Service Lead"
$iCuserSL = Get-SPUser -Web $DestSite -Limit All | Where { $_.UserLogin -eq "i:0#.w|domain\$iCSLLoginname" -and $_.UserLogin.substring(0,7) -eq "i:0#.w|" }
$iCuserSLFUV = SPFieldUserValue userValueLead = new SPFieldUserValue($DestSite,$iCuserSL.ID, $iCuserSL.LoginName);
##New-Object Microsoft.SharePoint. - ommitted from line above to test)
write-host "This is what i'm trying to write to List B ... "
write-host "Service Component ... " $iC."Service Component"
write-host "Description ... " $iC."Description"
write-host "Service Owner (User Object) ... "userValueOwner
write-host "Service Lead (User Object) ... " $iCuserSLFUV
$ItemToAdd = $bList.items.Add()
$ItemToAdd["ID #"] = $iC."ID #"
$ItemToAdd["Service Component"] = $iC."Service Component"
$ItemToAdd["Description"] = $iC."Description"
$ItemToAdd["Service Owner"] = userValueOwner
$ItemToAdd["Service Lead"] = $iCuserSLFUV
$ItemToAdd.Update()
}
In my write-host statements the "Service Owner (User Object) ..." just prints the word userValueOwner, and the write-host "Service Lead (User Object) ..." prints nothing.
The following error messages is generated: userValueOwner : the term 'userValueOwner' is not recognized as the name of a cmdlet, function, script file, or operable program.
The same happens for SPFieldUserValue
After looking online I thought I was using the SPFieldUserValue function correctly - obviously not, can you please assist in how I can change my script to accurately retrieve the user object, to be used in a list item, from the string representation of the login name.
SPFieldUserValue userValueOwner = new SPFieldUserValue($DestSite, $iCuserSO.ID, $iCuserSO.LoginName);
try using$userValueOwner = $DestSite.EnsureUser($iCuserSO.LoginName);
That's assuming$DestSite
is anSPWeb
object.