1

I am trying to get list items in select tag but it is not working.I have tried to fix this in many different ways but nothing works. my code:

<select id ="listBox" multiple="multiple" style="width:200px; height:400px;">
    <option></option>
</select>

var context = new SP.ClientContext.get_current();
var hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var parrentContext = new SP.AppContextSite(clientContext, hostWebUrl);
var parentWeb = parrentContext.get_web();
var list = parentWeb.get_lists().getByTitle("myList");
var listItems = list.getItems("");

$(document).ready(function () {
clientContext.executeQueryAsync(retrieveListItems);

});

function retrieveListItems() {

clientContext.load(listItems);
clientContext.executeQueryAsync(onSuccess2, onFail)

}

function onSuccess2() {
var onGoing = document.getElementById('listBox');

onGoing.options.length = 0;

var listEnumerator = listItems.getEnumerator();

while (listEnumerator.moveNext()) {

    var currentItem = listEnumerator.get_current();
    var listBoxItems = currentItem.get_item("Title");
    onGoing.options[onGoing.options.length] = new Option(listBoxItems, currentItem.get_item("Title"));
}

function onFail(sender, args) {
    alert(args.get_message());
}

}

function getQueryStringParameter(paramToRetrieve) {
var params =
document.URL.split("?")[1].split("&");
var strParams = "";
for (var i = 0; i < params.length; i = i + 1) {
    var singleParam = params[i].split("=");
    if (singleParam[0] == paramToRetrieve)
        return singleParam[1];
}

}

1
  • I've edited my answer so it should match your code perfectly, try this and see if it works :)
    – Dr Cox
    Sep 17, 2015 at 12:45

1 Answer 1

2

I've edited the complete code, to match your code.

And don't forget to set permissions in the Appmanifest.xml.

This should work:

App.js:

'use strict'

var context = SP.ClientContext.get_current();
var listItems;
var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var parentContext = new SP.AppContextSite(clientContext, hostweburl);
var parentWeb = parentContext.get_web();
var list = parentWeb.get_lists().getByTitle("myList");
var listItems1;
var currentItem;

$(document).ready(function () {
});

function retrieveListItems() {
    listItems = list.getItems("");

    context.load(listItems, 'Include(ID, Title)');

    context.executeQueryAsync(onSuccess2, onFail)
}
function onSuccess2() {
    var onGoing = document.getElementById('listbox');

    onGoing.options.length = 0;

    var listEnumerator = listItems.getEnumerator();

    while (listEnumerator.moveNext()) {

        var currentItem = listEnumerator.get_current();
        var dropItems = currentItem.get_item("Title");
        onGoing.options[onGoing.options.length] = new Option(dropItems, currentItem.get_item("ID"));
    }
}
function doSomething() {
    var id = $('#listbox').find('option:selected').val();

    currentItem = list.getItemById(id);

    context.load(currentItem);

    context.executeQueryAsync(onSuccessdoSomething, onFail)
}
function onSuccessdoSomething() {

    $("#DivDescription").html(currentItem.get_item('Title'));
}

function onFail() {
    alert('Failed');
}
function getQueryStringParameter(urlParameterKey) {
    var params = document.URL.split('?')[1].split('&');
    var strParams = '';
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split('=');
        if (singleParam[0] == urlParameterKey)
            return decodeURIComponent(singleParam[1]);
    }
}

In PlaceHolderMain in Default.aspx

<td class="td3">
    <div id="DivOnGoing">
        <select id="listbox" size="10" name="decision" style="width: 50%" onchange="if (this.selectedIndex >= 0) doSomething();">
        </select>
    </div>
</td>
<input id="Button1" type="button" value="button" onclick="retrieveListItems()" />
<div id="DivDescription"></div>

6
  • Ok I'll try. Thx!
    – abc
    Sep 16, 2015 at 11:54
  • I tried but it is not working, it is probably because of I haven't done it right. I'll update my code and take a look please if you have time.
    – abc
    Sep 17, 2015 at 7:59
  • Appmanifest.xml permission is on full control. I copied and pasted but it is not getting list items from my SP list.
    – abc
    Sep 17, 2015 at 13:34
  • @ Somethingcool123 I did but nothing happens. Did this code work for you?
    – abc
    Sep 18, 2015 at 7:29
  • @abc Glad to hear! and a tip for future SharePoint app development, is to use Internet Explorer to run your app, because Internet Explorer shows you errors a lot more than Chrome and Firefox does, it helps beginners a lot.
    – Dr Cox
    Sep 18, 2015 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.