1

When a user add a new recurrence event to a calendar I want to check if any of the recurrences overlap with events already in the calendar. I saw the property Recurrences Data and its XML but I didn't find a way to translate that value to dates I can test against the events dates on the calendar. So, is there a way to expand the recurrences of an event in the ItemAdding event receiver of a calendar?

Edit

I manage to get all recurrencies for the item in the ItemAdded event. This is the code I'm using (some code omitted for brevity):

                DateTime _dtAux = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                query.ExpandRecurrence = true;

                // Get all events (and all recurrencies)
                query.Query = String.Format(@"<Where>
                                <DateRangesOverlap>
                                    <FieldRef Name='EventDate' />;
                                    <FieldRef Name='EndDate' />
                                    <FieldRef Name='RecurrenceID' />
                                    <Value Type='DateTime'><Year /></Value>
                                </DateRangesOverlap>
                            </Where>");

                query.CalendarDate = _dtAux;

                SPListItemCollection existing_events = calendar.GetItems(query);

                // Loop trough all the recurrencies of the item added (where clause)
                foreach (var itemAdded in existing_events.Cast<SPListItem>().Where(x => Convert.ToInt64(x["ID"]) == properties.ListItemId))
                {
                    if (properties.Cancel)
                    {
                        break;
                    }


                    DateTime _itemAddedStart = DateTime.Parse(itemAdded["EventDate"].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
                    DateTime _itemAddedEnd = DateTime.Parse(itemAdded["EndDate"].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);

                    // Loop trough all the itens in the calendar except the item added (where clause)
                    foreach (var item in existing_events.Cast<SPListItem>().Where(x => Convert.ToInt64(x["ID"]) != properties.ListItemId))
                    {
                        DateTime _itemStart = DateTime.Parse(item["EventDate"].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);
                        DateTime _itemEnd = DateTime.Parse(item["EndDate"].ToString(), null, System.Globalization.DateTimeStyles.RoundtripKind);

                        // Verify if the dates overlap
                        if (_itemAddedStart < _itemEnd && _itemStart < _itemAddedEnd)
                        {
                            properties.Cancel = true;

                            properties.ErrorMessage += "Não foi possível salvar o agendamento, pois um agendamento já foi feito nesta mesma data. <BR><BR>";

                            properties.ErrorMessage += String.Format("Evento: {0} <BR> Hora de Início: {1} <BR> Hora de Término: {2} <BR><BR>", item["Title"], item["EventDate"], item["EndDate"]);

                            properties.ErrorMessage += "Por favor, volte e escolha uma nova data para o agendamento.";

                            // Delete List item
                            SPListItem itemToDelete = calendar.GetItemById(properties.ListItemId);
                            itemToDelete.Delete();

                            break;
                        }
                    }
                }

Now if I delete the item it continues to appear in the calendar and the properties.ErrorMessage doesn't display. Just after a refresh on the page it goes away (still no message).

Any ideas on how to make the item vanish after beign deleted?

1

According to this article:

You don’t need to parse this XML yourself to get the actual instances of the recurring event. Instead, you can use the SharePoint object model to expand recurring events during a given month:

It is a very large article, well detailed. So I pasted the link. Hope it will be helpfully.

  • The problem is I'm adding the item to the calendar. It doesn't exists yet, so I can't query it. But thank you! – erickalves05 Mar 9 '15 at 14:34
  • Can you use ItemAdded instead of ItemAdding? – Juan Pablo Pussacq Laborde Mar 9 '15 at 14:44
  • Yes. I'm trying it right now. I wrote this logic in the ItemAdded event: 1- get only the item added (and all recurrencies). 2- get all items from calendar (and all recurrencies) except the item added. 3- comparare the dates and time for each one. The only thing is I'm not able to determine if a item is recurrent or not. I will keep trying and post the anwser as soon as I can. – erickalves05 Mar 9 '15 at 17:32
  • Yo can try: if (Convert.ToBoolean(item["Recurrence"])) – Juan Pablo Pussacq Laborde Mar 9 '15 at 18:55
  • This gave me a null reference. I'm using the 'EventType' property. Some say that's an enum, or just a number like this article hungryminds10.blogspot.com.br/2011/12/…. But I'm using a string comparison with "Event_Recurring". So far so good. – erickalves05 Mar 9 '15 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.