GetListItems Syntax Check

Jul 31, 2014 at 3:12 AM
I believe I have the syntax correct, but I'm getting an error in the developer tools console that I'm having trouble resolving. The error is "Unterminated string constant"

I have a CEWP for loading the scripts, and it's pointing to a text file
<!-- Reference jQuery on the Google CDN //ajax.googleapis.com/ -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<!-- Reference my local copy of the SPServices -->
<script type="text/javascript" src="http://portal/sites/angularjs/Style%20Library/dg/jquery.SPServices-2014.01.js"></script>
My code is located in another text file that is referenced by a second CEWP
<script type="text/javascript">
      $().SPServices({
        operation: "GetListItems",
        async: false,
        listName: "Customers,
        CAMLQuery: "<Query></Query>",
        CAMLViewFields: "<ViewFields>  <FieldRef Name='Title' /> </ViewFields>",
        completefunc: function (xData, Status) {
            var myJson = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({
               mapping: {
                 ows_ID: {mappedName: "ID", objectType: "Counter"},
                 ows_Title: {mappedName: "Title", objectType: "Text"},
                 ows_Created: {mappedName: "Created", objectType: "DateTime"},
                 ows_Author: {mappedName: "Author", objectType: "User"},
                 ows_Country: {mappedName: "Country", objectType: "MultiChoice"},
                 ows_City: {mappedName: "City", objectType: "MultiChoice"},
                 ows_Lead_x0020_Source: {mappedName: "LeadSource", objectType: "MultiChoice"},
                 ows_Sales_x0020_Rep: {mappedName: "SalesRep", objectType: "UserMulti"},
                 ows_Potential_x0020_Value: {mappedName: "PotentialValue", objectType: "Currency"}
                },   // name, mappedName, objectType
               includeAllAttrs: true
              });
        }
       alert(myJson);
    });
</script>
Jul 31, 2014 at 5:00 AM
Missing closing double quote in: listName: "Customers,



--
Paul T.

-- Sent from Mobile

Jul 31, 2014 at 1:42 PM
I've updated the script and the error resolved itself. The alert is causing an error with a missing "}", if I remove the alert the error resolves itself

Watching fiddler, I can see a call to the lists.asmx, but no data is being returned.

Thanks,
Jason
Jul 31, 2014 at 11:55 PM
you say you updated the script... can you post the revised code?

Aug 1, 2014 at 12:22 AM
here is my updated script
<script type="text/javascript">
var JSONdata; 
$(document).ready(function() {
  $().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Customers",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
       
JSONdata = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({
       mapping: {
         ows_ID: {mappedName: "ID", objectType: "Counter"},
         ows_Title: {mappedName: "Title", objectType: "Text"},
         ows_Created: {mappedName: "Created", objectType: "DateTime"},
         ows_Author: {mappedName: "Author", objectType: "User"},
         ows_Country: {mappedName: "Country", objectType: "MultiChoice"},
         ows_City: {mappedName: "City", objectType: "MultiChoice"},
         ows_Lead_x0020_Source: {mappedName: "LeadSource", objectType: "MultiChoice"},
         ows_Sales_x0020_Rep: {mappedName: "SalesRep", objectType: "UserMulti"},
         ows_Potential_x0020_Value: {mappedName: "PotentialValue", objectType: "Currency"}
        },    
   includeAllAttrs: true
  });
    }
  });
});
alert(JSONdata);
</script>
Aug 1, 2014 at 12:30 AM
Can you post the exact error including any script line numbers... I'm assuming you are seeing the error in the console? yes?
Aug 1, 2014 at 12:42 AM
I don't see anything in the code above that would complain of a missing curly brace... I just took your code and ran it through jshint (http://www.jshint.com/) no errors...

If you are trying to do something with JSONData, you need to include all of your logic inside of the .ready() method callback... Although you have async: false in the SPservices call, that block of code could run after the alert() at the bottom, because the page may not be "ready()" yet...

Run this slightly modified version and see if it helps:
(Note: it has a few more alert()'s to give you information about what's happening... )

