Link to home
Start Free TrialLog in
Avatar of cbridgesaz
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).SPFilterNode("z:row").each(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_EmployeeID"));
                            //get eployee first name
                            var empFname = ($(this).attr("ows_FirstName"));
                            //get emplyee last name
                            var empLname = ($(this).attr("ows_LastName"));
                         
                      alert(empNumber);                            
                        });                
                    }
        });

}
Avatar of Isaac
Isaac
Flag of United States of America image

What error do you get?
Avatar of cbridgesaz
cbridgesaz

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?
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?
The libraries are : <link  type="text/css" rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.0/themes/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/jquery-ui.min.js"></script>
<!-- Reference SPServices on cdnjs (Cloudflare) -->
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.2/jquery.SPServices-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
ASKER CERTIFIED SOLUTION
Avatar of cbridgesaz
cbridgesaz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahhhh......good catch!  I'm glad you found the solution!
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.