Saturday, March 21, 2015

Retrieve user profile properties using javascript object model in Sharepoint

With new programming model for Sharepoint Online we need to use client and javascript object models more often than regular server-side object model. And we need to perform basic operations using client API. One of such operations is working with user profile properties. In this post I will show how to retrieve user profile properties using javascript object model. For example let’s use the script which retrieves language and country properties from user profile:

   1: SP.SOD.executeFunc('userprofile', 'SP.UserProfiles.PeopleManager', function () {
   2:     var pmPeopleManager = new SP.UserProfiles.PeopleManager(spContext);
   3:  
   4:     var spSite = spContext.get_site();
   5:     var spWeb = spSite.get_rootWeb();
   6:  
   7:     spContext.load(spSite);
   8:     spContext.load(spWeb);
   9:  
  10:     var currentUser = spWeb.get_currentUser();
  11:     spContext.load(currentUser);
  12:  
  13:     spContext.executeQueryAsync
  14:     (
  15:         Function.createDelegate
  16:         (
  17:            this, function () {
  18:                var strUsername = currentUser.get_loginName();
  19:                var arrProperties = ["DefaultLanguage", "DefaultCountry"];
  20:                var userProperties = new
  21:                  SP.UserProfiles.UserProfilePropertiesForUser(spContext,
  22:                    strUsername, arrProperties);
  23:                var uppProperties =
  24:                    pmPeopleManager.getUserProfilePropertiesFor(userProperties);
  25:  
  26:                spContext.load(userProperties);
  27:                spContext.executeQueryAsync
  28:                (
  29:                    Function.createDelegate
  30:                    (
  31:                       this, function () {
  32:                           var strLanguage = uppProperties[0];
  33:                           var strCountry = uppProperties[1];
  34:                       }
  35:                    ),
  36:                    Function.createDelegate
  37:                    (
  38:                        this, function (sender, args) {
  39:                        }
  40:                    )
  41:                );
  42:            }
  43:         ),
  44:         Function.createDelegate
  45:         (
  46:             this, function (sender, args) {
  47:             }
  48:         )
  49:     );
  50: });

First of all whole script is executed under SP.SOD.executeFunc() in order to ensure that SP.UserProfiles.PeopleManager type is loaded. In script we first asynchronously load current user (lines 5-19). After that we prepare parameters for loading user profile properties (lines 14-23) and retrieve properties by calling SP.UserProfiles.PeopleManager.getUserProfilePropertiesFor() method (lines 24-27). When query is executed we store DefaultLanguage and DefaultCountry user profile properties into local variables for further use (lines 34-35).

This is how you may retrieve use profile properties using javascript object model in Sharepoint. Hope that this information will help someone.

No comments:

Post a Comment