Search This Blog

Wednesday, October 6, 2010

Develop a SharePoint 2007 Event Handler (Part 2)

This is Part 2 of a multi-part article.

For Part 1 see this link - Part 1
For Part 3 see this link - Part 3
For part 4 see this link - Part 4

1. The next step is to start developing the custom event handler. For our development we will be using Visual Studio 2008 with WSPBuilder installed.

2. To create the Project follow these steps:

a. Open Visual Studio 2008

b. Create a new project of the type WSPBuilder Project, name it EmailEventHandlers

c. In Visual Studio, Right click on the project name and from the context menu choose Add -> New Item

d. From the pop-up dialogue box click on the WSPBuilder category in the left pane and choose the Event Handler Template in the right pane.

e. Name the event handler “EmailEventHandler”

f. A new pop-up menu will appear prompting you to add a Title, Description, and Scope, we will add the following information:

• Title: EmailEventHandler

• Description: A custom event handler for the e-mail received event.

• Scope: Web (Web Site or Sub Site)

g. WSPBuilder will create the correct 12-structure in the solution; it will also automatically add a reference to "Microsoft.SharePoint.dll" which is required by the event handler.

h. WSPBuilder will also automatically create the feature.xml and elements.xml with some of the tags pre-populated. We need to make a few corrections to the tags in the elements file:

• Name: EmailEventHandlers

• Type: EmailReceived

• Sequence: 20000

i. WSPBuilder also adds a class template as a starting point, and the class has the following using statements:

• using System;

• using System.Collections.Generic;

• using System.Text;

• using Microsoft.SharePoint;

j. WSPBuilder also adds some method stubs, but these are all related to the Item Add and Item Update events so we will not use them.

k. At this point, and due largely to WSPBuilder, all the initial work of project setup is already completed, and we can now proceed to the next step which is adding the remaining code framework.

3. To add the remaining code framework follow these steps:

a. Add the following using statements:

using System.Text.RegularExpressions;

using Microsoft.SharePoint.Utilities;

using Microsoft.SharePoint.Workflow;

b. Change the class inheritance from SPItemEventReceiver to SPEmailEventReceiver

c. Inside the class brackets delete the existing method signatures and add this method signature in their place:

public override void EmailReceived(SPList List, SPEmailMessage Message, string strReceiverData){ }

d. Your code so far should look as follows:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace EmailEventHandlers
{
     class EmailEventHandler : SPEmailEventReceiver
    {
        public override void EmailReceived(SPList List, SPEmailMessage Message, string strReceiverData)
       {
       }
    }
}

4. Now run build on the project, correct any errors if they occur (and hopefully they won’t) and you are now ready to begin the actual development, which will be the topic of my next Blog.

And that's all there is to it!

1 comment:

Zachery Hudson said...

Your tutorials are really making my job done to handle the event handler. Before this also it had made me learn to know more about SharePoint.