Implement event in calendar using EventReceiver

August 26th, 2009 | Categories: Customization, SharePoint, Solutions | Tags:

Event in SharePoint, this sentence is often play in our mind while thinking to implement custom solutions. What’s the way we can implement event. How to implement event and where it basically resides. How those events have been called in SharePoint.

SharePoint treat event in two different ways. One is asynchronous, and the other is synchronous event. We can implement event through EventReceiver template through VSeWSS in SharePoint.
Create a new empty SharPoint project. Add the reference Microsoft.SharePoint DLL. Build the TEMPLATE, and FEATURES document structure under the project.

Create a new empty SharPoint project. Add the reference Microsoft.SharePoint DLL. Build the TEMPLATE, and FEATURES document structure under the project.

add-sharepoint-dll

add-sharepoint-dll

Right click, and select add new item. Add New Item window will be appear, select SharePoint object in left nav bar. On the right hand site, you will found different template, select Event Receiver template.

 

It will ask you for which object you want to create EventReceiver. Here you need to select the document library/ list for which you want to implement event handling mechanism. Select Calendar.

 

Once you selected the list and click on OK, the associated form will be generated. ItemEventReceiver.cs file is the file where you can implement your event handler implementation. The XML file associated with ItemEventReceiver is ItemEventReceiver.xml.

You can implement your custom event handling mechanism inside any of the methods like ItemAdded(), , ItemAdding(), ItemAttachmentAdded(), ItemAttachmentAdding(), ItemDeleted(), ItemDeleting() etc. The parameters for any of these methods is SPItemEventProperties.

 

In the following example, you will find a very simple event receiver implementation for Calendar object. When someone added a new entry in the calendar, the following code will be executed. This is asynchronous event. Get the instances of the current list item i.e. SPItem through properties.ListItem.  And when the event will be fired, you need to take care about calling the two methods this.EnableEventFiring(), and  base.ItemAdded(properties).
public override void ItemAdded(SPItemEventProperties properties)
        {
            StringBuilder SBEventDetails = new StringBuilder();

            try
            {
                SPItem AddedItem = properties.ListItem;
                SBEventDetails.Append(“Title: ” + AddedItem["Title"].ToString());
                SBEventDetails.AppendLine();
                SBEventDetails.Append(“Location: ” + AddedItem["Location"].ToString());
                SBEventDetails.AppendLine();
                SBEventDetails.Append(“Start Time: ” + AddedItem["Start Time"].ToString());
                SBEventDetails.AppendLine();
                SBEventDetails.Append(“End Time: ” + AddedItem["End Time"].ToString());
                SBEventDetails.AppendLine();
                SBEventDetails.Append(“Description: ” + AddedItem["Description"].ToString());
                AddedItem.Update();
                this.EnableEventFiring();

                base.ItemAdded(properties);

            }
            catch (Exception ex)
            {
                properties.Cancel = true;
                properties.ErrorMessage = ex.Message;
                SBEventDetails.Append(ex.Message.ToString());
            }
            finally
            {
                //add the details into a text file at the server
                StreamWriter _SW = new StreamWriter(“C:\\EventCalendarUpdate.txt”);
                _SW.WriteLine(“[" + DateTime.Now + "]“);
                _SW.WriteLine(SBEventDetails);
                _SW.Flush();
                _SW.Close();
            }
        }

Deletion event in this article is also an asynchronous event. Here the methods name is ItemDeleted(). The line properties.Cancel = false; indicated that this is not a cancel event. Next line, i.e. DeletedItem.Update() update the calendar object. In other words it is executing the deletion process.

public override void ItemDeleted(SPItemEventProperties properties)
        {
            StreamWriter _SW = new StreamWriter(“C:\\CalendarDeleteRecord.txt”);
            _SW.WriteLine(“[" + DateTime.Now + "]“);

            SPItem DeletedItem = properties.ListItem;
            try
            {
                properties.ErrorMessage = DeletedItem.Fields["ID"].ToString() + ” has been deleted.”;
                properties.Cancel = false;
                DeletedItem.Update();
                this.EnableEventFiring();
            }
            catch (Exception ex)
            {
                properties.Cancel = true;
                properties.ErrorMessage = ex.Message;
            }
            finally
            {
                _SW.WriteLine(properties.ErrorMessage.ToString());
                _SW.Flush();
                _SW.Close();
            }
           
        }

The file ItemEventReceiver.xml contains the meta data information about this event. Contains the name of the assembly, namespace, its culture information, and build version.


It also contains the detailed information about Receivers class and associated implemented event handles details.

Build the project successfully and deploy the solution into SharePoint. After deploying there will be one entry in the feature folder in the 12 hive folder structure i.e. <DRIVE>\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES\ and two xml files feature.xml and ItemEventReceiver.xml will be created there. This files contains all the meta info and details on this feature and responsible to handling the event mechanism. 

  1. July 21st, 2010 at 14:36
    Reply | Quote | #1

    I’m trying to open forum but sometimes there are no images on it :(

  2. September 3rd, 2010 at 21:52
    Reply | Quote | #2

    Sorry admin – my post is test