I have this code (in App.js file):

var verificheList;
var ctx = SP.ClientContext.get_current();

var hostWebURL = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
var verbaleList;
var noop = function () { };
var statoVerifica;
var listTitle = 'Verbale Collaudo Fase 1 - Fine Montaggio';

function getVerificheCount(title, stato) {

    statoVerifica = stato;
    listTitle = title;
    var hostWebContext = new SP.AppContextSite(ctx, hostWebURL);
    verbaleList = hostWebContext.get_web().get_lists().getByTitle(listTitle);
    var camlQuery = new SP.CamlQuery();
        '<View><Query><Where><Eq>' +
        '<FieldRef Name=\'Verifica\'/><Value Type=\'Choice\'>' + stato + '</Value>' +
    verificheList = verbaleList.getItems(camlQuery);
    ctx.executeQueryAsync(noop, onGetverificheListFail);
    return verificheList.get_count();

But I got this error:

Uncaught Error: La raccolta non è stata inizializzata. Non è stata richiesta oppure la richiesta non è stata eseguita. Potrebbe essere necessario richiederla in modo esplicito.  
    at Function.Error.create (ScriptResource.axd?d=BVrFVl13Eo6pwgbtdwz-bzf_bf5-Fql_73RwlhGNsBAGel-cKU7OkT4jubFy4rQGKpXHed4O4zolQ_5WheE0IR6AYk3Hq_cORL76YvzjvoJyuAYJNuCcQXWu64kyr6vZRhzogddV95zmN5cAYkcc_0S0qDqgHiwdYVbjCXUVzbEmYwwFdBPhLwKtIUwMR5n60&t=ffffffffad4b7194:5)  
    at SP.ListItemCollection.$33_1 (sp.runtime.js:2)  
    at SP.ListItemCollection.get_count (sp.runtime.js:2)  
    at getVerificheCount (<anonymous>:27:26)  
    at <anonymous>:1:64 

It seem like I can't access the


property, when I use this code.

Strangely enought, instead, if I try with the Chrome:console


it shows the correct result (2).


I have tried to understand the promise function but the arguments it's a little too difficult for me.

In the Default.aspx page I use this (ugly I know) code to update a label based on the items on a Sharepoint collection:

<p><asp:Label id="Verbale1VerificheNON_OK" runat="server" Text="0" ForeColor="Red"></asp:Label> </p>
        <script type="text/javascript">
            document.getElementById('Verbale1VerificheNON_OK').innerHTML = getVerificheCount('Verbale Collaudo Fase 1 - Fine Montaggio','Verifica NON OK');

I have read jquery promise but I have only grasped the surface of issue. On jquery page the examples are related to animations applied to divs...
Instead the link you posted on SharePoint StackExchange it's quite difficult to understand the matter.

How (and where) should I apply the promise function to my code?


Instead of using noop in your executeQueryAsync you should pass a function, which will be called as soon as your async request is ready. Below code prints your count correctly:

verificheList = verbaleList.getItems(camlQuery);
}, onGetverificheListFail);  

For your convenience I also suggest to use promises with your code. For example jquery based as described in the answer here

| improve this answer | |
  • Thank you, now I have understand... But see my update, where should I use this promise function? – spiderman Oct 5 '18 at 9:12
  • I'm a bit lack of time right now.. could you please post a new question with your issue about jquery and promises, somebody else will answer or I will do that later – Sergei Sergeev Oct 5 '18 at 9:16

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.