Having problem updating custom user profile properties. Anyone with the same problem, code below:
[void][reflection.assembly]::Loadwithpartialname("Microsoft.Office.Server");
$site=new-object Microsoft.SharePoint.SPSite("$ProfilFullURL");
$serviceContext = Get-SPServiceContext $site;
$site.Dispose();
$upm = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);
function Update-UserProfileProperty ($property, $excelProperty)
{
Write-Host "Setting property $property : " -NoNewline
Try
{
$userProfile["$property"].Value = $excelProperty;
$userProfile.Commit()
Write-Host "$excelProperty" -ForegroundColor Green
}
Catch [system.exception]
{
Write-Host "Error" -ForegroundColor Red
}
}
Foreach ($user in $userobjects)
{
$userProfile = Get-UserProfileProperties $user
}
function Get-UserProfileProperties ($user)
{
$userName = $user.SM_LoginName
Write-Host "$userName : Finding user profile ..." -NoNewline
Try
{
$userProfile = $upm.GetUserProfile("$userName")
Write-Host "Found" -ForegroundColor Green
UserProfileProperty $user
}
Catch [system.exception]
{
Write-Host "Not found" -ForegroundColor Red
return
}
return $userProfile
}
What I'm trying to do is to add migrate data from a excel spreadsheet into the custom user profile property. The user and property exist!
The code works with standard properties like FirstName etc.
UPDATE The error I got is
The property 'Value' cannot be found on this object. Verify that the property exists and can be set. At line:1 char:1 + $userProfile[$property].Value = $excelProperty + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : PropertyNotFound
Got this error ONLY on custom user properties