Adding Event Receivers to all subsites of a certain type

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

namespace KMS_ER_ItemAdding_RS.FeatureCode
{

class KMS_ER_Itemadding_RS_Main : SPFeatureReceiver
{
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

using (SPSite siteCollection = (SPSite)properties.Feature.Parent)
{

using (SPWeb site = siteCollection.RootWeb)
{

foreach (SPWeb subsite in site.Webs)
{

if ((subsite.Name.ToString() == “systems”) || (subsite.Name.ToString() == “hosts”))
{

foreach (SPWeb newsubsite in subsite.Webs)
{
SPList plotList = newsubsite.Lists[“Related Systems”];
string assName = Assembly.GetExecutingAssembly().FullName;
plotList.EventReceivers.Add(SPEventReceiverType.ItemAdding, assName,
“KMS_ER_ItemAdding_RS.FeatureCode.KMS_ER_Itemadding_RS_Worker”);
newsubsite.Dispose();
}
}
subsite.Dispose();
}
}
}
}

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{

using (SPSite siteCollection = (SPSite)properties.Feature.Parent)
{
using (SPWeb site = siteCollection.RootWeb)
{
foreach (SPWeb subsite in site.Webs)
{
if ((subsite.Name.ToString() == “systems”) || (subsite.Name.ToString() == “hosts”))
{
foreach (SPWeb newsubsite in subsite.Webs)
{
SPList plotList = newsubsite.Lists[“Related Systems”];

foreach (SPEventReceiverDefinition item in plotList.EventReceivers)
{
if (item.Assembly == Assembly.GetExecutingAssembly().FullName)
{
item.Delete();
break;
}
}
newsubsite.Dispose();
}
}
subsite.Dispose();
}
}
}
}

public override void FeatureInstalled(SPFeatureReceiverProperties properties)
{
}

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

Comments are closed.