1

i have a problem with a CAML Query for a Sharepoint Online(Office365) Calendar. I try to get all Calendar Appointments between two dates. Here is my code:

        private void getFreeDays()
        {
        List urlaubskalender = clientContext.Web.Lists.GetByTitle("Urlaubskalender");

        DateTime startDate = Convert.ToDateTime(dpEventDate.SelectedDate);
        DateTime endDate = Convert.ToDateTime(dpEndDate.SelectedDate);

        CamlQuery getDatesQuery =new CamlQuery();
        getDatesQuery.ViewXml = "<Query>"+
                                    "<Where>"+
                                        "<And>"+
                                            "<Geq>"+
                                                "<FieldRef Name='EventDate' />"+
                                                    "<Value Type='DateTime'>"+startDate+"</Value>"+
                                            "</Geq>"+
                                            "<Leq>"+
                                                "<FieldRef Name='EndDate' />"+
                                                    "<Value Type='DateTime'>"+endDate+"</Value>"+
                                            "</Leq>"+
                                        "</And>"+
                                    "</Where>"+
                                "</Query>";

        eventCollection = urlaubskalender.GetItems(getDatesQuery);
        clientContext.Load(eventCollection);

        clientContext.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(onQSGetEvents), null);
        }

The Problem is that I get appointments from 01.01.2013 when I select 01.02.2013 as EventDate and 08.02.2013 as EndDate. The appointment is a reccurent one, maybe this is the cause of the problem? I tried to find some informations to create a CAML Query without recurring appointment but i couldn't found something. Maybe the failure is something total different...

I hope someone can help me...and sorry for my bad english, i gave my best ;)

2 Answers 2

2

I have solved the problem. I can change the format of the DateTime variable with startDate.ToString("yyyy-MM-ddTHH:mm:ssZ")

I have build the query with the U2U Query Builder (great tool!) here is the query if someone is interested in thisgetDatesQuery.ViewXml = "<Query>" + "<Where>" + "<And>" + "<Eq>" + "<FieldRef Name='fRecurrence' />" + "<Value Type='Recurrence'>false</Value>" + "</Eq>" + "<And>" + "<Leq>" + "<FieldRef Name='EventDate' />" + "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + startDate.ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Value>" + "</Leq>" + "<Geq>" + "<FieldRef Name='EndDate' />" + "<Value IncludeTimeValue='TRUE' Type='DateTime'>" + endDate.ToString("yyyy-MM-ddTHH:mm:ssZ") + "</Value>" + "</Geq>" + "</And>" + "</And>" + "</Where>" + "<OrderBy>" + "<FieldRef Name='EventDate' Ascending='True' />" + "</OrderBy>" + "</Query>";

1
  • The ToString method might solve your issue regarding the date formatting, but I have my doubts, that your code will expand and return recurring events. Based on my experience, one should use the DateRangesOverlap CAML operator and the Lists web service (no support in the Client OM or in OData / REST!) if one need it on the client side, see an example here: codeplayandlearn.wordpress.com/2015/06/22/…
    – pholpar
    Commented Jan 18, 2017 at 10:15
1

You need to use the SPUtility.CreateISO8601DateTimeFromSystemDateTime() method to get your date times in the proper format.

1
  • Thanks for your answer. I use the Client Object Model. There is just a Class called ClientUtility ( msdn.microsoft.com/en-us/library/ee538654.aspx ). It has no method CreateISO8601DateTimeFromSystemDateTime() or something like this. Do you know a workaround for my issue?
    – Alexander
    Commented Feb 5, 2013 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.