Pass parameter to CAMLQuery - GetListItems

Dec 29, 2010 at 10:21 PM

I have a function that calls the SPServices GetListItems, and passes a hard coded parameter, and returns the expected value. I am trying to make this a bit more dynamic, and pass the parameter in via a textbox, and button click event.  I believe its a syntax issue, but I'm having trouble spotting it.

 

this function works as expected:

 

<script language="javascript" type="text/javascript">

$(document).ready(function() {
  
  //var empID = $("#txtEmployeeID").val();
  var empID = "99999";
  
  //alert("Employee Query is Firing!");

  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Personnel",
    CAMLViewFields: "<ViewFields>" + 
    				"<FieldRef Name='BadgeID' />" +
    				"<FieldRef Name='FullName' />" +
    				"</ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='BadgeID'/><Value Type='Text'>" + empID + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName=z:row]").each(function() {
        var employeeID = $(this).attr("ows_BadgeID");
        alert(employeeID);
        //var liHtml = "<li>" + $(this).attr("ows_BadgeID") + "</li>";
        //$("#tasksUL").append(liHtml);
      });
    }
  });
});
</script>

 

This function doesn't fire off as expected:

 

<script language="javascript" type="text/javascript">

function EmployeeQuery(queryID) {
  
  //var empID = $("#txtEmployeeID").val();
  
  alert("Employee Query is Firing!");

  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Personnel",
    CAMLViewFields: "<ViewFields>" + 
    				"<FieldRef Name='BadgeID' />" +
    				"<FieldRef Name='FullName' />" +
    				"</ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='BadgeID'/><Value Type='Text'>" + queryID + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName=z:row]").each(function() {
        var employeeID = $(this).attr("ows_BadgeID");
        alert(employeeID);
        //var liHtml = "<li>" + $(this).attr("ows_BadgeID") + "</li>";
        //$("#tasksUL").append(liHtml);
     }); 
    }
  });
});

$(document).ready(function(){
$("#queryButton").click(function(){
alert("clicked");
var queryID = $("#txtEmployeeID").val();
EmployeeQuery(queryID);
alert("done");
});

});
</script>


<input id="queryButton" type="button"  style="vertical-align:middle;font-size:32px;font-family:verdana width: 240px; height: 90px;" name="Query" value="Query" />
Cheers,
Jason
Dec 30, 2010 at 4:03 AM

Hi Jason.

The syntax seems fine to me. Unfortunately you haven't included the markup for what you attempt to retrieve with $("#txtEmployeeID"). Actually, IIWY, I'd first check the ID of the html element and replace alert("clicked"); with alert(queryID); right after that val() invocation.

Hope this helps,

Alexey Krasheninnikov

Dec 30, 2010 at 7:53 PM

Hi Alexey,

        thank you for the help. After staring at the code for a long time, I decided to start over, and see what would happen. I simplified my code, and in the process got it to work. I am now able to pass a variable from the textbox into my CAMLQuery, and get back a value from the GetListItems operation. I'm planning on expanding on this in the future, but I'm excited to get this part working.

 

<script language="javascript" type="text/javascript">

function EmployeeQuery() 
{
  
 var empID = $("#txtPersonnelID").val();
  
 
  
  alert("Employee Query is Firing!");
 
  
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Personnel",
    CAMLViewFields: "<ViewFields>" + 
    				"<FieldRef Name='EmployeeID' />" +
    				"<FieldRef Name='Location' />" +
    				"</ViewFields>",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='EmployeeID'/><Value Type='Text'>" + empID + "</Value></Eq></Where></Query>",
    completefunc: function (xData, Status) {
      $(xData.responseXML).find("[nodeName=z:row]").each(function() {
        var employeeID = $(this).attr("ows_Location");
        alert(employeeID);
        
      });
    }
  });
}


</script>


<button type="button" style="vertical-align:middle;font-size:32px;font-family:verdana; width: 240px; height: 90px;" id="btnQuery" onclick="javascript:EmployeeQuery();">
		Query ID</button>

 

 

Jason