$(document).ready(function() {
    
    var JSONdata; 
    
    alert("jQuery v" + $.fn.jquery + " | SPServices v" + $().SPServices.Version());
    
    $().SPServices({
        operation:      "GetListItems",
        async:          false,
        listName:       "Customers",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
        completefunc:   function (xData, Status) {
            
            alert("Ajax call done! Status = " + Status + " | Rows found: " + $(xData.responseXML).SPFilterNode("z:row").length);
            
            JSONdata = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({
                mapping: {
                    ows_ID: {
                        mappedName: "ID", 
                        objectType: "Counter"
                    },
                    ows_Title: {
                        mappedName: "Title", 
                        objectType: "Text"
                    },
                    ows_Created: {
                        mappedName: "Created", 
                        objectType: "DateTime"
                    },
                    ows_Author: {
                        mappedName: "Author", 
                        objectType: "User"
                    },
                    ows_Country: {
                        mappedName: "Country", 
                        objectType: "MultiChoice"
                    },
                    ows_City: {
                        mappedName: "City", 
                        objectType: "MultiChoice"
                    },
                    ows_Lead_x0020_Source: {
                        mappedName: "LeadSource", 
                        objectType: "MultiChoice"
                    },
                    ows_Sales_x0020_Rep: {
                        mappedName: "SalesRep", 
                        objectType: "UserMulti"
                    },
                    ows_Potential_x0020_Value: {
                        mappedName: "PotentialValue", 
                        objectType: "Currency"
                    }
                },    
                includeAllAttrs: true
            });
            
        }
    });
  
    alert(JSONdata);

});

Aug 1, 2014 at 12:51 AM
Everything makes sense until it hits the alert for the JSON Data, which returns [object Object]
Aug 1, 2014 at 12:52 AM
Yes. That's expected. You are seeing the result of SPXmlToJson. What did you want see?



--
Paul T.

-- Sent from Mobile

Aug 1, 2014 at 12:55 AM
For some reason I was expecting it to send back a string of data. I need to work on doing something with the data that is being returned.

Thanks for the help,
Jason
Aug 1, 2014 at 1:16 AM
The return value of SPXmlToJson is an array of objects - where each object is a customer from your list... This the ideal format for you to work with in javascript, so when you say you expected a String, that is probably not what you really wanted....

Example: Place this inside of the above method, before the last alert():
    var allCustomers = "Customers Found: ";
    for (var i = 0, j=JSONData.length; i<j; i++) {
        allCustomers += JSONData[i].Title + ", ";
    }
    alert(allCustomers);
Good luck.
Aug 1, 2014 at 1:30 AM
Here is my updated script, it's returning JSONData not defined in the console. I'm looking at jshint, but not seeing any issues noted
<script type="text/javascript">
$(document).ready(function() {
    
    var JSONdata; 
    
    alert("jQuery v" + $.fn.jquery + " | SPServices v" + $().SPServices.Version());
    
    $().SPServices({
        operation:      "GetListItems",
        async:          false,
        listName:       "Customers",
        CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
        completefunc:   function (xData, Status) {
            
            alert("Ajax call done! Status = " + Status + " | Rows found: " + $(xData.responseXML).SPFilterNode("z:row").length);


            JSONdata = $(xData.responseXML).SPFilterNode("z:row").SPXmlToJson({
                mapping: {
                    ows_ID: {
                        mappedName: "ID", 
                        objectType: "Counter"
                    },
                    ows_Title: {
                        mappedName: "Title", 
                        objectType: "Text"
                    },
                    ows_Created: {
                        mappedName: "Created", 
                        objectType: "DateTime"
                    },
                    ows_Author: {
                        mappedName: "Author", 
                        objectType: "User"
                    },
                    ows_Country: {
                        mappedName: "Country", 
                        objectType: "MultiChoice"
                    },
                    ows_City: {
                        mappedName: "City", 
                        objectType: "MultiChoice"
                    },
                    ows_Lead_x0020_Source: {
                        mappedName: "LeadSource", 
                        objectType: "MultiChoice"
                    },
                    ows_Sales_x0020_Rep: {
                        mappedName: "SalesRep", 
                        objectType: "UserMulti"
                    },
                    ows_Potential_x0020_Value: {
                        mappedName: "PotentialValue", 
                        objectType: "Currency"
                    }
                },    
                includeAllAttrs: true
            });
            
        }
    });
            var allCustomers = "Customers Found: ";
            for (var i = 0, j=JSONData.length; i<j; i++) {
                allCustomers += JSONData[i].Title + ", ";
            }
            alert(allCustomers);
});
</script>
Aug 1, 2014 at 1:42 AM
My mistake. Correct the variable name. It hours be a lowercase 'd' as in: JSONdata



--
Paul T.

-- Sent from Mobile

Aug 1, 2014 at 1:52 AM
I meant: It should be a lowercase 'd' as in: JSONdata

--
Paul T.

-- Sent from Mobile

Aug 1, 2014 at 1:58 AM
That worked!

Thanks for the help,
Jason