REST API and Drop Down

rss

Posted by Dan_C - Dec 10 ’14 at 06:07

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[i], items[i]));

}

 

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
    Dmitry Kozlov
    Admin
    Member for: 6 years 10 months 25 days
    #1 by Dmitry Kozlov Dec 11 ’14 at 06:11

    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.

  • Dan_C
    Member
    Member for: 5 years 8 months 20 days
    #2 by Dan_C Dec 11 ’14 at 10:45

    [#1]: I saw that, but I know later on I will need to get into some more complex queries once I get up to speed so I'm trying to learn.  I might end up trying out your product but for me the task of learning to do it without still remains.

  • Dan_C
    Member
    Member for: 5 years 8 months 20 days
    #3 by Dan_C Dec 11 ’14 at 12:07

    [#1]: Also I need to do more with the data than just the drop down.  I need to use a list on the site collection main site for some validation.

    Perhaps you could hint at how I could populate a text field with a single value returned from a REST API call?

  • Dmitry Kozlov
    Dmitry Kozlov
    Admin
    Member for: 6 years 10 months 25 days
    #4 by Dmitry Kozlov Dec 12 ’14 at 04:49

    [#3]: Sure, in the following sample I pre-populate the Title field with the title of the first item from another list (Issues):

    $.getJSON("/_api/web/lists/GetByTitle('Issues')/items", 
        function(items) {
    	fd.field('Title').value(items.value[0].Title);
        });
    
  • Dan_C
    Member
    Member for: 5 years 8 months 20 days
    #5 by Dan_C Dec 12 ’14 at 01:32

    [#4]: Thanks Dmitry.

    Looks like this info may help me take a step or two forward.

Displaying 1 to 5 of 5 messages