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” – https://sharepointeducation.wordpress.com/2016/07/19/getting-person-or-group-type-field-in-rest-api-sharepoint-2013/  . In this post, I will be explaining about using Lookup field in REST API.

Analyze the below code:-


var ddEventType;
$(document).ready(function(){
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();
GetddlValues(ddEventType);
});
GetddlValues(ddEventType);
});//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 + “‘”;
//alert(queryUrl);
$.ajax({
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;
//alert(JSON.stringify(results));
$.each(results, function(index,dataRec) {
userEntry.push({
id:dataRec.Id,
name:dataRec.Title
});//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) {
alert(error.statusText);
}//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. 🙂

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s