A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Thursday, November 9, 2006

Writing an event handler for a survey

Writing an event handler for a survey
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 2007 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 2007 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 2007 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 FilesMicrosoft Visual Studio 8SDKv2.0Bin
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.

No comments:

Post a Comment