How to query lists.asmx to get the recurring events?


How to query list.asmx to get the recurring events?

I have created a calendar List named “MOSS Calander” and


Added some recurring events :-


To query the recurring items, you have to set the “ExpandRecurrence” property of the SPQuery object to true (If you using the CAML query).

SharePoint OM code to get the recurring calendar list items:-

// Get the Events list
  SPSite site = new SPSite("http://blrs2r11-2");
  SPWeb web = site.RootWeb;
  SPList calendarList = web.Lists["MOSS Calander"];
  // Construct a query that expands recurring events
  SPQuery query = new SPQuery();
  query.ExpandRecurrence = true;
  query.Query = "<Where><DateRangesOverlap><FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef Name=\"RecurrenceID\" /><Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap></Where>";
  // Look forward from the beginning of the current month
  query.CalendarDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
  // Returns all items (including recurrence instances) that
  // would appear in the calendar view for the current month
  SPListItemCollection calendarItems = calendarList.GetItems(query);
  foreach (SPListItem item in calendarItems)
  Console.WriteLine(item["Title"] + ": starts "
  + item["EventDate"].ToString() + " and ends "
  + item["EndDate"].ToString());


But this was not the case with list.asmx before the this KB i.e was released. Even if you set the “CalendarDate” and “ExpandRecurrence”, you get the one instance of the event. I installed the December Cumulative patch to ensure that I am latest path.

Downloaded and deployed the December 2009 CU

WSS: -


And set the “CalendarDate” and “ExpandRecurrence” in the “QueryOptions” parameter :-

queryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
<DateInUtc>TRUE</DateInUtc><ViewAttributes Scope=\"Recursive\" />" +
<RecurrencePatternXMLVersion>v3</RecurrencePatternXMLVersion>" +
<CalendarDate>"+ Fromdatestring + "</CalendarDate>"+
<ViewAttributes Scope=\"RecursiveAll\"/>";

Here is the complete code to query the list.asmx web service:-

Lists.Lists listService = new Lists.Lists(); 
        listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
        listService.Url = "http://blrs2r11-2/_vti_bin/lists.asmx";
        System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
        string listName = "{46FBBA73-C379-4B06-AD1D-7D35FACBE2F1}";
        string Fromdatestring = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today.AddDays(2));
        string Todatestring = Microsoft.SharePoint.Utilities.SPUtility.CreateISO8601DateTimeFromSystemDateTime(DateTime.Today.AddDays(8));
        using(SPSite site = new SPSite("http://blrs2r11-2"))
        using (SPWeb web = site.OpenWeb())
            System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
            System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
            System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
            query.InnerXml = "<Where><DateRangesOverlap><FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef Name=\"RecurrenceID\" /><Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap></Where><OrderBy><FieldRef Name='ID' /></OrderBy>";
            viewFields.InnerXml = ""; 
            queryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
            "<DateInUtc>TRUE</DateInUtc><ViewAttributes Scope=\"Recursive\" />" +
            "<RecurrencePatternXMLVersion>v3</RecurrencePatternXMLVersion>" +
            "<CalendarDate>"+ Fromdatestring + "</CalendarDate>"+
            "<ViewAttributes Scope=\"RecursiveAll\"/>";
            System.Xml.XmlNode nodeListItems = listService.GetListItems(listName, "", query, viewFields, null, queryOptions, web.ID.ToString());
            XmlDocument xmlListItems = new XmlDocument();
            xmlListItems.AppendChild(xmlListItems.ImportNode(nodeListItems, true));
            catch (System.Web.Services.Protocols.SoapException ex)
            Console.WriteLine("Message:\n" + ex.Message + "\nDetail:\n" +
            ex.Detail.InnerText +
            "\nStackTrace:\n" + ex.StackTrace);

Understanding the SharePoint calendar and how to export it to iCal format

Collaborative Application Markup Language Core Schemas

Query Schema

Comments (0)

Skip to main content