SharePoint Gen

The official blog of Chandrasekar, Microsoft SharePoint PFE

PowerShell script to create a new User Profile Property

PowerShell script to create a new User Profile Property

Rate This
  • Comments 4
 
  #Load SharePoint User Profile assemblies
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
 
#Function to get service context
function Get-SPServiceContext([Microsoft.SharePoint.Administration.SPServiceApplication]
 $profileApp)
{
 $profileApp = @(Get-SPServiceApplication | ? 
 {$_.TypeName -eq "User Profile Service Application"})[0]

return [Microsoft.SharePoint.SPServiceContext]::GetContext

($profileApp.ServiceApplicationProxyGroup,

[Microsoft.SharePoint.SPSiteSubscriptionIdentifier]::Default)

}
 
#Get UserProfileManager 
$serviceContext = Get-SPServiceContext
$userProfileConfigManager = New-Object Microsoft.Office.Server.UserProfiles.
 UserProfileConfigManager($serviceContext)
$userProfilePropertyManager = $userProfileConfigManager.ProfilePropertyManager
$userProfilePropertyManager = $userProfilePropertyManager.GetCoreProperties()

$userProfileTypeProperties = $userProfilePropertyManager.GetProfileTypeProperties

([Microsoft.Office.Server.UserProfiles.ProfileType]::User)

$userProfileSubTypeManager = [Microsoft.Office.Server.UserProfiles.
 ProfileSubTypeManager]::Get($serviceContext)

$userProfile = $userProfileSubTypeManager.GetProfileSubtype

([Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName

([Microsoft.Office.Server.UserProfiles.ProfileType]::User))

$userProfileProperties = $userProfile.Properties 
 
#Set Custom Property values
$PropertyName = "MyProperty"
$PropertyDisplayName = "MyCustomProperty" 
$Privacy="private"
$PrivacyPolicy="mandatory"
$coreProperty = $userProfilePropertyManager.Create($false)
$coreProperty.Name = $PropertyName
$coreProperty.DisplayName = $PropertyDisplayName
$coreProperty.Type = "string"
$coreProperty.Length = "50"
 
#Add Custom Property
$userProfilePropertyManager.Add($coreProperty)
$profileTypeProperty = $userProfileTypeProperties.Create($coreProperty)
 
#Display Settings
 
#Show on the Edit Details page
$profileTypeProperty.IsVisibleOnEditor = $true
 
#Show in the profile properties section of the user's profile page
$profileTypeProperty.IsVisibleOnViewer = $true
 
#Show updates to the property in newsfeed
$profileTypeProperty.IsEventLog = $true
 
$userProfileTypeProperties.Add($profileTypeProperty)
$profileSubTypeProperty = $userProfileProperties.Create($profileTypeProperty)
$profileSubTypeProperty.DefaultPrivacy =[Microsoft.Office.Server.UserProfiles.Privacy]
 ::$Privacy

$profileSubTypeProperty.PrivacyPolicy =

[Microsoft.Office.Server.UserProfiles.PrivacyPolicy]::$PrivacyPolicy

$userProfileProperties.Add($profileSubTypeProperty)
 
#Add New Mapping for synchronization user profile data
#SharePoint Synchronization connection
$connectionName ="connectionname"
 
#Attribute name in FIM/LDAP source
$attributeName ="attributename"
 
$synchConnection = $userProfileConfigManager.ConnectionManager[$connectionName]
$synchConnection.PropertyMapping.AddNewMapping
([Microsoft.Office.Server.UserProfiles.ProfileType]::User,$PropertyName,$attributeName)
 
Privacy is an Enum, which has these values,

Privacy Member Name

Description

Public

Everyone

Contacts

Colleagues

Organization

Workgroup

Manager

Manager and me

Private

Me

NotSet

Not set

 

PrivacyPolicy is an Enum, which has these values,

Privacy Policy Name

Description

Mandatory

Makes it a requirement that the user fill in a value.

OptIn

Opt-in to provide a privacy policy value for a property.

OptOut

Opt-out from providing a privacy policy value for a property.

Disabled

Turns off the feature and hides all related user interface

Setting Core Properties - http://msdn.microsoft.com/en-us/library/ee578663.aspx

Add New Mapping - http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.propertymapcollection.addnewmapping.aspx

Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post
  • Feel free to post this on the Microsoft ScriptCenter Repository so that others can find it easily.

    gallery.technet.microsoft.com/scriptcenter

  • $profileTypeProperty = $userProfileTypeProperties.Create($coreProperty) throwing null object error on SP 2013.

  • Exception Message:

    You cannot call a method on a null-valued expression.

    At E:\Sudarshan\User Profile\Moodys_UPA_CustomeProperties.ps1:57 char:1

    + $profileTypeProperty = $userProfileTypeProperties.Create($coreProperty)

    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

       + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

       + FullyQualifiedErrorId : InvokeMethodOnNull

  • RESOLVED ALL THE ISSUES EXCEPT:

    Return zero connection //$userProfileConfigManager.ConnectionManager.count is zero

    $synchConnection = $userProfileConfigManager.ConnectionManager[$connectionName]

Page 1 of 1 (4 items)