Getting Lookup Type field in REST API SharePoint 2013

In my previous post, I wrote about – “Getting Person or group type field in REST API” –  . In this post, I will be explaining about using Lookup field in REST API.

Analyze the below code:-

var ddEventType;
ddEventType = $(‘select[title=”Events Type Required Field”]’).children(“option”).filter(“:selected”).text();
$(‘select[title=”Events Type Required Field”]’).on(‘change’, function(e) {
ddEventType = $(‘select[title=”Events Type Required Field”]’).children(“option”).filter(“:selected”).text();
});//End of Doc ready
function GetddlValues(ddlValue){
SP.SOD.executeFunc(“sp.js”, “SP.ClientContext”, function() {
SP.SOD.executeFunc(“sp.runtime.js”, “SP.ClientContext”, function() {
var queryUrl = “<server_url>” + “/_api/web/lists/getbytitle(‘Event Category’)/items?$expand=Events_x0020_Type&$select=Id,Events_x0020_Type/Title,Events_x0020_Type/Id,Title
&$orderby=Title asc&$filter=Events_x0020_Type/Title eq ‘” + ddlValue + “‘”;
url: queryUrl,
method: “GET”,
headers: {
“Accept”: “application/json; odata=verbose”
success: onQuerySuccess,
error: onQueryError
function onQuerySuccess(data) {
$(‘select[title=”Events Category Required Field”]’).empty();
var userEntry = [];
var results = data.d.results;
$.each(results, function(index,dataRec) {
});//End of push
//alert(“In each userEntry”+ JSON.stringify(userEntry));
});//End of each
for(var j=0;j<userEntry.length;j++){
$(‘select[title=”Events Category Required Field”]’).append($(”, { value: userEntry[j].id,text : userEntry[j].name}));
}//End of for
}//End of onQuerySuccess
function onQueryError(error) {
}//End of onQueryError

In the above code, Event Type is a Lookup column. The most important point to consider here is how to save/append options in the drop-down.

Hope this helps. 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s