After the theory of properties in the first part now let’s see the code itself.
Create a new console application and add references to Microsoft.Office.Server, Microsoft.Office.Server.UserProfiles, Microsoft.SharePoint, Microsoft.SharePoint.Taxonomy and System.Web assemblies.
For the sake of simplicity we store manager classes as instance variables.
In the constructor we initialize the instance variables we defined above.
This helper method is useful to enumerate different type of properties when working with the application.
Next, we define some helper methods that we use to determine the display order of the properties we create.
The first one is the GetDisplayOrderForLastSection method that returns the DisplayOrder property of the last section. We use the value later in the CreateOrUpdateSection method to get the DisplayOrder of a new section.
The GetLastDisplayOrderForSection method makes it easy to get the DisplayOrder property of the last property within the specified section.
The GetSectionForProperty method returns the section the specified property belongs to.
The CreateOrUpdateSection method creates a section based on the specified name and display name.
The CreateOrUpdateProfileTypeProperty method creates or gets the specified type property and sets the type property level settings: the IsVisibleOnViewer and the IsVisibleOnEditor. We could set CoreProperty type specific values here as well, like the MaximumShown property for mulivalue string profile properties, but I wouldn’t like to make the example more complex than necessary.
The CreateOrUpdateSubtypeProperty method gets the type property first by calling the CreateOrUpdateProfileTypeProperty method, then sets subtype property level settings like IsUserEditable, DefaultPrivacy, and PrivacyPolicy. If a section is specified and it is not the actual section of the subtype property, then the new DisplayOrder value is set either.
The CreateOrUpdateDateProperty and the CreateOrUpdateIntProperty methods simply calls forward to the CreateOrUpdateProperty method specifying the requested property type date and integer.
The CreateOrUpdateStringProperty method is a bit more complex. It contains an extra Action for the CustomProperty that is run when the property is created and sets some string property type specific values like Length, IsMultivalued and TermSet. TermSet can be set on an existing CoreProperty instance, so we set it in the Action that is defined for the update either. The Length, IsMultivalued properties cannot set on an existing instance.
The CreateOrUpdateCoreProperty method creates the requested CoreProperty or returns an existing instance and sets the properties that are common for each property type, like Name, Type and DisplayName. Property type specific settings can be injected using the Action instances that are run after property creation or update.
The CreateOrUpdateProperty method is a wrapper over the CreateOrUpdateCoreProperty and the CreateOrUpdateSubtypeProperty methods and serves as a helper method for the property type specific methods we saw above (CreateOrUpdateDateProperty, CreateOrUpdateIntProperty, CreateOrUpdateStringProperty).
The following code block shows some examples of these methods. It creates a new section and adds a few profile properties to the new section. Of course, you should change the taxonomy related values, the term store, the group and the term set name to something existing one in your environment.
The code finally starts the profile information synchronization process that is the UserProfile – Profile Synchronization Job of the User Profile Service. See this post for details and code of the StartServiceJob method. It makes the time the new section and properties appear on the Edit User Profile Property management user interface shorter, although not immediate, so you have to wait a few seconds for the results.