Im trying to create a userprofile section but when i run the code Im gettiong the following error you cannot cal a method on a null valued expression.please help me out thanks in advance.
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
[xml]$xmlData = Get-Content "C:\xmlsample.xml"
function CreateUserProfileSection()
{
$SiteUrl = "url"
$site = Get-SPSite $xmlData.UserProfileSection.SiteURL
$context = Get-SPServiceContext ($site)
$upcm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($context);
$ppm = $upcm.ProfilePropertyManager
$cpm = $ppm.GetCoreProperties()
$ptpm = $ppm.GetProfileTypeProperties([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$psm = [Microsoft.Office.Server.UserProfiles.ProfileSubTypeManager]::Get($context)
$ps = $psm.GetProfileSubtype([Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName([Microsoft.Office.Server.UserProfiles.ProfileType]::User))
$pspm = $ps.Properties
$xmlData.UserProfileSection.Section | ForEach-Object{
$property = $pspm.GetSectionByName($_.Name)
if($property -eq $null)
{
$Privacy=$_.Privacy
$PrivacyPolicy=$_.PrivacyPolicy
$coreProp = $cpm.Create($true)
$coreProp.Name = $_.Name
$coreProp.DisplayName = $_.DisplayName
$cpm.Add($coreProp)
write-host -f green $_.Name section is created successfully
}
else
{
write-host -f yellow $_.Name section already exists
}
}
}
CreateUserProfileSection