Skip to main content

Cloud

Creating and Defining ProfileSubtypes Programmatically

ProfileSubtypes allow administrators to create alternate views for different user to display or hide different user profile properties. However, going through the process of manually changing each profile property could be substantial if there are numerous profile properties on your UserProfileServiceApplication. Therefore, being able to automate the process with a feature, service, or console application would greatly reduce the time spent creating and defining the ProfileSubtypes.
Adding new ProfileSubtypes requires that you know what type of ProfileSubtype you wish to create. There are 3 different ProfileTypes to choose from: Organization, Group, and User. The explanation of the different ProfileTypes can be found here. The SPSite represents a site that has the UserProfileServiceApplication attached to it. The code needed to create a ProfileSubtype is shown below.

1: // Create ProfileSubTypes 2: public void CreateProfileSubtpye(string name, string displayName, ProfileType type, SPSite site) 3: { 4: SPServiceContext serviceContext = SPServiceContext.GetContext(site); 5: ProfileSubtypeManager _profileSubtypeManager = ProfileSubtypeManager.Get(serviceContext); 6: 7: if (_profileSubtypeManager.GetProfileSubtype(name) == null) 8: _profileSubtypeManager.CreateSubtype(name, displayName, type); 9: }

.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode, .ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode pre
{margin:0em;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .rem
{color:#008000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .str
{color:#006080;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .op
{color:#0000c0;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .preproc
{color:#cc6633;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .html
{color:#800000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .attr
{color:#ff0000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .lnum
{color:#606060;}
There are 2 different approaches available to define the profile properties to include in ProfileSubtypes. This is due to the fact that when a new ProfileSubtype is added, all properties that are in the default ProfileSubtype are added to the newly created one.
Approach 1:
The first approach should be used if you have yet to create any custom properties that will be shown or hidden by a ProfileSubtype. In this case, it would be better to create a the subtypes before creating the rest of the profile properties then adding those that belong to that ProfileSubtype after the property is created. This is easier to create as you will just need to know what properties you want to show for that specific ProfileSubtype. The code needed to add profile properties to ProfileSubtypes is shown below.

1: // Take list of Property Names and Add them to a ProfileSubtype 2: public void CreateProfileSubtypePropertyGroup(string subtypeName, List<string> properties, SPSite site) 3: { 4: foreach (string property in properties) 5: { 6: SPServiceContext serviceContext = SPServiceContext.GetContext(site); 7: UserProfileManager _userProfileManager = new UserProfileManager(serviceContext); 8: ProfileSubtypePropertyManager _profileSubtypePropertyManager = _userProfileManager.DefaultProfileSubtypeProperties; 9: ProfileSubtypeProperty sProperty = _profileSubtypePropertyManager.GetPropertyByName(property); 10: 11: if (sProperty == null) 12: sProperty = _profileSubtypePropertyManager.GetSectionByName(property); 13: 14: AddPropertyToSubtype(subtypeName, sProperty, serviceContext); 15: } 16: } 17: 18: // Adding property to a ProfileSubType 19: private void AddPropertyToSubtype(string subtypeName, ProfileSubtypeProperty property, SPServiceContext serviceContext) 20: { 21: ProfileSubtypeManager _profileSubtypeManager = ProfileSubtypeManager.Get(serviceContext); 22: foreach (ProfileSubtype subtype in _profileSubtypeManager.GetSubtypesForProfileType(ProfileType.User)) 23: { 24: if (subtype.Name.Equals(subtypeName, StringComparison.CurrentCultureIgnoreCase)) 25: { 26: bool flag = false; 27: foreach (ProfileSubtypeProperty sprop in subtype.PropertiesWithSection) 28: { 29: if (sprop.Name.Equals(property.CoreProperty.Name, StringComparison.CurrentCultureIgnoreCase)) 30: { 31: flag = true; 32: break; 33: } 34: } 35: if (!flag) 36: { 37: ProfileSubtypeProperty tempProp = subtype.Properties.Create(property.TypeProperty); 38: 39: if (!property.IsSection) 40: { 41: tempProp.IsUserEditable = property.IsUserEditable; 42: tempProp.PrivacyPolicy = property.PrivacyPolicy; 43: tempProp.DefaultPrivacy = property.DefaultPrivacy; 44: tempProp.UserOverridePrivacy = property.UserOverridePrivacy; 45: } 46: 47: subtype.Properties.Add(tempProp); 48: } 49: } 50: } 51: }

.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode, .ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode pre
{margin:0em;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .rem
{color:#008000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .str
{color:#006080;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .op
{color:#0000c0;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .preproc
{color:#cc6633;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .html
{color:#800000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .attr
{color:#ff0000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .lnum
{color:#606060;}

.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode, .ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode pre
{margin:0em;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .rem
{color:#008000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .str
{color:#006080;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .op
{color:#0000c0;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .preproc
{color:#cc6633;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .html
{color:#800000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .attr
{color:#ff0000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .lnum
{color:#606060;}
Approach 2:
The second approach should be used if the user profile properties have already been created. After creating the new ProfileSubtype, removing the properties that should not be in the ProfileSubtype is necessary to define the view. The list of properties required for this approach is all of the properties that should not be in the ProfileSubtype. The code needed to remove the profile properties from a ProfileSubtype is shown below.
 
 
 

1: // Take list of Property Names and Remove them from a ProfileSubtype 2: public void RemovePropertiesFromProfileSubtypePropertyGroup(string subtypeName, List<string> properties, SPSite site) 3: { 4: foreach (string property in properties) 5: { 6: SPServiceContext serviceContext = SPServiceContext.GetContext(site); 7: UserProfileManager _userProfileManager = new UserProfileManager(serviceContext); 8: ProfileSubtypePropertyManager _profileSubtypePropertyManager = _userProfileManager.DefaultProfileSubtypeProperties; 9: ProfileSubtypeProperty sProperty = _profileSubtypePropertyManager.GetPropertyByName(property); 10: 11: if (sProperty == null) 12: sProperty = _profileSubtypePropertyManager.GetSectionByName(property); 13: 14: RemovePropertyToSubtype(subtypeName, sProperty, serviceContext); 15: } 16: } 17: // Remove property from a SubType 18: private void RemovePropertyToSubtype(string subtypeName, ProfileSubtypeProperty property, SPServiceContext serviceContext) 19: { 20: ProfileSubtypeManager _profileSubtypeManager = ProfileSubtypeManager.Get(serviceContext); 21: foreach (ProfileSubtype subtype in _profileSubtypeManager.GetSubtypesForProfileType(ProfileType.User)) 22: { 23: if (subtype.Name.Equals(subtypeName, StringComparison.CurrentCultureIgnoreCase)) 24: { 25: if (subtype.Properties.GetPropertyByName(property.Name) != null || subtype.Properties.GetSectionByName(property.Name) != null) 26: { 27: if (property.IsSection) 28: subtype.Properties.RemoveSectionByName(property.Name); 29: else 30: subtype.Properties.RemovePropertyByName(property.Name); 31: } 32: } 33: } 34: }

.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode, .ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode pre
{font-size:small;color:black;font-family:consolas, “Courier New”, courier, monospace;background-color:#ffffff;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode pre
{margin:0em;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .rem
{color:#008000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .kwrd
{color:#0000ff;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .str
{color:#006080;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .op
{color:#0000c0;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .preproc
{color:#cc6633;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .asp
{background-color:#ffff00;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .html
{color:#800000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .attr
{color:#ff0000;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .alt
{background-color:#f4f4f4;width:100%;margin:0em;}
.ExternalClass5DBC3E0EE9094ED8895EA79FAE7629DB .csharpcode .lnum
{color:#606060;}

 

Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.