cbridgesaz
asked on
CAML query to get items from SharePoint 2013 list
I am attempting to get certain items from a list in SharePoint 2013 by using CAML query, once set to variables these item are to be called to a custom form to populate fields. I have set an alert in the SPServices Call and even it is not working, any direction would be helpful.
function GetUsers()
{
//The Web Service method we are calling, to read list items we use 'GetListItems'
var method = "GetListItems";
//The display name of the list we are reading data from
var list = "EmployeeData";
var fieldsToRead = "<ViewFields>" +
"<FieldRef Name='EmployeeID' />" +
"<FieldRef Name='FirstName' />" +
"<FieldRef Name='LastName' />" +
"</ViewFields>";
var query = "<Query>" +
"<Where>" +
"<Neq>" +
"<FieldRef Name='EmployeeID'/><Value Type='Number'>0</Value>" +
"</Neq>" +
"</Where>" +
"<OrderBy>" +
"<FieldRef Name='EmployeeID'/>" +
"</OrderBy>" +
"</Query>";
alert(query);
//Here is our SPServices Call where we pass in the variables that we set above
$().SPServices({
operation: method,
async: false, //if you set this to true, you may get faster performance, but your order may not be accurate.
listName: list,
CAMLViewFields: fieldsToRead,
CAMLQuery: query,
//this basically means "do the following code when the call is complete"
completefunc: function (xData, Status) {
//this code iterates through every row of data returned from the web service call
$(xData.responseXML).SPFil terNode("z :row").eac h(function () {
//here is where we are reading the field values and putting them in JavaScript variables
//get the title field (ID)
var empNumber = ($(this).attr("ows_Employe eID"));
//get eployee first name
var empFname = ($(this).attr("ows_FirstNa me"));
//get emplyee last name
var empLname = ($(this).attr("ows_LastNam e"));
alert(empNumber);
});
}
});
}
function GetUsers()
{
//The Web Service method we are calling, to read list items we use 'GetListItems'
var method = "GetListItems";
//The display name of the list we are reading data from
var list = "EmployeeData";
var fieldsToRead = "<ViewFields>" +
"<FieldRef Name='EmployeeID' />" +
"<FieldRef Name='FirstName' />" +
"<FieldRef Name='LastName' />" +
"</ViewFields>";
var query = "<Query>" +
"<Where>" +
"<Neq>" +
"<FieldRef Name='EmployeeID'/><Value Type='Number'>0</Value>" +
"</Neq>" +
"</Where>" +
"<OrderBy>" +
"<FieldRef Name='EmployeeID'/>" +
"</OrderBy>" +
"</Query>";
alert(query);
//Here is our SPServices Call where we pass in the variables that we set above
$().SPServices({
operation: method,
async: false, //if you set this to true, you may get faster performance, but your order may not be accurate.
listName: list,
CAMLViewFields: fieldsToRead,
CAMLQuery: query,
//this basically means "do the following code when the call is complete"
completefunc: function (xData, Status) {
//this code iterates through every row of data returned from the web service call
$(xData.responseXML).SPFil
//here is where we are reading the field values and putting them in JavaScript variables
//get the title field (ID)
var empNumber = ($(this).attr("ows_Employe
//get eployee first name
var empFname = ($(this).attr("ows_FirstNa
//get emplyee last name
var empLname = ($(this).attr("ows_LastNam
alert(empNumber);
});
}
});
}
What error do you get?
ASKER
There is no error reported, and the alert does not show. Everything looks right to me but nothing happens.
Are the spservices and jquery libraries uploaded?
ASKER
Yes they are.
Your alert is not firing because it's not recognizing the libraries. Make sure the path to the files are correct.
Also, when you call the function is the file name correct?
Can you show a little bit more of your code?
Also, when you call the function is the file name correct?
Can you show a little bit more of your code?
ASKER
The libraries are : <link type="text/css" rel="stylesheet" href="//ajax.googleapis.co m/ajax/lib s/jqueryui /1.10.0/th emes/start /jquery-ui .css" />
<!-- Reference jQuery on the Google CDN -->
<script type="text/javascript" src="//ajax.googleapis.com /ajax/libs /jquery/1. 8.3/jquery .min.js">< /script>
<!-- Reference jQueryUI on the Google CDN -->
<script type="text/javascript" src="//ajax.googleapis.com /ajax/libs /jqueryui/ 1.10.0/jqu ery-ui.min .js"></scr ipt>
<!-- Reference SPServices on cdnjs (Cloudflare) -->
<script type="text/javascript" src="//cdnjs.cloudflare.co m/ajax/lib s/jquery.S PServices/ 0.7.2/jque ry.SPServi ces-0.7.2. min.js"></ script>
and the function is called by $(document).ready(function (){
GetUsers();
});
Also, if I change "<FieldRef Name='EmployeeID/'>" to "<FieldRef Name='ID/'>" I get an alert that reads undefined
<!-- Reference jQuery on the Google CDN -->
<script type="text/javascript" src="//ajax.googleapis.com
<!-- Reference jQueryUI on the Google CDN -->
<script type="text/javascript" src="//ajax.googleapis.com
<!-- Reference SPServices on cdnjs (Cloudflare) -->
<script type="text/javascript" src="//cdnjs.cloudflare.co
and the function is called by $(document).ready(function
GetUsers();
});
Also, if I change "<FieldRef Name='EmployeeID/'>" to "<FieldRef Name='ID/'>" I get an alert that reads undefined
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ahhhh......good catch! I'm glad you found the solution!
ASKER
I started by trying to read another list that was manually created in SharePoint designer and the code worked, I then noticed that only difference other than one being exported from excel was the manually created list had single word columns and no spaces.