WSS + CAML: Getting current events from a Calendar list

Hi!
If you want to display events that happen right now, right this moment… this should be easy, I guess… WRONG! CAML and SharePoint find some problems working with Reocurring Items.

To go around this problem, you need to get all events that CAML get to this moment and after that, filter them using server code (In my sample I use C# )

Take a Look:

SPSite mySite = new SPSite(SPContext.Current.Site.Url);
SPWeb currentSite = mySite.AllWebs[web];
list = currentSite.Lists[listName];

currentSite.AllowUnsafeUpdates = true;
string datesQuery = "<DateRangesOverlap>"+
"<FieldRef Name='EventDate' />"+
"<FieldRef Name='EndDate' />  "+
"<FieldRef Name='RecurrenceID' />  "+
"<Value Type='DateTime' IncludeTimeValue='TRUE'>"+
"<Today />" +
"</Value>" +
"</DateRangesOverlap>";

string dates2Query = "<And><Leq><FieldRef Name='EventDate' />" +
"<Value Type='DateTime' IncludeTimeValue='TRUE'>"+
"<Today />" +
"</Value></Leq><Geq><FieldRef Name='EndDate' />" +
"<Value Type='DateTime' IncludeTimeValue='TRUE'>"+
"<Today />" +
"</Value></Geq></And>";

SPQuery quer = new SPQuery();
quer.CalendarDate = DateTime.Now;
quer.ExpandRecurrence = true;
quer.Query = "<Where><And><And>" + datesQuery + dates2Query + "</And>" + needAcceptQuery + "</And></Where>";

items = list.GetItems(quer);
if (items.Count.Equals(0))
{
//No Event
}
else
{
foreach (SPListItem item in items)
{
itemCurr = item.ParentList.GetItemById(item.ID);// ERROR "Value does not fall within the expected range."
if (((bool)itemCurr["Recurrence"]).Equals(true))
{
if(!DisplayToday(itemCurr))
continue;
}

//DO WHAT YOU WANT
}
}


private bool DisplayToday(SPListItem itemCurr)
{
DateTime dtEventDate = (DateTime)itemCurr["EventDate"];
DateTime dtEndDate = (DateTime)itemCurr["EndDate"];
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(itemCurr["RecurrenceData"].ToString());
XmlNode xElem = xDoc.GetElementsByTagName("repeat")[0];
xElem = xElem.FirstChild;
if (xElem.Name.Equals("weekly"))
{
string todayAttrib = getTodayAttribute();
bool isToday = false;
if (xElem.Attributes[todayAttrib] != null)
isToday = Boolean.Parse(xElem.Attributes[todayAttrib].Value);
if (!isToday) return false;
}
else
{
if (xElem.Name.Equals("monthly"))
{
int day = 1;
if (xElem.Attributes["day"] != null)
day = Int32.Parse(xElem.Attributes["day"].Value);
if (!day.Equals(DateTime.Today.Day)) return false;
}
else
{
if (xElem.Name.Equals("yearly"))
{
int day = 1, month = 1;
if (xElem.Attributes["day"] != null)
day = Int32.Parse(xElem.Attributes["day"].Value);
if (xElem.Attributes["month"] != null)
month = Int32.Parse(xElem.Attributes["month"].Value);
if (!(month.Equals(DateTime.Today.Month) && day.Equals(DateTime.Today.Day))) return false;
}
}
}
if (dtEventDate.Date < DateTime.Now.Date || dtEndDate.Date > DateTime.Now.Date)
{
if (!(dtEventDate.TimeOfDay < DateTime.Now.TimeOfDay
&& dtEndDate.TimeOfDay > DateTime.Now.TimeOfDay))
return false;
else
return true;
}
return false;
}

private string getTodayAttribute()
{
if (DateTime.Today.DayOfWeek.Equals(DayOfWeek.Monday))
return "mo";
else
if (DateTime.Today.DayOfWeek.Equals(DayOfWeek.Friday))
return "fr";
else
if (DateTime.Today.DayOfWeek.Equals(DayOfWeek.Saturday))
return "sa";
else
if (DateTime.Today.DayOfWeek.Equals(DayOfWeek.Sunday))
return "su";
else
if (DateTime.Today.DayOfWeek.Equals(DayOfWeek.Thursday))
return "th";
else
if (DateTime.Today.DayOfWeek.Equals(DayOfWeek.Tuesday))
return "tu";
else
return "we";
}

  1. No trackbacks yet.

Deixar um comentário

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Modificar )

Imagem do Twitter

You are commenting using your Twitter account. Log Out / Modificar )

Facebook photo

You are commenting using your Facebook account. Log Out / Modificar )

Connecting to %s

Seguir

Get every new post delivered to your Inbox.