Search code examples
javascriptasynchronoussharepointsharepoint-jsom

How to pass an argument to onQuerySucceeded function


I'm trying to query a SharePoint list using JSOM and in the onQuerySucceeded method I need to pass an argument to perform some dependent actions based on the value in the custom argument.

I have tried to pass argument as given in the code below but it doesn't worked.

function onChildQuerySucceeded(sender, args, newArgument)

    function getChild(element) {
      var clientContext = new SP.ClientContext.get_current();
      var oList = clientContext.get_web().get_lists().getByTitle('ListName');
      var camlQuery = new SP.CamlQuery();
      camlQuery.set_viewXml("<View><Query><Where><Contains><FieldRef Name='Category1' Ascending='True' /><Value Type='Choice'>" + element + "</Value></Contains></Where></Query></View>");
      this.collListItem = oList.getItems(camlQuery);
      clientContext.load(collListItem);
      clientContext.executeQueryAsync(
        Function.createDelegate(this, onChildQuerySucceeded(element)),
        Function.createDelegate(this, onChildQueryFailed)
      );
    }

    function onChildQuerySucceeded(sender, args) {
      alert(element);
      var listItemInfo = "";
      var listItemEnumerator = collListItem.getEnumerator();
      while (listItemEnumerator.moveNext()) {
        oListItem = listItemEnumerator.get_current();
        console.log(oListItem.get_item('Title') + "Category: " + 
    oListItem.get_item('Category1'));
      }
    }

Solution

  • Instead passing existing function

    Function.createDelegate(this, onChildQuerySucceeded)
    

    Try inline function

    var element = 'my object';
    Function.createDelegate(this, function onChildQuerySucceeded(sender, args) { alert(element);})