Monday, 10 October 2011

List all event Receivers attached with a list using SharePoint object model


Last week I had an issue with event recivers, where VS was throwing errors while deploying one of my event recivers saying that the feature already exists. I completely removed the receiver but the error didn't go away. I also checked that there were no files in Solution and Features directory and no receiver running on the list.

So the only way to get all the receivers attached to our list was by creating a console program that would list out all the associated event receivers.

Here is a qucik program that would list out all the receivers associated with a list -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace EventViewer
{
class Program
{
string siteURL = "http://SPSite";
string listName = "Custom List";

static void Main(string[] args)
{
Program p = new Program();
p.PrintAllEvRecv();
}

//Prints all the Event Receivers attached to a list

public void PrintAllEvRecv()
{


using (SPSite site = new SPSite(siteURL))
{

using (SPWeb web = site.OpenWeb())
{

SPList list = web.Lists[listName];

int i = list.EventReceivers.Count;

SPEventReceiverDefinitionCollection eventReceivers = list.EventReceivers;

foreach (SPEventReceiverDefinition ev in eventReceivers)
{

Console.WriteLine("Type=" + ev.Type.ToString() + "\t Class=" + ev.Class.ToString()+"\t Name: " + ev.Name );
Console.WriteLine(" ");
}
}
}}}
}}

Ads by Google

No comments:

Post a Comment