{"id":1451,"date":"2013-04-30T22:45:50","date_gmt":"2013-04-30T21:45:50","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1451"},"modified":"2013-04-30T22:45:50","modified_gmt":"2013-04-30T21:45:50","slug":"daterangesoverlap","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2013\/04\/30\/daterangesoverlap\/","title":{"rendered":"DateRangesOverlap"},"content":{"rendered":"<p>When working with calendar items created with recurrence patterns, there&#8217;s no immediately obvious way through the object model to expose the individual recurrence items.  If you create a calendar event with a daily recurrence pattern which generates 6 separate events, you&#8217;re only ever given one item when looking for it through code.<\/p>\n<p>Using CAML, it&#8217;s easy to get all 6 items back as individual items through the use of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms436080.aspx\" title=\"DateRangesOverlap Element\" target=\"_blank\">DateRangesOverlap<\/a> element.<\/p>\n<p>The structure of the <em>DateRangesOverlap<\/em> element looks like:<\/p>\n<pre lang=\"xml\">\r\n<DateRangesOverlap>\r\n  <FieldRef Name='EventDate' \/>\r\n  <FieldRef Name='EndDate' \/>\r\n  <FieldRef Name='RecurrenceID' \/>\r\n  <Value IncludeTimeValue=\\\"TRUE\\\" Type='DateTime'>\r\n    <Now \/>\r\n  <\/Value>\r\n<\/DateRangesOverlap>\r\n<\/pre>\n<p><!--more--><br \/>\nWhat this does is bring back any recurring item where the start date (EventDate) and the end date (EndDate) overlap the value specified, which in the above example is <em>Now<\/em><\/p>\n<p>Possible values here are:<\/p>\n<ul>\n<li>Year &#8211; Retrieves everything from a year in the past\\future using today&#8217;s date<\/li>\n<li>Now &#8211; Brings back everything up to two years from today&#8217;s date and time<\/li>\n<li>Month &#8211; Brings back all items for the current month<\/li>\n<li>Day &#8211; As it sounds, everything that occurs within the current day<\/li>\n<\/ul>\n<p>Using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spquery.aspx\" title=\"SPQuery class\" target=\"_blank\">SPQuery<\/a> class you can set the date the above acts upon, so instead of the current date and time it could be a past or future date.<\/p>\n<p>Take a look at the following example:<\/p>\n<pre lang=\"csharp\">\r\nSPQuery query = new SPQuery();\r\nquery.Query =\r\n    \"<Where><DateRangesOverlap><FieldRef Name='EventDate' \/><FieldRef Name='EndDate' \/><FieldRef Name='RecurrenceID' \/><Value Type='DateTime'><Month \/><\/Value><\/DateRangesOverlap><\/Where>\";\r\nquery.ExpandRecurrence = true;\r\nquery.CalendarDate = DateTime.Now.AddMonths(-2);\r\n<\/pre>\n<p>The important thing to remember when using the <em>DateRangesOverlap<\/em> element is to also set the <a href=\"http:\/\/msdn.microsoft.com\/en-gb\/library\/microsoft.sharepoint.spquery.expandrecurrence.aspx\" title=\"SPQuery.ExpandRecurrence property\" target=\"_blank\">ExpandRecurrence<\/a>  property of your <em>SPQuery<\/em> class to true, otherwise you won&#8217;t get any recurring items back when running it.<\/p>\n<p>The example above will query for recurring events and return all that occurred during the month two months prior to the current date.<\/p>\n<p>You&#8217;ll also get back any non-recurring item that exists within the specified time period, which includes single all day events and one off calendar entries.<\/p>\n<p>So, based on a CalendarDate of 1 May 2013, specifying <em>Year<\/em> would bring back items that occurred between 1 May 2012 and up to 1 May 2014.<\/p>\n<p>For <em>Now<\/em>, you would get back items between 1 May 2013 and 1 May 2015.<\/p>\n<p>Day and Month should be fairly self explanatory.<\/p>\n<p>Adding the <em>IncludeTimeValue<\/em> attribute to the value node will further filter your results based on the time specified for the <a href=\"http:\/\/msdn.microsoft.com\/en-gb\/library\/microsoft.sharepoint.spquery.calendardate.aspx\" title=\"SPQuery.CalendarDate property\" target=\"_blank\">CalendarDate<\/a> property, or the current time if this is not set.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with calendar items created with recurrence patterns, there&#8217;s no immediately obvious way through the object model to expose the individual recurrence items. If you create a calendar event with a daily recurrence pattern which generates 6 separate events, &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2013\/04\/30\/daterangesoverlap\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[3],"tags":[27,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-np","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1451"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1451"}],"version-history":[{"count":11,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1451\/revisions"}],"predecessor-version":[{"id":1462,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1451\/revisions\/1462"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}