Introduction

SharePoint Calendar Lists not only offer standard “1:00-2:30 pm” type entries, but also entries that span multiple days, and all-day items, which have no set start or end time. Retrieving this data means learning something about CAML queries and SharePoint gotchas – and I hope this article saves you some time.

Overview

The “sliding window” calendar web part shows data for 1- 7 contiguous days. The number of days displayed can be configured via the tool pane. If the web part is set to show 3 days, you’ll see today, tomorrow, and the day after that. For example, if today is Wednesday, you’ll see Wednesday-Thursday-Friday; on Thursday, the window “slides” to Thursday-Friday-Saturday. The goal is to display a lot of information in a small amount of space. To that end, only the title of each item is listed and hyperlinked - and when the mouse hovers over an item, a tooltip displays the start and end times, title, location, and description.

Gotcha: In CAML queries, you must use the name of the column, not the display name, and the real name can be hard to find, especially, if you renamed any columns. To find column names for any SharePoint list, open the list, edit the list settings, and scroll down until you find the list of columns.

Click on a column name to view its properties:

In the textbox at the right, the column name is “Start Time” - but that is the display name. The real column name is at the end of the URL(!). Look at the rightmost bit of the address bar to see “Field=EventDate”; and “EventDate” is the name we want.

Back to the CAML query. Recurring events … recur. For example, a seminar that occurs every week for 10 weeks, from 2 – 3 pm on Thursdays. These list items are created at runtime, not stored in the database (like an ordinary event), and a standard query will not return them. The DateRangesOverlap element must be used to find recurring events; it will return everything for the specified interval.

In this example, we’re choosing to look at one week at a time. Let’s take another look at that CAML query in an easier-to-read format:

In the FieldRef elements of the query (which use the real column name), EventDate is the start date, EndDate the end date, and RecurranceID is not only the ID you’ll need for the hyperlink, but contains the start day and the time of the recurring event. An example of a RecurranceID for a recurring event: “4.0.2008-05-27T17:00:00Z”. In contrast, an example of a RecurranceID for an ordinary event: “7”.

Set ExpandRecurrence to true so the recurring values get created. The CalendarDate property sets the actual date, which, combined with the CAML query’s “Week” value, will cause the query to grab all the entries in the week containing the “CalendarDate”.

Processing the list items collection

Now that we have the item collection, we need to figure out if each item is an all-day, multi-day, or ordinary item; check if the item’s start date is in our date range, and if so, add it to our sorted list, calendarCells.

If it is a multi-day item, there will be only one occurrence, and we’ll need to add the other days manually; but, there may be one each week if the time span bridges the weekend. This raises the possibility of accidentally adding the same items twice, so we’ll save the uniqueId property in the Hashtable, processedListItems, so we can screen for already-added items. Each “cell” in the sorted list, calendarCells, will be 1 hour of a day.

Last bits

You’ll notice that tooltips (or screentips) here are handled by assigning them to the title attribute of the <a>tags, and there are commented-out lines nearby assigning JavaScript functions to the onmouseover and onmouseout events. If you prefer to use JavaScript tooltips, just un-comment these lines, and also the script-registering code in the OnLoad method, and a few lines in the tip method. (Don’t forget to upload the JavaScript file to the web server.) With JavaScript tooltips, you can include images, links, etc., in the tooltips.

Once the sorted list is built, we’re ready to build a DataTable, add matching columns to a GridView, and set the GridView’s DataSource to the table. A naming convention is used to tie the DataTable columns to the GridView columns. The Gridview columns are template fields, so HTML can be used.

One of the challenges of web part design is that there is no design surface, so you really can’t “see” what you’re doing. I found it very helpful to preview the GridView in a separate project -a sort of workbench- where I could just throw things on a design surface, set properties, play with the code, and spin it up fast.

Share

About the Author

N W Brown has been programming/coding/developing for over 18 years, divided. About 8 years B.C. (before children), and 10+ years A.C. (after children finally went to high school and beyond). During the break (the "children" period), major changes took place in software development- like the introduction of the internet... Which just shows that the joy of playing with puzzles all day and getting paid for it never gets old!

When not coding, she is often a freelance flutist for events, musical theater or churches, or hikes one of the many local trails.

Comments and Discussions

I'm not sure I understand exactly what you want, but I'll try to help!In our environment, we have one SharePoint calendar on the portal, and the users connect to that calendar from their Outlook clients, and update it. This way, any number of users can contribute and the web part need look at only 1 calendar.

To have multiple web parts on 1 SharePoint page, linked to different calendars, you could edit the web part code, and insert the following lines so that the calendar URL is set in the property page and persisted in the viewstate, then edit the GetListData method to pick up the URL from these new properties. Hope that helps!

I suspect you need to add a reference to System.Configuration in your project. In Solution Explorer, right-click on "References" and choose "add reference". In the dialog box, choose the ".Net" tab, highlight System.Configuration and click "ok". Hope that helps!

Don't know if anyone else is having the same problem, but we discovered that all-day events for the current day were not appearing. The line in ItemIsInDateRange() that creates a DateTime object for the current day @ midnight was leaving a few ticks in place which was causing DateTimes with true midnight to appear outside the test date range.

I changed ItemIsInDateRange() to the following and it fixed the issue.

Your project provides a truly useful part (rather than just being demonstrative of some technique) that is made all the better by being wrapped in a concise, very informative article. SharePoint's calendar view is not particularly useful overall. The week-view is the most useful because it at least separates all-day events from those with start/stop times, but your sliding-window view is the most useful I've seen for bringing relevant data in a small area. I mean, you have seen the month-view on SharePoint? There's no way to use that beast without dedicating an entire page to it. As you might be able to tell, I have no affinity for the OOTB webpart, which makes this one all the more appreciated.

I've managed to develop a couple of special purpose parts in the six months since I've been working with SharePoint, and regarding the lack of a designer for parts (which is *really* ... inconvenient) I've had good luck working with the SmartPart for SharePoint[^], a part that hosts user controls that have been coded using the designer. I've also found Windows PowerShell[^] to be very useful for testing out methods for accessing data in SharePoint through the object model (see SharePoint PowerShell for Beginners[^]).

Thanks! I’ve gotten so much great information from CodeProject- I’m glad to have something to offer.

I love books, but find I usually read technical books in my spare time, to fill in the gaps left by learning-on-the-fly and gleaning from blogs and forums. I bought a copy of “Real World SharePoint 2007: Indispensable Experiences From 16 MOSS and WSS MVPs” and the couple bits I read were good, but I haven’t read much. I’m fairly new to Sharepoint- just started a few months ago.

My introduction to Sharepoint was a Mindsharp Developer’s class, which was fantastic. I also learned a lot from Sahil Malik’s video demonstrations on dnrtv , and I’ve picked up lots of tips from his website/blog. Most of the time I just search for articles / blogs / tutorials when I’m learning a new technology.