Event receiver for copying an event

In SharePoint 2013, we discovered a problem with copying events that have recurring properties or the All Day setting selected. Users could not copy the event to another site in a SharePoint workflow, even when using the activity to create a new item and then copy each column. A sandboxed event receiver overcame this problem. While the name of the destination list is hard-coded in this example, I would update this code to read the name of the destination list from a SharePoint list so that site managers can configure the event receiver with whatever list they want to copy to.

namespace MyNameSpace.RecurringEventAdded
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class RecurringEventAdded : SPItemEventReceiver
    {
        /// <summary>
        /// An item was added.
        /// </summary>
        public override void ItemAdded(SPItemEventProperties properties)
        {
            base.ItemAdded(properties);
            using (SPSite site = properties.OpenSite())
            { 
            using (SPWeb web = site.OpenWeb())
                {
                    var destinationWeb = "http://mysite.mydomain.com/sites/mysitecollection/mysubsite";

                    using (SPSite destSite = new SPSite(destinationWeb))
                    {
                        using (SPWeb destWeb = destSite.OpenWeb())
                        { 

                        var list = destWeb.Lists["MyCalendarList"];
                        var itemCopy = list.Items.Add();
                        itemCopy["Title"] = properties.ListItem["Title"];
                        itemCopy["RecurrenceData"] = properties.ListItem["RecurrenceData"];
                        itemCopy["EventType"] = properties.ListItem["EventType"];
                        itemCopy["EventDate"] = properties.ListItem["EventDate"];
                        itemCopy["EndDate"] = properties.ListItem["EndDate"];
                        itemCopy["UID"] = System.Guid.NewGuid();
                        itemCopy["TimeZone"] = 13;
                        itemCopy["Recurrence"] = -1;
                        itemCopy.Update();
                    }
                    }
            }
            }
        }

    }
}

Leave a Reply