Skip to main content

Writing an event handler for a survey report in MOSS

Make sure event handlers are enabled on the site where your survey is.

1. To enable event handlers for your site, go to "SharePoint Central Administration".





2. Select "Application Management" tab and then select "Web application settings" in "SharePoint Web Application Management" section.


3. Select your web application from the drop down. Click "Change Web Application" and then select your web application from the form that opens.

 

4. Scroll down and locate the "Event Handlers" section and turn them on.



5. Now go back to your site. You are ready to write an event handler for your survey. I will create an event handler just for the demo purposes. There will be only one question in our survey:

Is MOSS 2010 an excellent product?
Answer to this question is either "Yes" or "No". Type is "Choice" and options are "Yes" and "No". Choices should be displayed as radio buttons.

6. Start a new "Class library" project in VS and select C# as the programming language. Project will contain just a few lines of code. I just want to show you how one can capture events in a handler and how the syntax differs from the SPS 2003 code. Following is the complete code for the handler:

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Net.Mail;

namespace surveyhndlr
{
public class handler : SPItemEventReceiver
{
public override void ItemAdding(SPItemEventProperties properties)
{
SPList oSurvey = properties.OpenWeb().GetList("http://sp:81/SiteDirectory/site1/Lists/Survey2");
string userResponse =
oSurvey.Items[oSurvey.Items.Count - 1]["Is MOSS 2010 an excellent product?"].ToString();

string body = string.Empty;
if (userResponse == "Yes")
{
body = "Dear user, <BR> Thank you for voting in favor of MOSS 2007.";
}
else
{

body = "Dear user, <BR> Please install MOSS 2007 B2TR if you haven't already done so and then give it another try. B2TR will fix many issues that you are facing in Beta 2. ";

}
SmtpClient oSMTP = new SmtpClient();
oSMTP.Host = "mail.mysite.com";
oSMTP.Send("
share.point@yahoo.com","share.point@yahoo.com","Thanks for responding to the survey",body);
}


}
}

 
Don't forget to include "System.Net.Mail" namespace. This will be used in sending emails from the code. We are using ItemAdding event that will capture the user response before the response is actually submitted. Open your survey using the following line:

SPList oSurvey = properties.OpenWeb().GetList("http://sp:81/SiteDirectory/site1/Lists/Survey2");
 
The following line will return the user's response:

string userResponse =
oSurvey.Items[oSurvey.Items.Count - 1]["Is MOSS 2010 an excellent product?"].ToString();

It doesn't matter what the user response is. He can select "Yes" or "No". In both cases, an email will be sent to the user. We will just change the content of the mail, that's it!
Define an SMTP object. 

SmtpClient oSMTP = new SmtpClient();

You can change your mail server using the following line:

oSMTP.Host = "mail.mysite.com";

Following line will send the mail. 

oSMTP.Send("share.point@yahoo.com","share.point@yahoo.com","Thanks for responding to the survey",body);

I have hard coded the email addresses but you can get user's email address using the SPContext object. For example, you can use following code to get currently logged in user's email address:

SPContext ctx = new SPContext();
string useremail = ctx.Web.CurrentUser.Email;

7. Now, compile your code and strong name the assembly. Strong naming utility (sn.exe) can be found in the following folder:

C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin
Run the following command to strong name your assembly:
sn.exe -k key.snk
You can copy the sn.exe utility to the folder where your assembly is located. Resulting key will be written to the key.snk file.

8. Go to your project's properties (Right click project name in the solution explorer and select "Properties" from the menu) and select "Signing" from the menu that appears on the left. This will open a form.


Check "Sign the assembly" checkbox and choose the key file from the drop down (Click the "Browse..." button in the drop down to locate the key.snk file that you generated in the previous step).

9. Re-compile your assembly.

10. Place your assembly in the GAC. We will use ".NET Framework 2.0 Configuration" application, although there are other ways of adding the assembly to the GAC. You can use gacutil.exe tool as well. Anyway, Go to control panel, click "Administrative Tools" and select "Microsoft .NET Framework 2.0 Configuration". 

11. Click "Manage the Assembly Cache" and then select "Add an Assembly to the Assembly Cache". Locate the assembly and click "Open". 

12. Read the "Public Key Token" from the Assembly Cache. Select your newly added assembly in the assemblies list and note down the public key token that appears as the last column.

13. Your event handler has been placed in the GAC. Now it is the time to bind the event handler to the list. Unlike SPS 2003 where you could use interface to bind handlers, you will have to write few more lines of code because in MOSS 2007, you have to bind the event handlers programmatically. Create a new Windows Application in Visual Studio and paste following lines in the form's Load() event:

SPSite site = new SPSite("http://sp:81/SiteDirectory/site1");
SPWeb web = site.OpenWeb();
SPList survey = web.Lists["Survey2"];

string assemblyName = "surveyhndlr, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a34245adab33a20a";
string className = "surveyhndlr.handler";

survey.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);
MessageBox.Show("Done");

