How To Update SharePoint Online User Profile Properties Using SPFx And Rest API

SharePoint is a content collaboration platform where we can create and share content. The user profile service applications are used in managing the user profiles inside SharePoint tenant. User Profile services use the data from Active Directory Services and keep the user data and AD user data in sync. SharePoint Online has 80+ default User Profile properties. To update the values of User Profile properties, SharePoint has no explicit place other than the SharePoint Admin center and Delve. Delve is limited to some of the User Profile properties and it idoes not allow properties created by the user to be edited.

SPFx is the new Microsoft Office development framework which is used in building customized applications. SPFx applications are installed on the SharePoint site and it will run in the SharePoint user context. So, it is very easy to integrate it with the SharePoint environment. SharePoint has exposed its APIs which is helpful in working closely with its environment. In this article, you will learn to update your user profile data.

Prerequisite

Our first thing is to configure the development environment. You can follow this link to create and work with SPFx apps. After configuring follow the below steps to update the User Profile properties using SPFx.

User Profile Properties

There are a lot of user properties and user profile properties in SharePoint Online site. Before starting with that, you have to know what are all the user profile properties available in SharePoint. Below is the list of user profile properties,

  • AboutMe
  • SPS-LastKeywordAdded
  • AccountName
  • SPS-Locale
  • ADGuid
  • SPS-Location
  • Assistant
  • SPS-MasterAccountName
  • CellPhone
  • SPS-MemberOf
  • Department
  • SPS-MUILanguages
  • EduExternalSyncState
  • SPS-MySiteUpgrade
  • EduOAuthTokenProviders
  • SPS-O15FirstRunExperience
  • EduPersonalSiteState
  • SPS-ObjectExists
  • EduUserRole
  • SPS-OWAUrl
  • Fax
  • SPS-PastProjects
  • FirstName
  • SPS-Peers
  • HomePhone
  • SPS-PersonalSiteCapabilities
  • LastName
  • SPS-PersonalSiteInstantiationState
  • Manager
  • SPS-PhoneticDisplayName
  • Office
  • SPS-PhoneticFirstName
  • PersonalSpace
  • SPS-PhoneticLastName
  • PictureURL
  • SPS-PrivacyActivity
  • PreferredName
  • SPS-PrivacyPeople
  • PublicSiteRedirect
  • SPS-ProxyAddresses
  • QuickLinks
  • SPS-RegionalSettings-FollowWeb
  • SID
  • SPS-RegionalSettings-Initialized
  • SISUserId
  • SPS-ResourceAccountName
  • SPS-AdjustHijriDays
  • SPS-ResourceSID
  • SPS-AltCalendarType
  • SPS-Responsibility
  • SPS-Birthday
  • SPS-SavedAccountName
  • SPS-CalendarType
  • SPS-SavedSID
  • SPS-ClaimID
  • SPS-School
  • SPS-ClaimProviderID
  • SPS-ShowWeeks
  • SPS-ClaimProviderType
  • SPS-SipAddress
  • SPS-ContentLanguages
  • SPS-Skills
  • SPS-DataSource
  • SPS-SourceObjectDN
  • SPS-Department
  • SPS-StatusNotes
  • SPS-DisplayOrder
  • SPS-Time24
  • SPS-DistinguishedName
  • SPS-TimeZone
  • SPS-DontSuggestList
  • SPS-UserPrincipalName
  • SPS-Dotted-line
  • SPS-WorkDayEndHour
  • SPS-EmailOptin
  • SPS-WorkDayStartHour
  • SPS-FeedIdentifier
  • SPS-WorkDays
  • SPS-FirstDayOfWeek
  • Title
  • SPS-FirstWeekOfYear
  • UserName
  • SPS-HashTags
  • UserProfile_GUID
  • SPS-HireDate
  • WebSite
  • SPS-Interests
  • WorkEmail
  • SPS-JobTitle
  • WorkPhone
  • SPS-LastColleagueAdded

Some of these properties are read-only by default. Also, admins can mark these properties read-only. To read the properties from SharePoint, you can use the below API Endpoint.

To get all user properties for current user,

https://<your_site_collection_url>/_api/SP.UserProfiles.PeopleManager/GetMyProperties

To get the user profile properties for specific user,

https://<your_site_collection_url>/_api/SP.UserProfiles.PeopleManager/GetPropertiesFor(accountName=@v)?@v='i:0%23.f|membership|username@yourtenant.onmicrosoft.com'

