Inconvenient Recurrence Data from a SharePoint Calendar using Client OM (JS)

The CAML to get SharePoint items from lists has a nice feature when it comes to Calendar data. Using the <DateRangesOverlap/> it is possible to check if a given date is within a range of dates and when passing the ExpandRecurrence property it even takes the Recurrence Data into account. Using Sandboxed Solutions to do so we can easily set this property SPQuery.ExpandRecurrence = true; However when it comes to the JavaScript Client OM we cannot use this property.

WebServices

The only way to query SharePoint for Recurrence Data using Client OM is by using theGetListItems Method from the List WebService (/_vti_bin/lists.asmx). The XML passed into the WebService can handle QueryOptions and we can use this to specify if ExpandRecurrence should be true. The Soap XML looks like this:

<soapenv:Envelopexmlns: soapenv=’http://schemas.xmlsoap.org/soap/envelope/’>    <soapenv:Body>     <GetListItemsxmlns =’http://schemas.microsoft.com/sharepoint/soap/’>        <listName>[MyListName]</listName>        <query>          <Query>            <Where>              <DateRangesOverlap>                <FieldRefName=’EventDate’ />                <FieldRefName=’EndDate’ />                <FieldRefName=’RecurrenceID’ />                <ValueType=’DateTime’>                  <Today />                </Value>              </DateRangesOverlap>            </Where>          </Query>        </query>        <queryOptions>          <QueryOptions>            <ExpandRecurrence>TRUE</ExpandRecurrence>            <CalendarDate>              <Today />            </CalendarDate>            <ViewAttributesScope=’RecursiveAll’ />          </QueryOptions>        </queryOptions>        <viewFields>          <ViewFields>            <FieldRefName=’EventDate’ />            <FieldRefName=’EndDate’ />            <FieldRefName=’fAllDayEvent’ />            <FieldRefName=’fRecurrence’ />            <FieldRefName=’Title’ />          </ViewFields>        </viewFields>        <RowLimit>10</RowLimit>      </GetListItems>    </soapenv:Body>  </soapenv:Envelope>

This example checks if there are events overlapping with <Today />. I found that it’s best to check with tags like <Today/>. <Week/>, <Month/> and <Year/> instead of using UTC Dates. Notice the <queryOptions/> wrapper passing the Options. Building regular CAML XML using the inner <QueryOptions/> tag didn’t work unfortunately. Both <ExpandRecurrence/> as <CalendarDate/> are mandatory in order for this query to work.

Tip: When using Client OM to process SharePoint data requests it’s best practice to specify as much properties as possible. Specifying <ViewFields/> will only return these fields instead of all possible fields that come with an Item. If you know the maximum amount of results specifying <RowLimit/> also helps to improve the performance on your Query.