WSS Practice: Create a Calendar Event

Another thing on the WSS syllabus is “Add a recurring event to a calendar”. Well, I gave it a go, and failed. It was actually pretty tough. In the end, I found myself referring to ‘How to: Add a Recurring Event to Lists on Multiple Sites‘. It’s a pretty good article, and talks about creating a Meeting Workspace and attaching it too, but the short version of it is my code…

SPList cal = site.Lists["Calendar"];
SPListItem calEvent = cal.Items.Add();
calEvent["Title"] = "Frequent Event";
string recurrence = "<recurrence><rule>" +
"<firstDayOfWeek>su</firstDayOfWeek>" +
"<repeat><daily dayFrequency='2'/></repeat>" +
"<windowEnd>2010-09-20T09:00:00Z</windowEnd>" +
"</rule></recurrence>";
calEvent["RecurrenceData"] = recurrence;
calEvent["EventType"] = 1;
calEvent["EventDate"] = new DateTime(2009, 1, 26, 8, 0, 0);
calEvent["EndDate"] = new DateTime(2009, 1, 26, 9, 0, 0);
calEvent["UID"] = System.Guid.NewGuid();
calEvent["TimeZone"] = 13;
calEvent["Recurrence"] = -1;

One tricky bit of this would be the CAML for the recurrence; this is described fairly well in the article linked to above. Another issue would be simply knowing the correct columns to populate! Still, the code created an event that recurs every two days…

Advertisements
WSS Practice: Create a Calendar Event

4 thoughts on “WSS Practice: Create a Calendar Event

  1. sandeep says:

    Can you show how to change the above recurring event to “no end date”. I am having problem with it.

    Thanks

    Like

  2. sarav says:

    Am trying to set the event date @ run time but its not working. With the hard coded vlaue as in your example it works. Is there any reason why you cannot assign the
    calEvent[“EventDate”] = new DateTime(2009, 1, 26, 8, 0, 0); @ runtime

    calEvent[“EventDate”] = new DateTime(nYear, nMonth, nDay, nHour,nMin ,nSec);

    Like

  3. What leaps to mind is, are you sure that your date/time values are for a valid DateTime? No 32nd days, or 13th months?

    Otherwise, no, I’ve no idea why that wouldn’t work.

    Like

Leave a Reply

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 / Change )

Twitter picture

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

Facebook photo

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

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s