views:

292

answers:

1

Hi all,

I had an event list. I created a new item that has recurrence daily ( Start Time : 1/5/2010 12 : 00 AM and End Time : 5/30/2010 12:00 AM). I want to delete the item which has Start Time : 5/12/2010 12:00 AM but my application throwed exception.

My code as below :

   DateTime eventDate = DateTime.Parse(list.Fields.GetFieldByInternalName("EventDate").GetFieldValueAsHtml(DateTime.Parse(this.DateTimeOfItem).ToUniversalTime()));
                            SPQuery pQuery = new SPQuery();
                            pQuery.ExpandRecurrence = true;
                            pQuery.CalendarDate = eventDate.AddDays(-1);
                            pQuery.Query = string.Format("<OrderBy><FieldRef Name=\"EventDate\"/></OrderBy><Where><And><DateRangesOverlap><FieldRef Name=\"EventDate\" /><FieldRef Name=\"EndDate\" /><FieldRef Name=\"RecurrenceID\" /><Value Type=\"DateTime\"><Week /></Value></DateRangesOverlap><Eq><FieldRef Name=\"ID\" /><Value Type=\"Counter\">{0}</Value></Eq></And></Where>", this.ID);
                            SPListItemCollection itemColl = list.GetItems(pQuery);
                            int index = 0;
                            while (index < itemColl.Count)
                            {
                                SPListItem item = itemColl[index];
                                if (DateTime.Parse(item["EventDate"].ToString()).CompareTo(eventDate) == 0)
                                {
                                    web.AllowUnsafeUpdates = true;
                                    item["UID"] = Guid.NewGuid().ToString();
                                    item["EventType"] = 3;
                                    item["RecurrenceID"] = eventDate;
                                    item["MasterSeriesItemID"] = this.ID;
                                    item["XMLTZone"] = null;
                                    item["RecurrenceData"] = "Every 1 day(s)";
                                    item.Update();
                                    list.Update();
                                    web.AllowUnsafeUpdates = false;
                                    break;
                                }
                                index++;
                            }

I do not know why I can not update this item. Please help me.

Thanks

PD.

+1  A: 

To delete an instance of a recurring event in SharePoint you have to actually add a NEW record and mark it as deleted.

To understand recurring events in SharePoint and keep your sanity you need to write a little utility to output the whole list (every field) to learn how they work. The CAML query is expanding out the recurring event into 'fake' instances and these can't be updated.

When you setup a recurring event you only add 1 record into the list no matter how many instances it has. This is the 'master record' and has the recurrence pattern in the RecurrenceDate field.

When you add an exception (e.g. either the instance on "1/5/2010 12 : 00 AM" has been deleted or moved to another date) then this is another NEW record.

In this exception record you have the following fields of interest

  • MasterSeriesItemID - The ID of the master recurrance record
  • EventType - 3 for a modified instance, 4 for a deleted instance
  • RecurrenceID - The datetime of the instance that this execption replaces

It refernces the orgional recurrance record

This is about the best reference around for Recurring Events.

Understanding the SharePoint calendar and how to export it to iCal format

Be aware that in SharePoint 2007 there are some pretty crazy bugs converting to/from UTC to site time in the RecurranceID field when you've got 'All Day Events' (either the master record or the exception)

Ryan
Thanks Ryan so much !
Phong Dang