0

I'm trying to access a SharePoint list and return the calendar dates for a custom webpart I made. It was working fine, then I decided to only retrieve the date selected rather than the whole calendar, so I wanted to add a where clause.

I've tried 'yyyy-MM-dd', 'yyyy-MM-ddThh:mm:ssZ', and 'yyyy-MM-dd hh:mm:ssZ' as string formats I've also tried MM/dd/yyyy as a date format.

I'm using jQuery, and I do have list items in the calendar. I'm assuming my date is not in the correct format.

        var date = $(this).attr('date');            

        var sharepointDate = Date.parse(date).toString('yyyy-mm-ddT00:00:01Z');
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
            <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
            <listName>CorporateCalendar</listName> \
            <viewFields> \
            <ViewFields> \
            <FieldRef Name='Title' /> \
            </ViewFields> \
            </viewFields> \
            <query><Query><Where><Geq><FieldRef Name='EventDate' /><Value Type='DateTime'>" + sharepointDate + "</Value></Geq></Where></Query></query> \
            <rowLimit>500</rowLimit> \
            </GetListItems> \
            </soapenv:Body> \
            </soapenv:Envelope>";

If I take the where clause out I receive all the items in the calendar. If the query is in there, I receive no results.

Thanks in advance

Working code:

    var sharepointDate = Date.parse(date).toString('yyyy-MM-dd');
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
            <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
            <listName>CorporateCalendar</listName> \
            <viewFields> \
            <ViewFields> \
            <FieldRef Name='Title' /> \
            </ViewFields> \
            </viewFields> \
            <query><Query><Where><Eq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='False'>" + sharepointDate + "</Value></Eq></Where></Query></query>\
            <rowLimit>1500</rowLimit> \
            </GetListItems> \
            </soapenv:Body> \
            </soapenv:Envelope>";
Rob Scott
  • 7,921
  • 5
  • 38
  • 63

2 Answers2

0

The problem is not in your CAML Query but it's in the parsing of the Date. Unfortunately the Date.toString method in JavaScript doesn't support formatting in the same way that C# does. The toString method doesn't accept any arguments, so you have to parse it to a valid ISO format yourself.

I took the ISODateString method from the question How do I output an ISO-8601 formatted string in Javascript?. We can use this method to get a valid value for sharepointDate, the code should look something like this:

function ISODateString(d){
 function pad(n){return n<10 ? '0'+n : n}
 return d.getUTCFullYear()+'-'
      + pad(d.getUTCMonth()+1)+'-'
      + pad(d.getUTCDate())+'T'
      + pad(d.getUTCHours())+':'
      + pad(d.getUTCMinutes())+':'
      + pad(d.getUTCSeconds())+'Z'}
var sharepointDate = ISODateString(new Date(Date.parse(date)));

Now the sharepointDate will be in the 'yyyy-mm-ddT00:00:01Z' format. And since this is the format that is expected in the CAML query you can now get the items filtered on date.

Community
  • 1
  • 1
Remco Eissing
  • 1,275
  • 2
  • 12
  • 15
0
    var sharepointDate = Date.parse(date).toString('yyyy-MM-dd');
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
            <soapenv:Body> \
            <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
            <listName>CorporateCalendar</listName> \
            <viewFields> \
            <ViewFields> \
            <FieldRef Name='Title' /> \
            </ViewFields> \
            </viewFields> \
            <query><Query><Where><Eq><FieldRef Name='EventDate' /><Value Type='DateTime' IncludeTimeValue='False'>" + sharepointDate + "</Value></Eq></Where></Query></query>\
            <rowLimit>1500</rowLimit> \
            </GetListItems> \
            </soapenv:Body> \
            </soapenv:Envelope>";
Rob Scott
  • 7,921
  • 5
  • 38
  • 63