0

I've been trying to show a count subtracted from a maximum of 35 for a seating count.

I've tried everything I can think of and have come to a dead end for myself. What am I doing wrong in my below code?

It only works for one call however, either the last call or the only call, it can't seem to handle multiple calls.

In the console I'm getting the following error for the items not working:

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.

*How I'm calling it on the page:

<script type="text/javascript">
      SP.SOD.executeFunc('sp.js', 'SP.ClientContext', GetAllSeatCounts);

 //   ExecuteOrDelayUntilScriptLoaded(function() { GetAllSeatCounts()
 //   }, "sp.js");

   function GetAllSeatCounts() {
        GetSeatCountDifference("SummitSeating", "SessionID", "1A", "seat-count-1a");
        GetSeatCountDifference("SummitSeating", "SessionID", "1B", "seat-count-1b");

        GetSeatCountDifference("SummitSeating", "SessionID", "2A", "seat-count-2a");
        GetSeatCountDifference("SummitSeating", "SessionID", "2B", "seat-count-2b");

        GetSeatCountDifference("SummitSeating", "SessionID", "3A", "seat-count-3a");
        GetSeatCountDifference("SummitSeating", "SessionID", "3B", "seat-count-3b");

        GetSeatCountDifference("SummitSeating", "SessionID", "4A", "seat-count-4a");
        GetSeatCountDifference("SummitSeating", "SessionID", "4B", "seat-count-4b");

        GetSeatCountDifference("SummitSeating", "SessionID", "5A", "seat-count-5a");
        GetSeatCountDifference("SummitSeating", "SessionID", "5B", "seat-count-5b");

        GetSeatCountDifference("SummitSeating", "SessionID", "6A", "seat-count-6a");
        GetSeatCountDifference("SummitSeating", "SessionID", "6B", "seat-count-6b");
    }  
</script>

Thank you for looking in advance!

**Js is located in a file in Site Assets and is being loaded in on the page in a script editor. Js below...


var CountObj = {
    allItems: null,
    name: "", 
    sItem: "", 
    dspID: ""
};

function GetSeatCountDifference(listName, columnName, searchItem, displayAtID) {
    GetContextAndListForCount(listName);
    CountObj.name = columnName;
    CountObj.sItem = searchItem;
    CountObj.dspID = displayAtID;
}

function GetContextAndListForCount(listName) {
    var cContext = SP.ClientContext.get_current();
    var tSite = cContext.get_site();
    var tWeb = tSite.get_rootWeb();

    var tList = tWeb.get_lists().getByTitle(listName);
    var tQuery = SP.CamlQuery.createAllItemsQuery();
    CountObj.allItems = tList.getItems(tQuery);

    cContext.load(CountObj.allItems);
    cContext.executeQueryAsync(onQueryCountSuccess, onQueryCountFailed);
    //cContext.executeQueryAsync(Function.createDelegate(this, this.onQueryCountSuccess), Function.createDelegate(this, this.onQueryCountFailed));
}

function onQueryCountSuccess() {
    var sContext = SP.ClientContext.get_current();
    var listEnum = CountObj.allItems.getEnumerator();
    var count = 0;

    while (listEnum.moveNext()) {
        var item = listEnum.get_current();
        var field = item.get_item(CountObj.name);

        if (field == CountObj.sItem) {
            count++;
        }
    }

    count = (35 - count);

    sContext.executeQueryAsync(ColumnCountSuccess(count), ColumnCountFailed);
    //sContext.executeQueryAsync(Function.createDelegate(this, this.ColumnCountSuccess(count)), Function.createDelegate(this, this.ColumnCountFailed));

}

function onQueryCountFailed() {
    console.log('Query List Failed');
}

function ColumnCountSuccess(count) {

    var output = " " + count + " out of 35";
    //console.log('Query List Success');
    DisplayValueAtElementID(CountObj.dspID, output);
}

function ColumnCountFailed() {
    console.log('Failed');
}

Your Answer

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

Browse other questions tagged or ask your own question.