To get all the user profiles property for a single specific user,

https://<your_site_collection_url>/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v, propertyName='<property_name>')?@v='i:0%23.f|membership|username@yourtenant.onmicrosoft.com'

User profile properties are classified as single and multi-valued. For example, WorkEmail is a single valued property and the SPS-School is a multi-valued property. To update these kinds of values we are going to use the below API end points

https://<your_site_collection_url>/_api/SP.UserProfiles.PeopleManager/SetSingleValueProfileProperty

https://<your_site_collection_url>/_api/SP.UserProfiles.PeopleManager/SetMultiValuedProfileProperty

SPFX application to interact with User Profile properties

In our SPFx application, we are going to have a text box to get the property name input from the user. Then, a GET button to perform the Get operation which gets the current value from the User Profile. Then, an Update button which updates the value of the user profile property that we have chosen.

SharePoint

 

Below is the code snippet to get the user profile property which is chosen through the textbox.
  1. private getValue() {  
  2.     let pptName: any = this.domElement.querySelector('#txtPropertyName');  
  3.     this.propertyName = pptName.value;  
  4.     let apiUrl = this.context.pageContext.web.absoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v, propertyName='" + this.propertyName + "')?@v='" + encodeURIComponent("i:0#.f|membership|") + this.userdetails.loginName + "'";  
  5.     let httpClient: SPHttpClient = this.context.spHttpClient;  
  6.     httpClient.get(apiUrl, SPHttpClient.configurations.v1).then(response => {  
  7.         response.json().then(responseJson => {  
  8.             let newValue: any = this.domElement.querySelector('#txtPropertyValue');  
  9.             newValue.value = responseJson.value;  
  10.         });  
  11.     });  
  12. }  

Below is the code snippet to update the User Profile property value. I am differentiating the multi-value and single-valued properties using a separator “|”. So if you want to update a multivalued property using this below code, make sure that you have given “|” at the suffix like this “Engineering|”.

  1. private updateValue() {  
  2.     let newValue: any = this.domElement.querySelector('#txtPropertyValue');  
  3.     this.value = newValue.value;  
  4.     if (this.value.indexOf("|") !== -1) {  
  5.         this.updateMultiUPValue();  
  6.     } else {  
  7.         this.updateSingleUPValue();  
  8.     }  
  9. }  
  10. updateMultiUPValue() {  
  11.     let apiUrl = this.context.pageContext.web.absoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetMultiValuedProfileProperty";  
  12.     let userData = {  
  13.         'accountName'"i:0#.f|membership|" + this.userdetails.loginName,  
  14.         'propertyName'this.propertyName, //can also be used to set custom single value profile properties  
  15.         'propertyValues'this.value.split("|")  
  16.     }  
  17.     let httpClient: SPHttpClient = this.context.spHttpClient;  
  18.     let spOpts = {  
  19.         headers: {  
  20.             'Accept''application/json;odata=nometadata',  
  21.             'Content-type''application/json;odata=verbose',  
  22.             'odata-version''',  
  23.         },  
  24.         body: JSON.stringify(userData)  
  25.     };  
  26.     httpClient.post(apiUrl, SPHttpClient.configurations.v1, spOpts).then(response => {  
  27.         alert("Updated");  
  28.     });  
  29. }  
  30. updateSingleUPValue() {  
  31.     let apiUrl = this.context.pageContext.web.absoluteUrl + "/_api/SP.UserProfiles.PeopleManager/SetSingleValueProfileProperty";  
  32.     let userData = {  
  33.         'accountName'"i:0#.f|membership|" + this.userdetails.loginName,  
  34.         'propertyName'this.propertyName, //can also be used to set custom single value profile properties  
  35.         'propertyValue'this.value  
  36.     }  
  37.     let httpClient: SPHttpClient = this.context.spHttpClient;  
  38.     let spOpts = {  
  39.         headers: {  
  40.             'Accept''application/json;odata=nometadata',  
  41.             'Content-type''application/json;odata=verbose',  
  42.             'odata-version''',  
  43.         },  
  44.         body: JSON.stringify(userData)  
  45.     };  
  46.     httpClient.post(apiUrl, SPHttpClient.configurations.v1, spOpts).then(response => {  
  47.         alert("Updated");  
  48.     });  
  49. }  

I hope you have understood how to update User Profile properties using SPFx and Rest API. Don’t hesitate to post your thoughts and doubts in the comment box below.

Happy SharePointing!!!