Stump the Panel: SharePoint Q&A

Immediate solutions for common SharePoint questions
It is currently Fri Nov 21, 2014 6:11 am

All times are UTC - 5 hours [ DST ]


Forum rules


What you are viewing is an archive of Stump the Panel. It remains for reference purposes. Please post all new questions on NothingButSharePoint.com
https://www.nothingbutsharepoint.com/sites/eusp/Forum



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: PreSaveAction and CAML Query
PostPosted: Tue May 10, 2011 3:20 pm 
Offline
User avatar

Joined: Mon Jan 04, 2010 1:15 am
Posts: 1839
Location: North Carolina
I'm working on a script i want to adapt into a PreSaveAction. Right now I'm just doing it in a document.ready so I can test my output. It seems I'm having an issue with my CAMLQuery portion. When I comment it out, I get data back. When it is not commented, I'm not getting my alert. I tested the CAML in U2U so I know it's working properly, but I must be doing something wrong.

Code:
var queryStringVals = $().SPServices.SPGetQueryString();
var cID = queryStringVals["c"];
//var cid = cID + ";#" + cID
var email = $().SPServices.SPGetCurrentUser({fieldName: "EMail",debug: false});
var ID = $().SPServices.SPGetCurrentUser({fieldName: "ID",debug: false});
//var string = ID + ";#" + email
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Current Registrations",
    //CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='CourseID' /><Value Type='Lookup'>"+ cID +"</Value></Eq><Eq><FieldRef Name='EmployeeEmail' /><Value Type='User'>"+ email +"</Value></Eq></And><Where></Query>",
    completefunc: function (xData, Status) {
        //iC = $(xData.responseXML).find("[nodeName=rs:data]").attr("ItemCount");
   alert(xData.responseXML.xml);
     }
   });


Do you see any glaring issues why GetListItems would not work with my CAML Query?


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Tue May 10, 2011 3:41 pm 
Offline
User avatar

Joined: Mon Jan 04, 2010 1:15 am
Posts: 1839
Location: North Carolina
Typo in my ending where syntax. Closed it and that resolved it.


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Tue May 10, 2011 4:53 pm 
Offline
User avatar

Joined: Mon Jan 04, 2010 1:15 am
Posts: 1839
Location: North Carolina
This works great as a document.ready function, but PreSave it never errors. I'm not sure what is preventing it from canceling the save action.

Code:
function PreSaveAction() {
var queryStringVals = $().SPServices.SPGetQueryString();
var cID = queryStringVals["c"];
var email = $().SPServices.SPGetCurrentUser({fieldName: "EMail",debug: false});
var iC = "0";
$().SPServices({
    operation: "GetListItems",
    async: false,
    listName: "Current Registrations",
    CAMLQuery: "<Query><Where><And><Eq><FieldRef Name='CourseID' /><Value Type='Lookup'>"+ cID +"</Value></Eq><Eq><FieldRef Name='EmployeeEmail' /><Value Type='User'>"+ email +"</Value></Eq></And></Where></Query>",
    completefunc: function (xData, Status) {
        iC = $(xData.responseXML).find("[nodeName=rs:data]").attr("ItemCount");
      //alert(xData.responseXML.xml);
     }
   });

if(iC == "0")
    {
      return true;     
    }
      return false;
      alert("You've already registered for this session.");
      window.location.href="http://ww.yahoo.com";
}


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Tue May 10, 2011 5:17 pm 
Offline

Joined: Tue Jan 05, 2010 5:59 am
Posts: 145
A few things I notice. I guess you are not using a newer jQuery library as you are required to have single quotes on the selector rs:data (Just pointing that out as I was sure you were already aware. The next thing that I would do is modify the last part to something like this:

Code:
if(iC != "0")
    {
      return true;     
    }
else
{
      alert("You've already registered for this session.");
      window.location.href="http://ww.yahoo.com";
}


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Tue May 10, 2011 7:58 pm 
Offline
User avatar

Joined: Mon Jan 04, 2010 1:15 am
Posts: 1839
Location: North Carolina
Thanks for the suggestions, I'll investigate them tomorrow.


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Wed May 11, 2011 8:34 am 
Offline
User avatar

Joined: Mon Jan 04, 2010 1:15 am
Posts: 1839
Location: North Carolina
Weird, it just doesn't want to work as a presaveaction. I'll just proceed with it in my document.ready function as it makes the most sense to execute it when the page has loaded and not when they try to save the item.


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Wed May 11, 2011 8:49 am 
Offline
User avatar

Joined: Sat Jan 02, 2010 4:29 pm
Posts: 589
Location: Boston, MA, USA
Eric:

IT should work exactly the same in a PreSaveAction as in a $(document).ready().

Be sure to add single quotes around nodeNames:
Code:
iC = $(xData.responseXML).find("[nodeName='rs:data']").attr("ItemCount");


M.


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Wed May 11, 2011 9:18 am 
Offline
User avatar

Joined: Mon Jan 04, 2010 1:15 am
Posts: 1839
Location: North Carolina
I did wrap rs:data in single quotes. In document ready, I alert the the iC and it returns correctly. I even put alerts in the if blocks and those alert correctly. When I move this into a presaveaction, the alerts in the if block don't fire and the item is saved always.


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Wed May 11, 2011 9:37 am 
Offline
User avatar

Joined: Sat Jan 02, 2010 6:02 pm
Posts: 1035
Location: Silver Spring, MD
Is this form a custom form or an OOTB form?


Top
 Profile  
 
 Post subject: Re: PreSaveAction and CAML Query
PostPosted: Wed May 11, 2011 9:45 am 
Offline

Joined: Tue Jan 05, 2010 5:59 am
Posts: 145
I am not sure if I reversed my return true/false in my code as it seems backwards to me as to which one to use. One thing to just test would be to remove all the code and put in a simple alert in the presave function to be sure that it is firing. Just a thought.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group