2

We are hiding the List GUI from our visitors on some landing pages in SharePoint Foundations 2013 and displaying instead REST data harvested from the applicable lists.

Since SharePoint's REST has a 100 record limit, I am testing for the __next object and recursively calling the GET until all the list has been harvested.

Here's my jQuery code:

var test = {
    root_url: "http://" + document.domain,

    init: function(){
        // Just some styling
        $('.ms-core-sideNavBox-removeLeftMargin').css('display', 'none');
        $('#contentBox').css('margin-left', '0' );

        // Event Handlers
        $('#btnGoHome').on('click', this.showHome);
        $('#corp_search').on('focus', this.getAllCorp);
    },

    getAllCorp: function(){
        $('#event_listing').html("");
        test.getListItems( test.root_url + "/humanresources/_api/web/lists/GetByTitle('Corporate Directory')/items?$select=FirstName,Title&$orderby=Title asc", '#news_listing' );
    },

    getListItems: function(pURL, loc){
        $(loc).html("");
        var listDeferred = $.ajax({
            url: pURL,
            method: "GET",
            headers: { "accept" : "application/json;odata=verbose" },
            dataType: 'json'
        });

        $.when(listDeferred).done(function(data){
            var msg;
            $('#event_listing').append(data.d.results.toString());
            msg = data.d.results.length;

            //msg.append(data.d.results);

            if(data.d.__next){
                msg += 'still got more!';
                var url = data.d.__next;
                test.getListItems( url, loc );
            } // end if

            $(loc).append(msg);
        });
        },

        showHome: function(){
        $('body').append('HOME');
        }
};

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

The "news_listing" div shows me my recursive calls are working:

100still got more!27

However, rather than the list contents, the other div returns:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object][object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

There's 127 of them so I am returning the list results. HOW do I turn that into something I can pass to a Handlebars template for proper display?

Thanks in advance!

1 Answer 1

6

You can not just turn data.d.results on a string. You must make a grab for For all results that return in data.d.results. So you will have:

for(var i = 0; i<data.d.results.length; i++){
     $('#event_listing').append(data.d.results[i].ID);
     $('#event_listing').append(data.d.results[i].Title);
}

So you return the ID and title of all objects, since you have an array of objects and each object is an item, each item has its fields like: ID, Title, Author, and so on. You must give a console.log(data.d.results[i]) to see what each object brings.

8
  • So my return object is an object(object(object(arrayofObjects)))? Commented Mar 6, 2014 at 16:56
  • array[object, object,object, object], many objects into the array. If you do FOR as I said, you will have access to an object at a time. So you can get the values ​​of each field as my example. Commented Mar 6, 2014 at 17:02
  • yes, I tested it and it works. Now I have to figure out how to make pass it into a Handlebars template Commented Mar 6, 2014 at 17:04
  • 2
    If it worked please mark as answer. For any other questions just open a new topic, if I can help I will help. Commented Mar 6, 2014 at 17:27
  • One quick question -- in the loop if I create a new object with the necessary values, can I "return" that object to getAllCorp where I can call the Handlebars template? The reason I ask this is I have 4 other similar calls on the page, so I am trying to make it modular and DRY and not rewrite the $.Ajax for each. (If you think this is a new question, I will mark you right and start another). Commented Mar 6, 2014 at 17:32

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.