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

  • @sonnytoiparker http://twitpic.com/2kkard - Which one's Tommy's?

  • @nineplanfailed Sounds like my lunch

  • @nicfranc Have you seen the Epic Citadel demo on iPhone / iPad? oooooooh shiny

  • @LisaMedcalf Dunno what's happening with the house until next week, wankers essentially want me to put a new roof on!

  • @LisaMedcalf Aww cool! I saw a girl on a moped who looked a bit like you and I thought a scooter would be a good idea. Cool, cheap, fun ...

Follow Me