Created
July 23, 2015 18:01
-
-
Save skaggej/d8e47d0a64ec36287eaa to your computer and use it in GitHub Desktop.
SharePoint 2007 - Issue Archiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using Microsoft.SharePoint; | |
using System.Diagnostics; | |
namespace EricSFeatures | |
{ | |
public class IssuesArchiverFeatureReceiver : SPFeatureReceiver | |
{ | |
public override void FeatureActivated(SPFeatureReceiverProperties properties) | |
{ | |
SPWeb site = null; | |
try | |
{ | |
site = (SPWeb)properties.Feature.Parent; | |
if (site.Title == "CPS") | |
{ | |
//Adds the overridden ItemUpdated Event Receiver to the Issues list | |
SPList lstIssues = site.Lists["Issues"]; | |
string IssuesasmName = "EricSFeatures, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1dbe881e5c1ff48"; | |
string IssuesitemReceiverName = "EricSFeatures.IssuesItemEventReceiver"; | |
lstIssues.EventReceivers.Add(SPEventReceiverType.ItemUpdated, IssuesasmName, IssuesitemReceiverName); | |
//Adds the overridden ItemUpdated Event Receiver to the Issues Archive list | |
SPList lstArchive = site.Lists["Issues Archive"]; | |
string ArchiveasmName = "EricSFeatures, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1dbe881e5c1ff48"; | |
string ArchiveitemReceiverName = "EricSFeatures.IssuesArchiveItemEventReceiver"; | |
lstArchive.EventReceivers.Add(SPEventReceiverType.ItemUpdated, ArchiveasmName, ArchiveitemReceiverName); | |
} | |
} | |
catch (Exception e) | |
{ | |
string sSource; | |
string sLog; | |
string sEvent; | |
sSource = "IssueArchiver"; | |
sLog = "Application"; | |
sEvent = e.ToString() + ": IssueArchiver encountered an error while trying to Add an Event Receiver to a list. Please ensure that the 'Issues' and 'Issues Archive' lists exist within the site in which the IssueArchiver feature was activated."; | |
if (!EventLog.SourceExists(sSource)) | |
{ | |
EventLog.CreateEventSource(sSource, sLog); | |
} | |
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 888); | |
} | |
finally | |
{ | |
if (site != null) | |
{ | |
site.Dispose(); | |
} | |
} | |
} | |
public override void FeatureDeactivating(SPFeatureReceiverProperties properties) | |
{ | |
//Remove the event receivers from their corresponding lists when this feature is deactivated | |
SPWeb site = null; | |
try | |
{ | |
site = (SPWeb)properties.Feature.Parent; | |
//Removes the overridden ItemUpdated Event Receiver from the Issues list | |
SPList lstIssues = site.Lists["Issues"]; | |
SPEventReceiverDefinitionCollection Issueserdc = lstIssues.EventReceivers; | |
SPEventReceiverDefinition Issueserd = null; | |
string IssuesasmName = "EricSFeatures, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1dbe881e5c1ff48"; | |
//Cycle through the Event Receivers in the Event Receiver Collection for the Issues list and delete the IssueArchiver if it's found | |
for (int i = 0; i < Issueserdc.Count; i++) | |
{ | |
Issueserd = Issueserdc[i]; | |
if (Issueserd.Assembly == IssuesasmName && Issueserd.Type == SPEventReceiverType.ItemUpdated) | |
{ | |
Issueserd.Delete(); | |
} | |
} | |
//Removes the overridden ItemUpdated Event Receiver from the Issues Archive list | |
SPList lstArchive = site.Lists["Issues Archive"]; | |
SPEventReceiverDefinitionCollection Archiveerdc = lstArchive.EventReceivers; | |
SPEventReceiverDefinition Archiveerd = null; | |
string asmName = "EricSFeatures, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a1dbe881e5c1ff48"; | |
//Cycle through the Event Receivers in the Event Receiver Collection for the Issues Archive list and delete the IssueArchiver if it's found | |
for (int i = 0; i < Archiveerdc.Count; i++) | |
{ | |
Archiveerd = Archiveerdc[i]; | |
if (Archiveerd.Assembly == asmName && Archiveerd.Type == SPEventReceiverType.ItemUpdated) | |
{ | |
Archiveerd.Delete(); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
string sSource; | |
string sLog; | |
string sEvent; | |
sSource = "IssueArchiver"; | |
sLog = "Application"; | |
sEvent = e.ToString() + ": IssueArchiver encountered an error while trying to Remove an Event Receiver from a list. Please ensure that the 'Issues' and 'Issues Archive' lists exist within the site in which the IssueArchiver feature is being deactivated."; | |
if (!EventLog.SourceExists(sSource)) | |
{ | |
EventLog.CreateEventSource(sSource, sLog); | |
} | |
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 888); | |
} | |
finally | |
{ | |
if (site != null) | |
{ | |
site.Dispose(); | |
} | |
} | |
} | |
public override void FeatureInstalled(SPFeatureReceiverProperties properties) { } | |
public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment