1

I have an Announcement list in SharePoint site,I need to show all announcement in homepage,I am following msdn code sample to retrieve items from the list,But only last item will be display.

function Fetchdata() {
    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context object based upon the URL  
    var appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
    var web = appCtxSite.get_web();
    var list = web.get_lists().getByTitle("Announcement");

    var camlQuery = new SP.CamlQuery();
    //camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'Title\'/>' +
    //    '<Value Type=\'Text\'>Annoucement1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
    this.collListItem = list.getItems(camlQuery);

    ctx.load(collListItem);


    ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

}

function onQuerySucceeded(sender, args) {

    var listItemInfo = '';



        var listItemEnumerator = collListItem.getEnumerator();
        while (listItemEnumerator.moveNext()) {

            var oListItem = listItemEnumerator.get_current();

            listItemInfo = oListItem.get_item('Title');
            listItemInfo = oListItem.get_item('Body');
        }
    }

            document.getElementById('title1').innerHTML = listItemInfo.toString();
            document.getElementById('body1').innerHTML = listItemInfo.toString();







function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
0

Last item is displayed because you are overwriting the results during each iteration.

Update following code block

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {

        var oListItem = listItemEnumerator.get_current();

        listItemInfo += oListItem.get_item('Title');
        listItemInfo += oListItem.get_item('Body');
    }
    document.getElementById('title1').innerHTML = listItemInfo.toString();
    document.getElementById('body1').innerHTML = listItemInfo.toString();
}
  • Is there any javascript errors? Can you add couple of alert statements and check? – Amal Hashim Dec 23 '14 at 11:14
  • Also remove this from this.collListItem = list.getItems(camlQuery); – Amal Hashim Dec 23 '14 at 11:15
  • Please read the statement carefully. I didn't asked you to remove the complete statement. – Amal Hashim Dec 23 '14 at 11:30
  • Now it is displaying all three items twice...I am using slider.js..for showing one announcement at a time – sherin Dec 23 '14 at 12:08
  • @sherin it might be an issue with slider js control. I have never used it so cant comment. – Amal Hashim Dec 23 '14 at 12:30
0

I had to add an Array to store the item in the list

var hostWebUrl;
var appWebUrl;
// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {
    hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
    appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
    Fetchdata();
});

function manageQueryStringParameter(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];
        }
    }
}

function Fetchdata() {
    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context object based upon the URL  
    var appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
    var web = appCtxSite.get_web();
    var list = web.get_lists().getByTitle("Announcement");

    // var camlQuery = new SP.CamlQuery();
    //camlQuery.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'Title\'/>' +
    //    '<Value Type=\'Text\'>Annoucement1</Value></Geq></Where></Query><RowLimit>10</RowLimit></View>');
    collListItem = list.getItems("");

    ctx.load(collListItem);

    ctx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
    var listItemInfo1 = new Array();
    var listItemInfo2 = new Array();
    var listItemInfo3 = new Array();

    var itemCount = 0;

    var listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        listItemInfo1[itemCount] = oListItem.get_item('Title');
        //alert(listItemInfo1);
        listItemInfo2[itemCount] = oListItem.get_item('Body');
        listItemInfo3[itemCount] = oListItem.get_item('Images').get_url();
        itemCount++;
        // alert(itemCount);
    }
    for (var i in listItemInfo1) {
        var parent = $('#announcement_slider');
        //Create a div
        var p1 = document.createElement('div');
        parent.append(p1);

        var images = document.createElement('img');
        $(images).attr('src', listItemInfo3[i]);

        $(images).attr('class', 'left mrg_right_10');

        var spanTag = document.createElement('span');

        var spanTag1 = document.createElement('span');
        // alert(listItemInfo3[i]);

        //alert(listItemInfo3[i]);

        spanTag.innerHTML = listItemInfo1[i].toString();
        spanTag1.innerHTML = listItemInfo2[i].toString();

        p1.appendChild(images);
        p1.appendChild(spanTag);
        p1.appendChild(spanTag1);
    }

    $('#announcement_slider').slidesjs({
        height: 50,
        play: {
            active: true,
            auto: true,
            interval: 6000,
            swap: true
        },
        navigation: {
            active: false
        }
    });
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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