Plexus81
Plexus81

Reputation: 1251

Update custom user profile properties - Powershell - SharePoint

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
}
}

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.

Upvotes: 0

Views: 1335

Answers (1)

sqlsolver
sqlsolver

Reputation: 91

One item that jumps out is that you're disposing of your SPSite object before your code has opportunity to make use of it. Move ' $site.Dispose(); ' to the end of your code block.

Upvotes: 1

Related Questions