Build SharePoint List Event Receiver

I would like to show you to develop simple SharePoint List Event Receiver.
We could use same Project list and Project Report library that I’ve created from the previous post.

  1. Open Microsoft Visual Studio 2010 > File > New > Project > Empty SharePoint Project.
  2. Enter the Project Name, Location and Solution Name.
  3. Select Deploy as farm solution and click OK.
  4. Right click on the project and select Add > New Item.
  5. Select Event Receiver item and enter Event Receiver’s Name.
  6. In SharePoint Customization Wizard window:
    EventReceiver1
  7. My final solution will look like below.
    EventReceiver2
  8. Open EventReceiverProjectReport.cs and update the code to:
    using System;
    using Microsoft.SharePoint;
    
    namespace ProjectReportEventReceiver.EventReceiverProjectReport
    {
        ///
        /// List Item Events
        ///
        public class EventReceiverProjectReport : SPItemEventReceiver
        {
            ///
            /// An item was added.
            ///
            public override void ItemAdded(SPItemEventProperties properties)
            {
                base.ItemAdded(properties);
                if (properties.List != null && properties.List.Title.ToLower().Trim() == "project reports")
                {
                    SPListItem oItem = properties.ListItem;
                    oItem["ReportComment"] = String.Format("Created by Event Receiver on {0}", DateTime.Now.ToString("dd/MM/yyyy"));
                    oItem.Update();
                }
            }
    
            ///
            /// An item was updated.
            ///
            public override void ItemUpdated(SPItemEventProperties properties)
            {
                base.ItemUpdated(properties);
                if (properties.List != null && properties.List.Title.ToLower().Trim() == "project reports")
                {
                    SPListItem oItem = properties.ListItem;
                    oItem["ReportComment"] = String.Format("Updated by Event Receiver on {0}", DateTime.Now.ToString("dd/MM/yyyy"));
                    oItem.Update();
                }
            }
        }
    }
    
  9. Build and Deploy the project.

Now its time to test it.

  1. Upload document to Project Reports library.
    TestResult1
  2. ReportComment column will be set by event receiver automatically.
    EventReceiver3
Advertisements

Tagged: , , , ,

One thought on “Build SharePoint List Event Receiver

  1. […] my previous article to build SharePoint list event receiver and custom workflow in SharePoint Designer, I start questioning myself […]

Leave a Reply

Please log in using one of these methods to post your comment:

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

%d bloggers like this: