Creating A New User Property Programmatically In SharePoint /O365 using CSOM

Introduction

This article demonstrates how to create and use a user property in SharePoint Online (O365). This article starts with introduction of the User Profile Service. After that, it demonstrates how to work and play with the User Profile Service.

A User Profile is a collection of user properties that describe about a single user. The property includes basic details, policies, and settings. Most of the organizations get a big headache in maintaining User Profile properties in an efficient way but it helps people to find content that connects them with other people.

And based on the User Profile Properties, you can learn many things about the user and connect with them using social features, like blogs and discussions.

The Admin from SharePoint Online can enhance the SharePoint capabilities by adding user profile properties, defining user policies, and creating audiences. Here are the basic screenshots I have added to access the User Profile Service.

Open your SharePoint Central Admin on Server and click Application Management, then select User profile Service.

After this, you will get the new screen. Click the SharePoint link for redirecting to the below screen.


In that User Profile, three important sections are available. We will learn about them one by one later. Now, I have explained about the sections here.

  1. People
    In this section, the Admin can manage all user properties and profiles .They can provide the permissions for our audience here.

  2. Organization
    This sections is for managing organization's details.

  3. My Site Settings

    This is totally for Mysite related topics. My Sites are essentially personal portals that give individual users the ability to have a one-to-many collaboration path with the enterprise.

Now, we will see about Manage User Properties section.

Click the "Manage User Properties" link under the "Peoples" heading. You will be redirected to the below screen.



The basic default properties are available under this link and here, you can use this page to add, edit, organize, delete, or map User Profile properties. Profile properties can be mapped to Active Directory or LDAP compliant directory services.

Profile properties can also be mapped to Application Entity Fields exposed by "Business Data Connectivity".

Here, you are able to create a new user propery and new section. Those details can be explained in the next articles.




Now, we will see how to create a User Profile property programmatically using Client object model.

Specify property settings for this property: The name will be used programmatically for the property by the user profile service, while the display name is the label used when the property is shown.

After the property is created, the only property setting you can change is the display name. Specify a description for this property that will provide instructions or information to users.

We have to put this below DLL to fetch the User profile properties.

Microsoft.SharePoint.Client.UserProfiles.dll assembly (16 version targeted to cloud) and more precisely as follows.

  • Microsoft.SharePoint.Client.UserProfiles.PeopleManager::SetSingleValueProfileProperty()
  • Microsoft.SharePoint.Client.UserProfiles.PeopleManager::SetMultiValuedProfileProperty()

Note  We have to set the permission for UserProfile service in Visual studio ,Set User profile Service Scope to Write .

The below code is for sample for updating the user properties programmatically using CSOM.

Copy the below code and paste it in your Visual Studio.

Source Code 

  1. var Context = SharePointContextProvider.Current.GetSharePointContext(Context);  
  2.        using (var cContext = spContext.CreateUserClientContextForSPHost())  
  3.            {  
  4.            //Get the people manager instance for current context to get account name  
  5.            PeopleManager peopleManager = new PeopleManager(cContext);  
  6.            PersonProperties personProperties = peopleManager.GetMyProperties();  
  7.            cContext.Load(personProperties, p => p.AccountName);  
  8.            cContext.ExecuteQuery();      
  9.            peopleManager.SetSingleValueProfileProperty(personProperties.AccountName, "AboutMe""Test");  
  10.            cContext.ExecuteQuery();  
  11.         
  12.            }   

Conclusion

The above article will help you to understand the basic topic of how to access the SharePoint Online User Profile Property.

Thanks for reading my articles.