You can register your custom MOSS event handlers using a few techniques. I have always been a fan of a simple Console application which will do the trick. The pre-requisites are:
1) Ensure that the assembly implementing the custom event handlers in strongly typed (i.e., should be signed with your private key - Click here to learn how to do this...)
2) Ensure that your custom event handlers strongly typed assembly has been registered in the GAC of the server (or on each server on a web farm).
Once you have checked the pre-requisites you can write a Console application to do your job. An example is as below:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace RagavWebsite.Website.RegisterEventHandlers
{
// Console application to register custom event handlers on a specific list called "Comments" on the website
class Program
{
static void Main(string[] args)
{
SPSite curSite = new SPSite("http://RagavWebsite/sites/Vowel/RagavBlog");
SPWeb curWeb = curSite.OpenWeb();
SPList commentsList = curWeb.Lists["Comments"];
string asmName = "RagavWebsite.WebSite.EventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=682fb345e6f432a";
string className = "RagavWebsite.WebSite.EventHandler.RagavWebsiteVowelCheckItemHandler";
commentsList.EventReceivers.Add(SPEventReceiverType.ItemAdding, asmName, className);
commentsList.EventReceivers.Add(SPEventReceiverType.ItemUpdating, asmName, className);
}
}
}
Showing posts with label MOSS 2007 Event Handlers. Show all posts
Showing posts with label MOSS 2007 Event Handlers. Show all posts
Saturday, April 21, 2007
ItemAdded event handler vs. ItemAdding event handler
I often get asked this question when should i use the ItemAdded / ItemAdding event handler in MOSS 2007. Well the answer is really simple. If you are adding an item to a list and want to perform a set of actions before the item is saved, including preventing the item from being saved or throw an exception, "ItemAdding" is the event handler to override. If you want to perform post add actions then "ItemAdded" in the event handler to override.
Also note that in your event handler code, you would need to use the "AfterProperties" bag to read the list field values that have changed (as opposed to the "Properties" bag). For example:
private void VowelCheck(SPItemEventProperties properties)
{
string vowelErrorMessage = string.Empty;
// Check whether the Title field exists within the List
if (properties.AfterProperties["Title"] != null)
{
// Assumption that you have a class called VowelListManager.cs which has a function CheckVowelExists()
vowelErrorMessage = VowelListManager.CheckVowelExists(properties.AfterProperties["Title"].ToString());
}
// If Error Message is returned then the current words has failed the vowel test- This code also shows how to cancel a SharePoint event (like stop the item from being added or updated)
if (!vowelErrorMessage.Equals(string.Empty))
{
properties.ErrorMessage = vowelErrorMessage;
properties.Cancel = true;
}
}
Also note that in your event handler code, you would need to use the "AfterProperties" bag to read the list field values that have changed (as opposed to the "Properties" bag). For example:
private void VowelCheck(SPItemEventProperties properties)
{
string vowelErrorMessage = string.Empty;
// Check whether the Title field exists within the List
if (properties.AfterProperties["Title"] != null)
{
// Assumption that you have a class called VowelListManager.cs which has a function CheckVowelExists()
vowelErrorMessage = VowelListManager.CheckVowelExists(properties.AfterProperties["Title"].ToString());
}
// If Error Message is returned then the current words has failed the vowel test- This code also shows how to cancel a SharePoint event (like stop the item from being added or updated)
if (!vowelErrorMessage.Equals(string.Empty))
{
properties.ErrorMessage = vowelErrorMessage;
properties.Cancel = true;
}
}
Subscribe to:
Posts (Atom)