Tuesday, 9 December 2008

How to Create a Simple Event Handler when a Feature is activated

If you want to bind an event handler with a simple feature all you need to do is tweak your Feature.xml and add the Assembly Information into that and Create a Simple Event handler
Feature.xml File
Feature Id="ec4b9423-46ff-4a61-9264-8761e7c17145"
Title="FeatureWithReceiver1"
Description="Description for FeatureWithReceiver1"
Version="1.0.0.0"
Hidden="FALSE"
Scope="Site"
DefaultResourceFile="core"
ReceiverAssembly="SimpleEventHandlerPro, Version=1.0.0.0, Culture=neutral, PublicKeyToken=$PublicKeyToken$"
ReceiverClass="SimpleEventHandlerPro.FeatureWithReceiver1"
xmlns="http://schemas.microsoft.com/sharepoint/">
Creating a Simple Event Handler
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SimpleEventHandlerPro
{
class FeatureWithReceiver1 : SPFeatureReceiver
{

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{

SPSite rootsite = new SPSite("http://js-upsideserver");
SPWeb mywebsite = rootsite.OpenWeb();
mywebsite.Title = "JJ SITE Changed";

foreach (SPList list in mywebsite.Lists)
{
list.EnableFolderCreation = false;

list.Update();
}

mywebsite.Update();

}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{

}
public override void FeatureActivated (SPFeatureReceiverProperties properties)
{
}
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{

}
}