Pages

Wednesday, November 17, 2010

Deploying Event Receivers

This can be done in either below three ways

  1. Deploy programmatically through code
  2. Using STSADM command ( SharePoint Feature)
  3. Using content types

Let’s see how to deploy using code. This method is not suitable when you are working in Development life cycle. Best practice is to create a Feature and deploy that to your production server and you are good. But for illustration purpose lets see the code way!!!

In my previous post we created simple Event Log event receiver which logs the user entries in a text file. Lets deploy the same Event Receiver.

Console Application for Deploying Event Receiver:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.SharePoint;

namespace DeployEventLog

{

class Program

{

static void Main(string[] args)

{

SPSite spsite = new SPSite("http://SharePointForFun:555/");

SPWeb mysite = spsite.OpenWeb();

SPList theList = mysite.Lists["EventLog"];

SPEventReceiverDefinition newReceiver = theList.EventReceivers.Add();

newReceiver.Class = "EventLog.LogEvent ";

newReceiver.Assembly = "EventLog",

Version= 1.0.0.0,Culture=neutral,PublicKeyToken=234c6d4671b2a894";

newReceiver.SequenceNumber = 5055;

newReceiver.Type = SPEventReceiverType.ItemAdded;

newReceiver.Update();

Console.Read();

}

}

}

That's it. Once you run this console application Event gets deployed to the SharePoint server defined in the SPSite object. Have Fun with SharePoint.

No comments:

Post a Comment