0

I am trying to run a CAML query to sort through some data. Once the page finishes loading i do not get the results i want. I checked the functions and it turns out that for some reason, my CAML query returns undefined. It only does this after it is passed in the set_viewXml() function. Code provided below:

var runQuery = function (dateObj, query, success, failure, context) {

   var camlQuery = new SP.CamlQuery();

   alert(query); // shows the CAML query
   alert(camlQuery.set_viewXml(query(dateObj))); // shows me undefined

   camlQuery.set_viewXml(query(dateObj));

   items = list.getItems(camlQuery);

   context.load(items);

   context.executeQueryAsync(success, failure);
};

Here is the CAML:

"<View>" + 
   "<Query>" +
      "<Where>" + 
         "<And>" +
            "<Eq>" +
               "<FieldRef Name='EventStartDate' />" +
               "<Value Type='DateTime' IncludeTimeValue='FALSE'>" +
                  dateObj.toISOString() +
               "</Value>" +
            "</Eq>" +
            "<Eq><FieldRef Name=\"QueryID\" /><Value Type=\"Text\">2216</Value></Eq>" +
         "</And>" +
      "</Where>" +
      "<GroupBy><FieldRef Name='EventName'></FieldRef></GroupBy>"+
      "<OrderBy><FieldRef Name='EventStartDate' /><FieldRef Name='EventEndDate' /><FieldRef Name='EventName' /></OrderBy>" +
   "</Query>" +
"</View>";

I am pretty sure there is something wrong with the CAML query because i do not work with it on a day to day basis.

I have this code running fine in another app and when I alert(alert(camlQuery.set_viewXml(query(dateObj)));) I am able to see the full CAML query build (it does not return undefined). Not sure if that helps any.

2
  • Where you define this variable in your code dateObj? Commented Oct 7, 2016 at 20:11
  • It is just some other functions above where I call the runQuery function. I am able to see the date if i alert it.
    – IE5Master
    Commented Oct 7, 2016 at 20:13

2 Answers 2

0

I think your issue at this function query(dateObj) within alert(camlQuery.set_viewXml(query(dateObj)));

This function may be undefined or defined as var query = function (dateObj) {}. in this case, it will be undefined if you tried to call it like this query (dateObj) so try to define it as function query(dateObj) {}

Also, make sure that it's not defined at the same scope of query variable because it has the same name.

2
  • M.Qassas, I have tried your suggestion but when i change the function to function query(dateObj) I receive an error saying that the query function is not a function or is not defined. When i change it back i do not receive and error
    – IE5Master
    Commented Oct 11, 2016 at 14:34
  • should you share your final code , or if you can to change its name and try again , then tell me result Commented Oct 11, 2016 at 14:40
0

It looks as though your 'query' parameter is a variable and not a function, so query(dateObj) will not work.

create a function outside of var runQuery = function:

    var query = function(dateObj){
      if(dateObj){
        return "<View>" + 
       "<Query>" +
          "<Where>" + 
             "<And>" +
                "<Eq>" +
                   "<FieldRef Name='EventStartDate' />" +
                   "<Value Type='DateTime' IncludeTimeValue='FALSE'>" +
                      dateObj.toISOString() +
                   "</Value>" +
                "</Eq>" +
                "<Eq><FieldRef Name=\"QueryID\" /><Value Type=\"Text\">2216</Value></Eq>" +
             "</And>" +
          "</Where>" +
          "<GroupBy><FieldRef Name='EventName'></FieldRef></GroupBy>"+
          "<OrderBy><FieldRef Name='EventStartDate' /><FieldRef Name='EventEndDate' /><FieldRef Name='EventName' /></OrderBy>" +
       "</Query>" +
    "</View>";
  }
  else {
    // return something else
  }
}

then when you, remove query as a parameter to your runQuery function, and then call query(dateObj); from inside runQuery. It's important that your query function return a string that's CAML in order to set it as the viewXML properly.

You can create a more comprehensive query function to handle the proper CAML query to make it more dynamic, but for your original question, the function above should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.