0

I need to Fetch all items from a Announcement List and show each of them by using slider.For Sliding,I am using JQuery.I am facing problem in fetching all items and displaying one by one.I have used below code.

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(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('body1').innerHTML = listItemInfo.toString();
    alert(listItemInfo);


}



  function onQueryFailed(sender, args) {

        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
Sunil Hari
  • 1,716
  • 1
  • 12
  • 20

2 Answers2

0

Hi reading announcements list items are demonstrated in MSDN (http://msdn.microsoft.com/en-us/library/office/hh185007(v=office.14).aspx). Your code mostly looks like the MSDN example snippets- which should work.

  1. Make sure you are using the correct URL of the SharePoint site
  2. Make sure your list name you spelled correctly into your JS code.

If both of them above are already correct, try to debug your code in VS or Chrome debugger.

Moim
  • 486
  • 1
  • 8
  • 23
  • I have tried that,only list will be dispalyed,Thtsy I am using above code – Sherin Mathew Dec 29 '14 at 09:03
  • I think you incorrectly used AppContextSite. If you are using this in AppWeb and trying to read the list form the host web, you can try using the SP.RequestExecutor.js. Please read the details from MSDN here : http://msdn.microsoft.com/en-us/library/office/fp179927(v=office.15).aspx – Moim Dec 29 '14 at 09:25
0

I have found the solution for the above mentioned issue

enter code here

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('' + // 'Annoucement110'); 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());

}