Share via


SharePoint : Effective way of accessing UserProfile properties

Introduction

In multiple scenarios, we need to fetch data from User's properties in SharePoint. SharePoint provides many ways to access UserProfiles's data via. REST, SPService, CSOM, JSOM, .Net.But while accessing user specific data, we need to be careful mostly on the amount to data getting returned.

Details

If we do a general User Profile call is quite heavy on SharePoint server and can affect the Performance.
We will be using REST api for our example, when we use /_api/SP.UserProfiles.PeopleManager/GetMyProperties it includes “ExtendedManagers” , “Peers” , “Manager” along with UserProfileProperties.
 
 

These fields can have lot of information and it perform multiple joins back in SQL which is a heavy operation. So, be careful while using 'GetMyProperties'. Recommended approach is to only fetch properties which are needed. There are multiple ways to do so,

Basic properties we can directly get using $select

e.g.  /_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=AccountName,Title
Following are the list of Basic properties

If we need to get all the UserProfileProperties and Organization specific properties use $select=UserProfileProperties. This will give list all of all UserProfileProperties without extra information.

In case we need to get some specific extended properties /Organization specific propertie, we cannot use $expand on UserProfileProperties. For this we need to use ‘GetUserProfilePropertyFor’

var targetUser = "i:0#.f|membership|" + _spPageContextInfo.userLoginName;
 var url = _spPageContextInfo.siteAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetUserProfilePropertyFor(accountName=@v,propertyName='<PropertyName>')?@v=%27" + encodeURIComponent(targetUser) + "%27";

This will be a very small call and will only provide the required data.

In case we need to get multiple properties

SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function  () {
   var clientContext = SP.ClientContext.get_current();
   var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
 
   var targetUser = "i:0#.f|membership|" + _spPageContextInfo.userLoginName;
   var props = ['PROP1', 'PROP2'];
   var userProfilePropertiesForUser = new SP.UserProfiles.UserProfilePropertiesForUser(clientContext, targetUser, props);
   var personProperties = peopleManager.getUserProfilePropertiesFor(userProfilePropertiesForUser);
   clientContext.load(userProfilePropertiesForUser);
 
   clientContext.executeQueryAsync(function () {
           var PROP1 = personProperties[0];
           var PROP2 = personProperties[1];                      
           },
           function () {
             console.log("Error");              
           });
 });

These are some effective way on how to use REST api, same stand correct in other way of access User's property.

See Also