Last active
August 29, 2015 14:25
-
-
Save skaggej/2e2ba02647f6ea2bbf1c 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.Diagnostics; | |
using Microsoft.SharePoint; | |
namespace EricSFeatures | |
{ | |
public class IssuesItemEventReceiver : SPItemEventReceiver | |
{ | |
string sArchiveList = "Issues Archive"; | |
string sArchiveFieldClosed = "Closed"; | |
string sArchiveFieldCNC = "CouldNotClose"; | |
public override void ItemUpdated(SPItemEventProperties properties) | |
{ | |
SPListItem sourceItem=null; | |
SPWeb site = null; | |
try | |
{ | |
DisableEventFiring(); | |
if ((properties.ListItem[sArchiveFieldClosed]!=null && properties.ListItem[sArchiveFieldClosed].ToString().Trim('#', ';').ToUpper() == "CLOSED") || (properties.ListItem[sArchiveFieldCNC]!=null && properties.ListItem[sArchiveFieldCNC].ToString().Trim('#', ';').ToUpper() == "COULD NOT CLOSE")) | |
{ | |
sourceItem = properties.ListItem; | |
site = properties.OpenWeb(); | |
SPList listIssuesArchive = site.Lists[sArchiveList]; | |
SPListItem archiveItem = listIssuesArchive.Items.Add(); | |
for (int i = sourceItem.Versions.Count - 1; i >= 0; i--) | |
{ | |
foreach (SPField sourceField in sourceItem.Fields) | |
{ | |
SPFieldType ArchiveSourceType = sourceField.Type; | |
SPListItemVersion version = sourceItem.Versions[i]; | |
if ((!sourceField.ReadOnlyField) && (ArchiveSourceType != SPFieldType.Attachments)) | |
{ | |
archiveItem[sourceField.Title] = version[sourceField.Title]; | |
} | |
else | |
{ | |
if (sourceField.Title == "Created" || sourceField.Title == "Created By" || sourceField.Title == "Modified" || sourceField.Title == "Modified By") | |
{ | |
archiveItem[sourceField.Title] = version[sourceField.Title]; | |
} | |
} | |
} | |
archiveItem.Update(); | |
} | |
foreach (string attachmentName in sourceItem.Attachments) | |
{ | |
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + attachmentName); | |
archiveItem.Attachments.Add(attachmentName, file.OpenBinary()); | |
} | |
archiveItem.Update(); | |
sourceItem.Delete(); | |
} | |
} | |
catch (Exception e) | |
{ | |
string sSource; | |
string sLog; | |
string sEvent; | |
sSource = "IssueArchiver"; | |
sLog = "Application"; | |
sEvent = e.ToString() + ": Most likely the Issues list and the Issues Archive list have mismatching fields"; | |
if (!EventLog.SourceExists(sSource)) | |
{ | |
EventLog.CreateEventSource(sSource, sLog); | |
} | |
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 888); | |
} | |
finally | |
{ | |
if (site != null) | |
{ | |
site.Dispose(); | |
} | |
EnableEventFiring(); | |
} | |
} | |
} | |
public class IssuesArchiveItemEventReceiver : SPItemEventReceiver | |
{ | |
string sIssuesList = "Issues"; | |
string sIssuesFieldClosed = "Closed"; | |
string sIssuesFieldCNC = "CouldNotClose"; | |
public override void ItemUpdated(SPItemEventProperties properties) | |
{ | |
SPWeb site = null; | |
SPListItem sourceItem = null; | |
try | |
{ | |
DisableEventFiring(); | |
if ((properties.ListItem[sIssuesFieldClosed] == null && properties.ListItem[sIssuesFieldCNC] == null)) | |
{ | |
sourceItem = properties.ListItem; | |
site = properties.OpenWeb(); | |
SPList listIssues = site.Lists[sIssuesList]; | |
SPListItem IssuesItem = listIssues.Items.Add(); | |
for (int i = sourceItem.Versions.Count - 1; i >= 0; i--) | |
{ | |
foreach (SPField sourceField in sourceItem.Fields) | |
{ | |
SPFieldType IssuesSourceType = sourceField.Type; | |
SPListItemVersion version = sourceItem.Versions[i]; | |
if ((!sourceField.ReadOnlyField) && (IssuesSourceType != SPFieldType.Attachments)) | |
{ | |
IssuesItem[sourceField.Title] = version[sourceField.Title]; | |
} | |
else | |
if (sourceField.Title == "Created" || sourceField.Title == "Created By" || sourceField.Title == "Modified" || sourceField.Title == "Modified By") | |
{ | |
IssuesItem[sourceField.Title] = version[sourceField.Title]; | |
} | |
} | |
IssuesItem.Update(); | |
} | |
foreach (string attachmentName in sourceItem.Attachments) | |
{ | |
SPFile file = sourceItem.ParentList.ParentWeb.GetFile(sourceItem.Attachments.UrlPrefix + attachmentName); | |
IssuesItem.Attachments.Add(attachmentName, file.OpenBinary()); | |
} | |
IssuesItem.Update(); | |
sourceItem.Delete(); | |
} | |
} | |
catch (Exception e) | |
{ | |
string sSource; | |
string sLog; | |
string sEvent; | |
sSource = "IssueArchiver"; | |
sLog = "Application"; | |
sEvent = e.ToString() + ": Most likely the Issues list and the Issues Archive list have mismatching fields"; | |
if (!EventLog.SourceExists(sSource)) | |
{ | |
EventLog.CreateEventSource(sSource, sLog); | |
} | |
EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 888); | |
} | |
finally | |
{ | |
if (site != null) | |
{ | |
site.Dispose(); | |
} | |
EnableEventFiring(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment