Creating SharePoint 2010 UPA Properties via Powershell

So I want to say thanks to Vijai

I took his blog post as a starting point, while I had to tweak it a bit, because it threw an error for me, in the end it works a charm!

The code from this post did not work as well (the user props did not show up in the central admin management page.

Here are my comments for Vijai:

changed

$ps = $psm.GetProfileSubtype([Microsoft.Office.Server.UserProfiles.
ProfileSubtypeManager]::GetDefaultProfileName
[Microsoft.Office.Server.UserProfiles.ProfileType]::User))

into

$defaultSubType = [Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$ps = $psm.GetProfileSubtype($defaultSubType)

– the xml allows configuration of IsVisible, the code doesn’t reflect that (static $true).
– for anyone who is looking for IsUserEditable, this is how it would look in you code.

$profileSubTypeProp.IsUserEditable = $true;

Properties.xml
Properties.xml

CreateProperties.ps1

# Load Sharepoint SnapIn
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

# Create Service Context
$site = Get-SPSite http://win-dgkr68kv601/my
$serviceContext = Get-SPServiceContext $site

$upcm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext);
$ppm = $upcm.ProfilePropertyManager
$cpm = $ppm.GetCoreProperties()
$ptpm = $ppm.GetProfileTypeProperties([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$psm = [Microsoft.Office.Server.UserProfiles.ProfileSubTypeManager]::Get($serviceContext)
$defaultSubType = [Microsoft.Office.Server.UserProfiles.ProfileSubtypeManager]::GetDefaultProfileName([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$ps = $psm.GetProfileSubtype($defaultSubType)

[xml]$xmlData=Get-Content "E:\Scripting\Properties.xml"

$pspm = $ps.Properties
$xmlData.UserProfileProperties.Property | ForEach-Object {
$property = $cpm.GetPropertyByName($_.Name)
if($property -eq $null)
{
$Privacy=$_.Privacy
$PrivacyPolicy=$_.PrivacyPolicy

#$property = $properties.Create($false)
$coreProp = $cpm.Create($false)

$coreProp.Name = $_.Name
$coreProp.DisplayName = $_.DisplayName
$coreProp.Description = $_.Description
$coreProp.Type = $_.Type
$coreProp.Length = $_.Length

$cpm.Add($coreProp)

$profileTypeProp = $ptpm.Create($coreProp);

$profileTypeProp.IsVisibleOnEditor = $true;
$profileTypeProp.IsVisibleOnViewer = $true;
$ptpm.Add($profileTypeProp)

$profileSubTypeProp = $pspm.Create($profileTypeProp);

# Public
# Privacy level gives visibility of users' profile properties, and other My Site content, to everyone.
# Contacts
# Privacy level limits the visibility of users' profile properties, and other My Site content, to my colleagues.
# Organization
# Privacy level limits the visibility of users' profile properties, and other My Site content, to my workgroup.
# Manager
# Privacy level limits the visibility of users' profile properties, and other My Site content, to my manager and me.
# Private
# Privacy level limits the visibility of users' profile properties, and other My Site content, to me only.
# NotSet
# Privacy level is not set.
$profileSubTypeProp.DefaultPrivacy = [Microsoft.Office.Server.UserProfiles.Privacy]::$Privacy

# 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.
$profileSubTypeProp.PrivacyPolicy = [Microsoft.Office.Server.UserProfiles.PrivacyPolicy]::$PrivacyPolicy

$profileSubTypeProp.IsUserEditable = $true;

$pspm.Add($profileSubTypeProp)

write-host -f yellow $_.Name property is created successfully
}
else
{
write-host -f red $_.Name property already exists
}
}

DeleteProperties.ps1

# Load Sharepoint SnapIn
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell
}

# Create Service Context
$site = Get-SPSite http://win-dgkr68kv601/my
$serviceContext = Get-SPServiceContext $site

# Get ProfileManager, User Profiles and User Properties
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext)
$properties = $profileManager.get_Properties()
# Create new Property

[xml]$xmlData=Get-Content "E:\Scripting\Properties.xml"

$upcm = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext)
$pdtc = $upcm.GetPropertyDataTypes()
$ppm = $upcm.ProfilePropertyManager
$cpm = $ppm.GetCoreProperties()

$xmlData.UserProfileProperties.Property | ForEach-Object {
#$property = $properties.GetPropertyByName($_.Name)
$property = $cpm.GetPropertyByName($_.Name)
if($property -ne $null)
{
$cpm.RemovePropertyByName($_.Name)
write-host -f yellow $_.Name property is deleted successfully
}
else {
write-host -f red $_.Name property does not exist
}
}

Advertisements

2 Responses to Creating SharePoint 2010 UPA Properties via Powershell

  1. SharePoint Bloke says:

    It doesn’t work though does it?!!! So frustrating. Delete property.ps1 says Stored Procedure Returned an error

    • sp2007hut says:

      Well it did for me. The account you are using (probably farm account) is admin of the user profile service? can you get me more info on the error and maybe some input on what you are trying to do? (delete existing or delete one you just created?)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: