Setting CAMLQuery for SPServices GetListItems within Sharepoint Forms Designer

Nov 26, 2012 at 9:01 PM

From within SharePoint Forms Designer I'm trying to populate some text boxes based upon a drop down list. I'm trying to use jQuery and SPServices GetListItems to accomplish this. I'm already using the SPCascadeDropdowns on the same page with success.

I'm trying to set the CAMLQuery string for the service call and it will not parse and I get a javascript error when the page loads.

Any help would be greatly appreciated....thanks in advance.

Error Message: Expected ';'

Code Sample:

$(document).ready(function() {

      $('.callname').live('change', function(){
                   
                    var selectedValue = $('.callname option:selected').text();
                   
                   
                    var test = "<Query>" +
                        "<Where>" +
                            "<Eq>" +
                                "<FieldRef Name='Title' /><Value Type='Text'>Bob Tester</Value>" +
                            "</Eq>" +
                        "</Where>" +
                    "</Query>";                                   
                   
                    $().SPServices({
                    operation: "GetListItems",
                    async: false,
                    listName: "Plant Personnel",                   
                    CAMLRowLimit: 1,
                    CAMLQuery: test,                                       
                    completefunc: function (xData, Status) {
                        alert(xData);
                        $(xData.responseXML).SPFilterNode("z:row").each(function(){
        
                        });
                    }
                    });

      });
               
 });

Coordinator
Nov 27, 2012 at 12:27 PM

I'm not spotting it, but obviously the browser thinks you're missing a semi-colon. There ought to be a line number associated with the error in the console, which should show you roughly where.

M.

Nov 27, 2012 at 3:58 PM

When I run the page and look at the script the page is creating, the section of code that fails is below. Specifically the line where FieldRef and Value tags are and its converting 'Title' to "Title" and thinks there should be an end of statement ";". So it has to do with the single quotes or tags within the string.

 

                   var test = "<Query>" +
                        "<Where>" +
                            "<Eq>" +
                                "<FieldRef Name="Title"></FieldRef><Value Type="Text">Bob Tester</Value>" +
                            "</Eq>" +
                        "</Where>" +
                    "</Query>";

If I modify the code to check out the behavior something like this works

var test;               
test ="<Query><Where><Eq>FieldRef name='Title'</Eq></Where></Query>";
alert (test);

As soon as I wrap <> around the FieldRef Tag my error is triggered (Error: Expected ";")

   test ="<Query><Where><Eq><FieldRef name='Title' /></Eq></Where></Query>";    

Any thoughts?          

Nov 27, 2012 at 4:16 PM
"<FieldRef Name="Title"></FieldRef><Value Type="Text">Bob Tester</Value>"

has nested double quotes.  To correctly nest quoted items in this case, the easiest is...

"<FieldRef Name='Title'></FieldRef><Value Type='Text'>Bob Tester</Value>"

(Notice single quotes around the internal items).

 

 

Coordinator
Nov 27, 2012 at 4:19 PM

Yup, it's the quotes nesting. Sometimes this stuff is hard to spot!

M.

Nov 27, 2012 at 4:30 PM

My original code and post has single quotes in the string assignment (re-pasted code snippet below). What I was trying to explain in my 2nd post is running the debugger and looking at the generated script the single quotes are converted to double quotes. Also, that having single quotes in string is fine until I introduce the <> in the FieldRef tag. So either my browser is converting single to double quotes (this happens in both IE and FireFox) or The SharePoint Designer is doing something to it.

 

                    var test = "<Query>" +
                        "<Where>" +
                            "<Eq>" +
                                "<FieldRef Name='Title' /><Value Type='Text'>Bob Tester</Value>" +
                            "</Eq>" +
                        "</Where>" +
                    "</Query>";

Coordinator
Nov 27, 2012 at 4:53 PM
Edited Nov 27, 2012 at 5:04 PM

Make sure that none of your quotes are "curly quotes". You often end up with those when you copy code from a blog post or other Web page. What you've pasted directly above looks right, but try simply combining it all into one long string.

