SharePoint 2013/Office 365 : Retrieve lookup column value REST-Javascript.

With SharePoint 2013 and Office 365, REST api is being extensively used for working with SharePoint data. Working in SharePoint with REST is all about generating HTTP requests with the correct URLs to get the required data. In this article, we will walk through the code to get the Lookup column value from a List using REST-JavaScript.

I have created two lists, Countries and Cities.  The countries in the cities list is a lookup column.

     

To fetch all the list items using REST, we use the following request URL  : /_api/web/lists/getByTitle('Cities')/items and set the response type as JSON
Let us try to make a JQuery ajax request using this URL to see the response. 

function GetCities() {     $.ajax({             url : _spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getByTitle('Cities')/items",             method : "GET",             headers : {"accept" "application/json;odata=verbose"},             success : function(d){                 if(d.d.results.length > 0)                 {                     alert(d.d.results[0]["Country"].Title);                 }             },             error : function(data, errorCode, errorMessage){                 alert(errorMessage);             }           }); }

When the GetCities function is triggered, we will get all the list items from Cities list in response. Here is how the JSON response looks like using the IE developer toolbar.

The image shows the first item from the results received from the server. The title of the list item is fetched which shows us the city name "Mumbai" however, the country is not returned. Only the ID of the lookup in the field "CountryId" is returned. This is because we have to specify certain parameters in the REST request URL to fetch the value of the Lookup column.

And hence the below statement  that we used in our code will not give us any value. 

d.d.results[0][``"Country"``].Title

Using the below URL in our Ajax request, we shall get the Country value in the response.

url : _spPageContextInfo.webAbsoluteUrl + ``"/_api/web/lists/getByTitle('Cities')/items?$select=Country/Title&$expand=Country/Title"``,

And the response is 


Notice the $select and the $expand parameters. With the country column we have specified the "/Title" which will get us the value of the lookup column.