REST API and Drop Down
Posted: 10 Dec 2014
Hi there,
I'm still new to all this.
Been trying to get a successful result from the REST API (part 1) then use that knowledge in combination with other code (part 2) to piece together how to populate a drop down via the results of an API call in spform or just in general.
Part 1:
Have a div with the id message.
function doGet()
{
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/lists('<guid is here>')/items?$select=*",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data,textStatus, jqXHR) {
var message = jQuery("#message");
message.text("X");
jQuery.each(data.d.results, function (index, item) {
message.append("<br/>");
message.append(item.Id);
});
});
call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving: " + errorThrown);
});
}
As for populating the drop down with an array here's the code I was able to get working in part 2.
Part 2:
There's an HTML box select element called selectMonth inside spForms.
var items = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var list = document.getElementById("selectMonth");
for(var i in items) {
list.add(new Option(items, items));
}
If anyone has any advice on getting the REST API to work in part 1 I think I can stitch things together to populate the drop down list. So far I've been frustrated.
Thanks to anyone who can help.
I'm still new to all this.
Been trying to get a successful result from the REST API (part 1) then use that knowledge in combination with other code (part 2) to piece together how to populate a drop down via the results of an API call in spform or just in general.
Part 1:
Have a div with the id message.
function doGet()
{
var call = jQuery.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/lists('<guid is here>')/items?$select=*",
type: "GET",
dataType: "json",
headers: {
Accept: "application/json;odata=verbose"
}
});
call.done(function (data,textStatus, jqXHR) {
var message = jQuery("#message");
message.text("X");
jQuery.each(data.d.results, function (index, item) {
message.append("<br/>");
message.append(item.Id);
});
});
call.fail(function (jqXHR,textStatus,errorThrown){
alert("Error retrieving: " + errorThrown);
});
}
As for populating the drop down with an array here's the code I was able to get working in part 2.
Part 2:
There's an HTML box select element called selectMonth inside spForms.
var items = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var list = document.getElementById("selectMonth");
for(var i in items) {
list.add(new Option(items, items));
}
If anyone has any advice on getting the REST API to work in part 1 I think I can stitch things together to populate the drop down list. So far I've been frustrated.
Thanks to anyone who can help.