Previous: Setting Page Content In SharePoint 2007 Programattically, And It's Drawbacks
Next: I Think I'm Glad Wales Lost

Querying Sharepoint Events Using SPQuery (CAML)

Here's an example of how I query a SharePoint 2007 Event List for events that occur after the current date, using the SPQuery class.

using (SPSite site = new SPSite(serverRootURL))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPList eventsList = web.Lists["Events"];
SPQuery eventQuery = new SPQuery();
eventQuery.Query = "<Where><Gt><FieldRef Name='EventDate'/><Value Type='DateTime'>" + DateTime.Now.ToString("yyyy-MM-dd") + "</Value></Gt></Where>";
SPListItemCollection eventItems = eventsList.GetItems(eventQuery);
foreach (SPListItem eventItem in eventItems)
{
//List Output Here
}
}
}

The DateTime value needs to be in the yyyy-mm-dd format for this to work.

Comments

No comments have been added.

Add a comment

0 comments

Latest Tweets

  • @CaiGriffiths This in on a par with Lou Reed being penalised for shouting at someone. Crazy. So tThumping tackles being outlawed

  • RIP Corey Haim

  • RT @nineplanfailed: New show announced in Cardiff at Tommy's Bar on March 10, 2010 http://lnk.ms/6z2rq

  • Spent 10 minutes on databases and 30 minutes inventing / playing google analytics top trumps

  • Teaching databases to a boy with autism. We're both enjoying.

Follow Me