The public key token you noted down earlier will be used in this code. Define a variable named "assemblyName" of string type and include assembly's name, version, culture and publickeytoken. Define another variable called as "className" of string type and use the following line to add your handler to the EventReceivers list:

survey.EventReceivers.Add(SPEventReceiverType.ItemAdding, assemblyName, className);

14. That's all! Open the survey and respond to the question. Select "Yes" or "No", an email will be sent to the user, in this case, to you with a message specific to the user's response. You can do many things using event handlers. This is an excellent way of extending the "out of the box" functionality. In SPS 2003, you could use event handlers with document libraries only but in MOSS 2007, you can use event handlers with all lists. Users now have more power in their hands.

Comments

Post a Comment

Popular posts from this blog

Tab Control in Asp.Net

Scenerio: I need your help in designing tab control in   asp.net .My requirement is I need a tab control in   asp.net (C#) like  for example goto my computer ,right click c drive and select properties.we get tabs like general,security etc....... like that i need to design one tab control(no need of any rightclick) in my web page and the database table columns should come as tabs and inseide the tab data of that particular column should come.   Example:Employee master tab1:Employee name.........his name in the tab tab2:Age.............his age tab3:Address........his address   Solution:   You can do this using a simple div <style type="text/css"> .tabs         {             position: relative;             height: 20px;             margin: 0;   ...

AI and Microsoft: Revolutionizing Efficiency in Nonprofit Organizations

  How AI and Microsoft Enhance Efficiency in Nonprofit Organizations In today’s fast-paced world, nonprofit organizations face unique challenges—limited resources, increasing demands, and the constant need to do more with less. But what if technology could be the game-changer they need? In my latest research paper,  "How AI and Microsoft Enhance Efficiency in Nonprofit Organizations" , I explore how cutting-edge technologies like Artificial Intelligence (AI) and Microsoft’s innovative tools are revolutionizing the way nonprofits operate. From streamlining administrative tasks to enhancing donor engagement and optimizing resource allocation, AI and Microsoft’s solutions are empowering nonprofits to focus on what truly matters—their mission. This paper dives deep into real-world examples, practical applications, and the transformative potential of these technologies. Whether you’re a nonprofit professional, a tech enthusiast, or simply curious about the intersection of technolo...

Social tagging overview in Sharepoint 2010

A tag is a word or phrase that identifies an individual piece of information according to a set of attributes or criteria. Tags make it easy to find and share information about a specific subject or task. Social tagging helps users categorize information in ways that are meaningful to them. Social tagging can improve the quality of search results by filtering against specific tags, and it can also connect individuals who want to share information with other users who have like interests. This article describes the social tagging features in Microsoft SharePoint Server 2010. This article does not describe how to configure social tagging features. It also does not discuss how to implement social tagging features as part of an overall social media strategy for an enterprise. About using social tagging features Social tagging features help users to share information and to retrieve relevant, high-quality content more efficiently. Such sharing encourages collaboration and b...