REST API and Drop Down
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.
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Hi,
Have you tried our another solution Cross-site Lookup that uses REST api to populate a drop-down control? It allows you to filter and sort the result set, request additional columns, and customize view of the result set.
Have you tried our another solution Cross-site Lookup that uses REST api to populate a drop-down control? It allows you to filter and sort the result set, request additional columns, and customize view of the result set.
- Dmitry Kozlov
- Site Admin
- Posts: 1524
- Joined: Thu Jun 07, 2012
Sure, in the following sample I pre-populate the Title field with the title of the first item from another list (Issues):
Code: Select all
$.getJSON("/_api/web/lists/GetByTitle('Issues')/items",
function(items) {
fd.field('Title').value(items.value[0].Title);
});
-
- Information
-
Who is online
Users browsing this forum: No registered users and 5 guests