2
\$\begingroup\$

I have created JavaScript code using REST API for getting the username of a SharePoint user from an active directory if you know the ID. Please suggest any improvements.

// get user name from id
function getUserFromId(userid) {

// var userid = _spPageContextInfo.userId;
   var requestUri = _spPageContextInfo.webAbsoluteUrl + “/_api/web/getuserbyid(” + userid + “)”;
   var requestHeaders = {
      “accept”: “application/json;odata=verbose”
   };
   $.ajax({
      url: requestUri,
      async: false,
      contentType: “application/json;odata=verbose”,
      headers: requestHeaders,
      success: onSuccess,
      error: onError
  });

   function onSuccess(data, request) {
      var Logg = data.d;
      userLoginName = Logg.Title;
   }

   function onError(error) {
      alert(“error”);
   }
   return userLoginName;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ A minor nitpick: this code accesses SharePoint's web service to get the user information; it is not getting the information directly from Active Directory. It would be more accurate to say that this code gets a username from SharePoint's profile service (which may or may not sync with Active Directory). \$\endgroup\$ – Thriggle Mar 31 '15 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.