Pages

Tuesday, June 9, 2015

How to get SharePoint User by User Id in JavaScript using JSOM

Solution

  1. function getUserById() {
  2.  
  3.     var context = new SP.ClientContext.get_current();
  4.     var user = context.get_web().get_siteUsers().getById('1073741823');//Give the user id here
  5.     context.load(user);
  6.     context.executeQueryAsync(
  7.          Function.createDelegate(null, ensureUserSuccess),
  8.          Function.createDelegate(null, onFail)
  9.     );
  10.  
  11.     function ensureUserSuccess() {
  12.         //You can access the properties here
  13.         alert(user.get_title());
  14.         alert(user.get_loginName());
  15.     }
  16.  
  17.     function onFail() {
  18.         alert('Fail');
  19.     }
  20. }