Tuesday, 17 May 2011

Deploying an sharepoint event handler

We Can deploy a Event handler two method :

Using object model :
To deploy the event reciever using object model put the below code in console application and execute the console applicaton :


SPSite spSite = new SPSite("http://webapplicationurl");
                    SPWeb spWeb = spSite.OpenWeb();
                    objSPList = spWeb.Lists["ListName"];
                    objSPList.EventReceivers.Add(SPEventReceiverType.ItemAdded, "AsseblyFullyQualifiedName", "ClassName");
                    objSPList.Update();



Using Features :

To deploy our event receiver as a feature, Create a Feature Folder ("My Custom Receiver")in the Feature folder in  (Local_Drive:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURE

Put the below feature.xml and Elements.xml in "My Custom Receiver" folder. Deploy the written event Receiver in GAC.

Feature.xml :


  <Feature Scope="Web" Title="Event Handler Deployment"
   Description = "My Custom Event handler"
   Id="2366B8CB-1C6A-469c-9119-717A6vu68358"
   xmlns="http://schemas.microsoft.com/sharepoint/">
  <ElementManifests>
    <ElementManifest Location="Elements.xml"/>
  </ElementManifests>
</Feature>


Elements.xml :


</Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Receivers ListTemplateId="101">
      <Receiver>
        <Name>ItemAddedEvent</Name>
        <Type>ItemAdded</Type>
        <SequenceNumber>10000</SequenceNumber>
        <Assembly>EventAssemblyName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=23d656782aea270a</Assembly>
        <Class>EventAssemblyName.ClassName</Class>
        <Data></Data>
        <Filter></Filter>
</Receiver>
    </Receivers>
</Elements>

Receivers node parameters details :
  • Name – unique name for the receiver – you can set it upon your wish
  • Type – Type of event like adding/added/deleting/updating etc.
  • SequenceNumber – integer which determines in which order the event will be executed
  • Assembly – Name of the assembly deployed in the gac
  • Class – class which has the event handler implementation

Install and deploy the feature using below command from command prompt:

stsadm -o installfeature -filename "My Custom Receiver\Feature.xml"
stsadm -o activatefeature -filename "My Custom Receiver\Feature.xml" -url http://webapplicationurl
iisreset