using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Text;
using System.Collections.Generic;
namespace SPRecordCreator.Archiver
{
public class Archiver : SPItemEventReceiver
{
public override void ItemUpdating(SPItemEventProperties properties)
{
if (ShouldDoSomething(properties, false))
{
base.ItemUpdating(properties);
SendToRecordsCenter(properties);
}
}
public override void ItemDeleting(SPItemEventProperties properties)
{
if (ShouldDoSomething(properties, true))
{
base.ItemDeleting(properties);
SendToRecordsCenter(properties, true);
}
}
public override void ItemCheckingOut(SPItemEventProperties properties)
{
if (ShouldDoSomething(properties, false))
{
base.ItemCheckingOut(properties);
properties.ErrorMessage = "Check out is not allowed when using the " +
"Record Archiver. The Record Archiver prevents check out of " +
"documents to ensure that the correct version of the document is " +
"sent to the archive. This function only effects content types that " +
"are within the \"My Group\" group of content types.";
properties.Status = SPEventReceiverStatus.CancelWithError;
}
}
private Boolean ShouldDoSomething(SPItemEventProperties properties, Boolean deleting)
{
if (properties.ListItem == null)
return false;
if (properties.ListItem.ContentType.Group.ToString() != "My Group")
return false;
return CheckVersioning(properties, deleting);
}
private Boolean CheckVersioning(SPItemEventProperties properties, Boolean deleting)
{
if (!properties.List.EnableVersioning)
return true;
Int32 MinorVersion = properties.ListItem.File.MinorVersion;
Int32 MajorVersion = properties.ListItem.File.MajorVersion;
if (deleting && MinorVersion == 0)
return true;
if (MajorVersion > 0 && MinorVersion == 0)
return true;
return false;
}
private static void SendToRecordsCenter(SPItemEventProperties properties)
{
try
{
SPFile file = properties.ListItem.File;
string strOut = "";
OfficialFileResult retVal = file.SendToOfficialFile(out strOut);
string value = retVal.ToString();
}
catch (Exception ex)
{
// this will never save without retention piece working
properties.ErrorMessage = ex.Message;
properties.Status = SPEventReceiverStatus.CancelWithError;
}
}
}
}