0

EDIT

I have a created the property now using below url. now I want to assign a custom subtype to the section property and the existing property by using powershell. Any inputs?

 // Take list of Property Names and Remove them from a ProfileSubtype 
public void RemovePropertiesFromProfileSubtypePropertyGroup(string subtypeName, List<string> properties, SPSite site) 
{ 
foreach (string property in properties) 
 { 
 SPServiceContext serviceContext = SPServiceContext.GetContext(site); 
UserProfileManager _userProfileManager = new UserProfileManager(serviceContext);
ProfileSubtypePropertyManager _profileSubtypePropertyManager = _userProfileManager.DefaultProfileSubtypeProperties; 
ProfileSubtypeProperty sProperty = _profileSubtypePropertyManager.GetPropertyByName(property); 

 if (sProperty == null) 
sProperty = _profileSubtypePropertyManager.GetSectionByName(property); 

RemovePropertyToSubtype(subtypeName, sProperty, serviceContext); 
} 
} 
// Remove property from a SubType 
private void RemovePropertyToSubtype(string subtypeName, ProfileSubtypeProperty property, SPServiceContext serviceContext) 
{ 
ProfileSubtypeManager _profileSubtypeManager = ProfileSubtypeManager.Get(serviceContext); 
foreach (ProfileSubtype subtype in _profileSubtypeManager.GetSubtypesForProfileType(ProfileType.User)) 
{ 
if (subtype.Name.Equals(subtypeName, StringComparison.CurrentCultureIgnoreCase)) 
{ 
if (subtype.Properties.GetPropertyByName(property.Name) != null || subtype.Properties.GetSectionByName(property.Name) != null) 
{ 
if (property.IsSection) 
subtype.Properties.RemoveSectionByName(property.Name); 
  else 
subtype.Properties.RemovePropertyByName(property.Name); 
} 
} 
} 
}
4
  • What error? Are you a UPA Admin? Commented Jul 16, 2015 at 13:49
  • Actually, first time when i execute the script, no error comes, but then Section property is not added if i check in Manage User Property. And when the script is executed second time then the error comes "Duplicate property cannot be added"
    – Deepika
    Commented Jul 16, 2015 at 14:06
  • Is the section visible? What happens when you enumerate the sections with PoSh, do you see the new section? Commented Jul 16, 2015 at 14:33
  • ok I did that using this thread: social.technet.microsoft.com/Forums/en-US/…. Now I want to add a subtype to the already created property. Any inputs how to do that in powershell?
    – Deepika
    Commented Jul 17, 2015 at 11:08

2 Answers 2

1

I would avoid the use of user profile subtypes and organizations. Neither was fully implemented and the support for subtypes is weak.

8
  • yes, even on internet there is so less the info for the same. But I got some bits of code in c# but I am facing issues converting it to powershell. Updating the c# code in question. Could you help me in this?
    – Deepika
    Commented Jul 17, 2015 at 14:37
  • Like I said, I avoid using subtypes. What is your use case? How do you plan to use them? Commented Jul 18, 2015 at 16:41
  • I have to add a new section and a property. Also I have to hide 2 sections and some properties. When i create a new subtype, it is automatically assigned to the existing properties. If we remove the "Default User profile subtype" from the properties to be removed; they will be hidden. So I am trying to remove the subtype from the existing properties.
    – Deepika
    Commented Jul 20, 2015 at 6:14
  • No, I mean what are you using the subtype for? If you just need to hide properties you don't need subtypes. Commented Jul 21, 2015 at 13:24
  • 1
    You can change the Visibility and Edit-ability using IsVisibleOnEditor msdn.microsoft.com/en-us/library/office/… Commented Jul 22, 2015 at 14:40
0

This is the working code for hiding a property

##########CODE########################
# Load SharePoint.PowerShell snapin 
if (!(Get-PSSnapin | ?{$_.name -eq 'Microsoft.SharePoint.PowerShell'})) 
{ 
    if(Get-PSSnapin -registered | ?{$_.name -eq 'Microsoft.SharePoint.PowerShell'}) 
    { 
        Add-PSSnapin Microsoft.SharePoint.PowerShell 
        write-host "Loading Microsoft.SharePoint.PowerShell in session" 
    } 
    else 
    { 
        write-host "Microsoft.SharePoint.PowerShell is not registered with the system." 
        break 
    } 
} 
else 
{ 
    write-host "Microsoft.SharePoint.PowerShell is already loaded" 
} 
$ScriptPath = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$logcsv = $ScriptPath + "\HideProperty.csv"     #output csv format
Set-Content -Path $logcsv -Value $header -Encoding UTF8
#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)
}

$stackTrace = ""

try{
####Get UserProfileManager
$stackTrace = "Error in: User Profile Objects"
$serviceContext = Get-SPServiceContext
$userProfileConfigManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext)
$userProfilePropertyManager = $userProfileConfigManager.ProfilePropertyManager
$userProfileTypeProperties = $userProfilePropertyManager.GetProfileTypeProperties([Microsoft.Office.Server.UserProfiles.ProfileType]::User)
$corePropertyManager = $userProfilePropertyManager.GetCoreProperties()
$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 

Write-Host "User Profile Objects setup complete"

Write-Host "Updating Properties..."


####Set Core Property values
$stackTrace += "Error in: Core Property"

$PropertyName = "EmailVersion"
$coreProperty = $userProfileProperties.GetPropertyByName($PropertyName)

#### Add Profile Type Property
$stackTrace += "Error in: Profile Type Property"
Write-Host "Changing Profile Type Property settings"
$profileTypeProperty = $userProfileTypeProperties.GetPropertyByName($coreProperty.Name)
if($profileTypeProperty ){
Write-Host "Updating... profileType Property: " $PropertyName
$profileTypeProperty.IsVisibleOnEditor = $IsPropertyIsVisibleOnEditor
$profileTypeProperty.Commit()
}
else{
    Write-Host -ForegroundColor Red "Property: " $PropertyName  " does'nt exist."
    $PropertyName + " does'nt exist."  | Add-Content -Path $logcsv      
}


catch{
Write-Host -ForegroundColor Red $_.Exception.Source
Write-Host -ForegroundColor Red $_.Exception.Message
Write-Host -ForegroundColor Red $_.Exception.StackTrace
$_.Exception.Source + ";"+ $_.Exception.Message +";" + $_.Exception.StackTrace  | Add-Content -Path $logcsv
}

 ##################################

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.