I have a problem with my global variable. I saw that there is fundamental diffenrce the C language's Family.

I would like to reach and modify my variable "number" everywhere. I try with arguments or Returns but as I am not very experienced in JavaScript I failed.

Here is my code

var number;
    var message;

    $(function () {
        ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");
        ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");

     function runCode() {
        // Specify a client context using the server-relative URL of the sub web, load the sub web, and call the ExecuteQueryAsync method.
        var clientContext = new SP.ClientContext.get_current();
        targetWeb = clientContext.get_web();
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded1), Function.createDelegate(this, this.onQueryFailed1));

    function onQuerySucceeded1() {
        // On success, use get_ property assessor methods to return the value of the Title, ID, Language, uiVersion, Description, and Created properties of the SP.Web object.
        number = targetWeb.get_title();
        message = "<strong>Meldung: </strong>" + targetWeb.get_title();

    function onQueryFailed1(sender, args) {
        alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());

    function retrieveListItems() {
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Auftragsordner');
        var camlQuery = new SP.CamlQuery();

        camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'Title\'/>' + 
        '<Value Type=\'Text\'>'+number+'</Value></Eq></Where></Query></View>');
        this.collListItem = oList.getItems(camlQuery);

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
  • Is number undefined in retrieveListItems() method. If yes, call retrieveListItems() inside onQuerySucceeded1() method. and call only ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js"); initially. – Nadeem Yousuf-AIS May 29 '18 at 8:28
  • Thank you, I didn't think to this solution and it works perfectly ! – fullcarton May 29 '18 at 10:41
  • adding it as answer. – Nadeem Yousuf-AIS May 29 '18 at 10:42

The number varibale is undefined in retrieveListItems() as onQuerySucceeded1 method hasn't finished execution yet.

Call retrieveListItems() inside onQuerySucceeded1() method. And remove ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js"); from

$(function () {
        ExecuteOrDelayUntilScriptLoaded(runCode, "sp.js");
        ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");

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.