var test = "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Bob Tester</Value></Eq></Where></Query>";

M.

Nov 27, 2012 at 5:01 PM

Nice catch @gkoliver!

It's interesting that the original post has single quotes. Was it edited?  If not, then the problem may be something else altogether.

 

Cheers,
Matthew 

Nov 27, 2012 at 6:02 PM

Setting my variable in one long string (code snippet below) does not make a difference and I receive the same error. 

var test = "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>Bob Tester</Value></Eq></Where></Query>";

I believe something else is causing this behavior, but I have no clue why or what it is. That's why I thought maybe the designer was causing this behavior.

Nov 27, 2012 at 8:00 PM

I honestly think it has something to do with your .live() call or the usage of .SPFilterNode().  There were some changes to jQuery and as a result SPServices.  The usage of .live() was deprecated in 1.7 and you using .SPFilterNode() suggests you are at least using 1.7+.

Can you confirm the version of jQuery and SPServices that you are using on this page?

 

Cheers,
Matthew 

Nov 27, 2012 at 8:29 PM

I'm using...

   
   

.live() call seems to fire ok but I can change it.

Also If I remove the $().SPServices code and just have the string assignment I still get the error.

I know that the $().SPServices call will return data if I don't try to add a filter (ie where clause).

Nov 27, 2012 at 8:49 PM

I've not used SPDesigner with SPServices so that is a mystery to me.  I wonder if you were to escape the quotes around the field Name and Type values, would that help SPDesigner interpret things correctly?

var test = "<Query><Where><Eq><FieldRef Name=\'Title\' /><Value Type=\'Text\'>Bob Tester</Value></Eq></Where></Query>";

 

 

Coordinator
Nov 28, 2012 at 6:16 AM
Edited Nov 28, 2012 at 6:17 AM

Just for an example, here's a CAMLquery I just set up:

     CAMLQuery: "<Query><OrderBy><FieldRef Name='PublishDate' Ascending='FALSE'/></OrderBy>" +
       "<Where>" +
        "<And>" +
         "<IsNotNull><FieldRef Name='PromoImg' /></IsNotNull>" +
         "<Neq><FieldRef Name='PublishDate' /><Value Type='DateTime'>1900-01-01 00:00:00</Value></Neq>" +
        "</And>" +
       "</Where></Query>",

As you can see it's a bit more complex, but the formatting is basically the same as yours.

Can you try the GetListItems call without anything else wrapped around it except the document.ready()?

M.

p.s. I *always* work in SharePoint Designer, so I don't think that it's doing anything to you. I edited the CAMLQuery above in SPD2007.

Nov 28, 2012 at 8:15 PM

Well, that answers my question.  I am not authorized to use SharePoint Designer.   The organization that owns the servers here have it in their head that if I use SPDesigner, I may destroy civilization as we know it  ;-)   I edit/store all of my scripts in Notepad as *.js files, upload them to a JavaScript Document Library in my site, then reference them from within the affected page via a CEWP.  I've never had any trouble that way--other than the obvious pain of editing a script in Notepad.

Nov 28, 2012 at 11:10 PM

If you are forced down this route, there are much better javascript editors than Notepad. One surprising favorite seems to be Visual Studio (with extensions). There are others such as Aptana Studio, but even NotePad++ would be an improvement.

From: gkoliver [email removed]
Sent: Wednesday, November 28, 2012 8:16 PM
To: steve.eagleson@sky.com
Subject: Re: Setting CAMLQuery for SPServices GetListItems within Sharepoint Forms Designer [SPServices:404644]

From: gkoliver

Well, that answers my question. I am not authorized to use SharePoint Designer. The organization that owns the servers here have it in their head that if I use SPDesigner, I may destroy civilization as we know it ;-) I edit/store all of my scripts in Notepad as *.js files, upload them to a JavaScript Document Library in my site, then reference them from within the affected page via a CEWP. I've never had any trouble that way--other than the obvious pain of editing a script in Notepad.