2

I need to create a CAML query to obtain the following two events from a SharePoint 2010 calendar:

To consider:

  • There may be recurring events

  • And exceptions to events

The purpose is to show in a webpart the following two events, ie, greater than or equal to today.

Thanks!

1

It is quite difficult to make such queries. The documentation is somewhat ambiguous and contradictory in some cases. Finally this query gave me good results. I leave it here in case anyone else is interested.

SPQuery consulta = new SPQuery();

consulta.ViewFields = "";
consulta.ViewFields += "<FieldRef Name='Title'/>";
consulta.ViewFields += "<FieldRef Name='EventDate'/>";
consulta.ViewFields += "<FieldRef Name='EndDate'/>";
consulta.ViewFields += "<FieldRef Name='fRecurrence'/>";
consulta.ViewFields += "<FieldRef Name='RecurrenceData'/>";
consulta.ViewFields += "<FieldRef Name='fAllDayEvent'/>";

consulta.ExpandRecurrence = true;
// consulta.CalendarDate = DateTime.Now;

consulta.Query = "<Where><DateRangesOverlap><FieldRef Name='EventDate' /><FieldRef Name='EndDate' /><FieldRef Name='RecurrenceID' /><Value IncludeTimeValue='TRUE' Type='DateTime'><Now/></Value></DateRangesOverlap></Where>";
consulta.Query += "<OrderBy><FieldRef Name='EventDate' /></OrderBy>";

consulta.RowLimit = 2;

Source of inspiration:

http://sharepointchan.blogspot.com.ar/2011/10/situation-i-want-to-get-all-calendar.html

http://www.nothingbutsharepoint.com/2012/04/26/use-spservices-to-get-recurring-events-as-distinct-items-aspx/

| improve this answer | |
1

Another option is to avoid the CAML and use calendar views. According to this blog post, you can create a view that shows recurring events and displays them in chronological order. Then at the end of the view definition, you can limit the view to show the next 2 items.

To create the view, go to the calendar and follow these steps:

  1. Ribbon -> Calendar -> Create View
  2. Select "Standard View, With expanded Recurring Events"
  3. Name the View and arrange the Columns - I put Start Time near the front.
  4. At the bottom under Item Limit, select 2

The View will display like so:

enter image description here

This view can be overlaid onto another calendar or pulled into another site using a Web Part. This solution is simpler and out of the box and is easier to maintain than a coded solution involving CAML and CSOM.

| improve this answer | |
  • It is a very good choice. In my case a bit complicated because I develop all in C #. That means that the views also have to create in C # for each of the sites. But I like the idea of using standard functionality. Thank you! – Juan Pablo Pussacq Laborde Apr 20 '16 at 11:20
  • For people who visit this question in the future - this solution works Out Of the Box (OOB) whereas the accepted answer by the OP involves a developer, installation and runs on the farm. – Taterhead May 3 '16 at 7:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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