0

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'));
      }
    }
Cedan Misquith
  • 1,134
  • 9
  • 20
Joseph Jojo John
  • 500
  • 1
  • 7
  • 19

2 Answers2

1

Instead passing existing function

Function.createDelegate(this, onChildQuerySucceeded)

Try inline function

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

I believe you need to adjust a couple things.

You'll need to add the "this" keyword and supply the arguments that the function expects.

You are technically overriding the original callback of "onQuerySucceeded".

Something like this should work for you.

 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, this.onChildQuerySucceeded(element)),
    Function.createDelegate(this, this.onChildQueryFailed)
  );
}

function onChildQuerySucceeded(element) {
  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'));
  }
}

If that doesn't work set the "element" variable to a global variable the success function can access and don't bother passing it.

  • by using the above mentioned code I can pass the element variable. But it breaks the remaining code. It cannot able to process the item collection and getting the below error. Error: sp.runtime.debug.js?rev=VFkBZgkYpbAZS6abSrBqIg%3D%3D:1894 Uncaught 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. at getChild (byCategoryWebpart.js:40) at HTMLAnchorElement.onclick (ByCategoryTest.aspx:1) – Joseph Jojo John Aug 19 '19 at 13:15
  • Can you post the entire script? – Ryan McGuirk Aug 19 '19 at 17:42
  • I tried calling a function with element as an argument inside the onsucceed function, which got worked for me. – Joseph Jojo John Aug 29 '19 at 09:35
  • I tried as you mentioned. usage of "this"keyword solved the issues. so there is no case of passing value as an argument. Thank you so much! – Joseph Jojo John Oct 17 '19 at 21:20