0

I'm building a SP2013 App.

  • I able to successfully pull back list data from the host web with with the following code that loads the "sharePointReadyToGetReviews()" function through an on-load event.
  • My users will then be able to add to that list. The code functions to add a list item and then recalls this set of functions to reload the list.
  • However I keep receiving the following error: The Collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. when this line is reloaded:

     var enumerator = allSolutionReviews.getEnumerator();
    

the code is as follows:

    var web;
    var hostweburl;
    var appweburl;
    var collListItem;

function sharePointReadyToGetReviews() {
       hostweburl =
            decodeURIComponent(
                getQueryStringParameter('SPHostUrl')
        );
       appweburl =
           decodeURIComponent(
               getQueryStringParameter('SPAppWebUrl')
        );

        var scriptbase = hostweburl + '/_layouts/15/';

        $.getScript(scriptbase + 'SP.Runtime.js',
            function () {
                $.getScript(scriptbase + 'SP.js',
                    function () { $.getScript(scriptbase + 'SP.RequestExecutor.js', returnReviews); }
                );
            }
        );
    }


    function getQueryStringParameter(param) {
        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] == param) {
                return singleParam[1];
            }
        }
    }

function returnReviews() {

$("#renderAnnouncements").empty();

    var context;
    var factory;
    var appContextSite;
    var oWebsite;

    context = new SP.ClientContext(appweburl);
    appContextSite = new SP.AppContextSite(context, hostweburl);    
    oWebsite = appContextSite.get_web();

    var list = oWebsite.get_lists().getByTitle("SolutionReviews"); 
    var camlString = 
        "<View><ViewFields>" + 
            "<FieldRef Name='Title' />" + 
            "<FieldRef Name='Created' />" + 
            "<FieldRef Name='Author' />" + 
            "<FieldRef Name='AverageRating' />" + 
        "</ViewFields></View>"; 

    var camlQuery = new SP.CamlQuery(); 
    camlQuery.set_viewXml(camlString); 
    var allSolutionReviews = list.getItems(camlQuery); 
    context.load(allSolutionReviews); 

    context.executeQueryAsync( 
        Function.createDelegate(this, getReviewSuccessHandler), 
        Function.createDelegate(this, getReviewErrorHandler) 
    ); 

function getReviewSuccessHandler(data, req) { 
    var enumerator = allSolutionReviews.getEnumerator(); 

    while (enumerator.moveNext()) { 
        var announcement = enumerator.get_current();
        var name = announcement.get_item("Author").get_lookupValue();
        var starHTML = "";
        var stars = announcement.get_item("AverageRating");
        for (var i=0;i<stars;i++)
            {
                starHTML += '<span class="glyphicon glyphicon-star"></span>';
            }

        //<img> url below must be updated dependant on the domain its in
        $(".reviews-list").append('<li class="review clearfix">' +
                                '<img src="https://heathehret-my.sharepoint.com/_layouts/15/userphoto.aspx" alt="Reviewer Avatar" />' +
                                '<div class="review-container">'+
                                    '<strong class="reviewer-name">' + name + '</strong>'+
                                    '<span class="review-date">'+announcement.get_item("Created") +'</span>'+
                                    '<div class="rating">'+
                                        starHTML +
                                    '</div>'+
                                    '<p class="review-content">'+announcement.get_item("Title")+'</p>'+
                                '</div>'+
                            '</li>'); 
    } 

} 

function getReviewErrorHandler(data, error, errorMessage) { 
    document.getElementById("renderAnnouncements").innerText = 
        "Could not complete cross-domain call: " + 
        errorMessage; 
} 
}
1

I solved my own problem. I was calling sharePointReadyToGetReviews() every time I wanted the list data. This means it was loading SP.Runtime.js, SP.js, and SP.RequestExecutor.js every time I tried to load the list even though they were already loaded. So after the first call I always call returnReviews() since the functions it need are already loaded.

|improve this answer|||||

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.