SharePoint Version Archiver

  1. using System;
  2. using System.Security.Permissions;
  3. using Microsoft.SharePoint;
  4. using Microsoft.SharePoint.Utilities;
  5. using Microsoft.SharePoint.Workflow;
  6. using System.Text;
  7. using System.Collections.Generic;
  8.  
  9. namespace SPRecordCreator.Archiver
  10. {
  11.  
  12. public class Archiver : SPItemEventReceiver
  13. {
  14.  
  15. public override void ItemUpdating(SPItemEventProperties properties)
  16. {
  17. if (ShouldDoSomething(properties, false))
  18. {
  19. base.ItemUpdating(properties);
  20. SendToRecordsCenter(properties);
  21. }
  22. }
  23.  
  24. public override void ItemDeleting(SPItemEventProperties properties)
  25. {
  26. if (ShouldDoSomething(properties, true))
  27. {
  28. base.ItemDeleting(properties);
  29. SendToRecordsCenter(properties, true);
  30. }
  31. }
  32.  
  33. public override void ItemCheckingOut(SPItemEventProperties properties)
  34. {
  35. if (ShouldDoSomething(properties, false))
  36. {
  37. base.ItemCheckingOut(properties);
  38. properties.ErrorMessage = "Check out is not allowed when using the " +
  39. "Record Archiver. The Record Archiver prevents check out of " +
  40. "documents to ensure that the correct version of the document is " +
  41. "sent to the archive. This function only effects content types that " +
  42. "are within the \"My Group\" group of content types.";
  43. properties.Status = SPEventReceiverStatus.CancelWithError;
  44. }
  45. }
  46. private Boolean ShouldDoSomething(SPItemEventProperties properties, Boolean deleting)
  47. {
  48. if (properties.ListItem == null)
  49. return false;
  50. if (properties.ListItem.ContentType.Group.ToString() != "My Group")
  51. return false;
  52. return CheckVersioning(properties, deleting);
  53. }
  54.  
  55. private Boolean CheckVersioning(SPItemEventProperties properties, Boolean deleting)
  56. {
  57. if (!properties.List.EnableVersioning)
  58. return true;
  59.  
  60. Int32 MinorVersion = properties.ListItem.File.MinorVersion;
  61. Int32 MajorVersion = properties.ListItem.File.MajorVersion;
  62.  
  63. if (deleting && MinorVersion == 0)
  64. return true;
  65.  
  66. if (MajorVersion > 0 && MinorVersion == 0)
  67. return true;
  68.  
  69. return false;
  70. }
  71.  
  72. private static void SendToRecordsCenter(SPItemEventProperties properties)
  73. {
  74. try
  75. {
  76. SPFile file = properties.ListItem.File;
  77. string strOut = "";
  78. OfficialFileResult retVal = file.SendToOfficialFile(out strOut);
  79. string value = retVal.ToString();
  80. }
  81. catch (Exception ex)
  82. {
  83. // this will never save without retention piece working
  84. properties.ErrorMessage = ex.Message;
  85. properties.Status = SPEventReceiverStatus.CancelWithError;
  86. }
  87. }
  88. }
  89. }