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("https://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());
}
Console.ReadKey();
}
But this was not the case with list.asmx before the this KB i.e https://support.microsoft.com/kb/974087 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: - https://support.microsoft.com/?kbid=977026
MOSS: - https://support.microsoft.com/?kbid=977027
And set the “CalendarDate” and “ExpandRecurrence” in the “QueryOptions” parameter :-
queryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
<DateInUtc>TRUE</DateInUtc><ViewAttributes Scope=\"Recursive\" />" +
<RecurrencePatternXMLVersion>v3</RecurrencePatternXMLVersion>" +
<ExpandRecurrence>True</ExpandRecurrence>"+
<CalendarDate>"+ Fromdatestring + "</CalendarDate>"+
<RecurrenceOrderBy>TRUE</RecurrenceOrderBy>"+
<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 = "https://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("https://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>" +
"<ExpandRecurrence>True</ExpandRecurrence>"+
"<CalendarDate>"+ Fromdatestring + "</CalendarDate>"+
"<RecurrenceOrderBy>TRUE</RecurrenceOrderBy>"+
"<ViewAttributes Scope=\"RecursiveAll\"/>";
try
{
System.Xml.XmlNode nodeListItems = listService.GetListItems(listName, "", query, viewFields, null, queryOptions, web.ID.ToString());
Console.Write(nodeListItems.InnerXml);
XmlDocument xmlListItems = new XmlDocument();
xmlListItems.AppendChild(xmlListItems.ImportNode(nodeListItems, true));
xmlListItems.Save("c://CalendarListItemsIncoming.xml");
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Console.WriteLine("Message:\n" + ex.Message + "\nDetail:\n" +
ex.Detail.InnerText +
"\nStackTrace:\n" + ex.StackTrace);
}
Console.ReadKey();
}
}
Understanding the SharePoint calendar and how to export it to iCal format
Collaborative Application Markup Language Core Schemas
https://msdn.microsoft.com/en-us/library/ms462365.aspx
Query Schema