Click here to Skip to main content
15,893,266 members
Articles / API
Technical Blog

How to Get Login Name and Display Name using SharePoint 2013 REST API

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
4 Dec 2013CPOL 42.1K   1   3
How to get login name and display name using SharePoint 2013 REST API.

Working with REST API is quite simple and straightforward, for example when you need to fetch data from a list you can use the following jQuery AJAX code snippet:

JavaScript
jQuery.ajax({
   url: "http://YourSite/_api/web/lists/getbytitle('ListName')/items",
   type: "GET",
   headers: { "Accept": "application/json;odata=verbose" },
   success: function(data, textStatus, xhr) {
    var dataResults = data.d.results;
    alert(dataResults[0].Title);     
    },
   error: function(xhr, textStatus, errorThrown) {
   alert("error:"+JSON.stringify(xhr));
   }
});

Another good example is when you need to get specific fields like “Title”, “ID” or  “Modified” , you can use the “$select keyword.

For example:

C#
url: "http://YourSite/_api/web/lists/getbytitle('ListName')/items$select= Title ,ID, Modified",
            type: "GET",
            headers: { "Accept": "application/json;odata=verbose" },
            success: function(data, textStatus, xhr) {
             var dataResults = data.d.results;
             alert(dataResults[0].Modified);     
                },
            error: function(xhr, textStatus, errorThrown) {
            alert("error:"+JSON.stringify(xhr));
            }
         });

But what happens when you need to get a user/group field like “Author” ? well, things are not as obvious as they seem.

Unfortunately you can’t use /getbytitle(‘ListName’)/items or /getbytitle(‘ListName’)/items?filter=Author to get the user field since this field does not exist in the response data, but luckily for us we have an “AuthorId” field that (as you already  guessed) will get us the user id.

image

So after getting the user id from your list you need to make another Ajax call to get the user login name/display name by using /_api/Web/GetUserById method .

Example:

JavaScript
function getUser(id){
var returnValue;
  jQuery.ajax({
   url: "http://YourSite/_api/Web/GetUserById(" + id + ")",
   type: "GET",
   headers: { "Accept": "application/json;odata=verbose" },
   success: function(data) {
           var dataResults = data.d;
      //get login name  
      var loginName  = dataResults.LoginName.split('|')[1];
      alert(loginName);     
      //get display name
      alert(dataResults.Title);
   }
 });
}

image

Full Example:

JavaScript
jQuery.ajax({
    url: "/SiteName/_api/web/lists/getbytitle('ListName')/items",
    type: "GET",
    headers: { "Accept": "application/json;odata=verbose" },
    success: function(data, textStatus, xhr) {
        var dataResults = data.d.results;
        var resultId = dataResults[0].AuthorId.results[0];        
        getUser(resultId)
    },
    error: function(xhr, textStatus, errorThrown) {
        alert("error:"+JSON.stringify(xhr));
    }
});

function getUser(id){
var returnValue;
  jQuery.ajax({
   url: "http://YourSite/_api/Web/GetUserById(" + id + ")",
   type: "GET",
   headers: { "Accept": "application/json;odata=verbose" },
   success: function(data) {
           var dataResults = data.d;
      //get login name  
      var loginName  = dataResults.LoginName.split('|')[1];
      alert(loginName);     
      //get display name
      alert(dataResults.Title);
   }
 });
}

I would like to thank Ofir Elmishali for helping me with this post.

Hope you’ll find this post helpful.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) E4D
Israel Israel
Alex Choroshin is working as a Consultant/Web Developer at LogoUi company , he has a vast experience developing client side solutions and single page application using the latest web technologies: HTML5, CSS3 , AngularJS, JQuery, SignalR, ASP.NET MVC4, Web API, NodeJS etc.

Also experience with the SharePoint 2010 & SharePoint 2013 platform encompassing all the aspects of SharePoint architecture and development.

Comments and Discussions

 
QuestionEven when $expand won't work, there's another way... Pin
acrush20-Aug-15 3:13
acrush20-Aug-15 3:13 
QuestionYou may use REST $expand Pin
Sohel_Rana16-Jan-14 18:03
Sohel_Rana16-Jan-14 18:03 
AnswerRe: You may use REST $expand Pin
acrush21-Aug-15 0:14
acrush21-Aug-15 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.