sharepoint 2010 event receiver code that checks in files newly added to style library


/ Published in: C#
Save to your folder(s)

Keep the namespace name, class name, guid that VS automatically generates. Make sure all of the "using" inclusions included.


Copy this code and paste it in your HTML
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security.Permissions;
  4. using Microsoft.SharePoint;
  5. using Microsoft.SharePoint.Security;
  6. using System.Linq;
  7.  
  8. namespace Lilly.Navigation2012.Features.Feature1
  9. {
  10. /// <summary>
  11. /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
  12. /// </summary>
  13. /// <remarks>
  14. /// The GUID attached to this class may be used during packaging and should not be modified.
  15. /// </remarks>
  16.  
  17. [Guid("5253cb53-9e84-478d-b425-86d2cd8bc09b")]
  18. public class Feature1EventReceiver : SPFeatureReceiver
  19. {
  20. // Uncomment the method below to handle the event raised after a feature has been activated.
  21.  
  22. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  23. {
  24. if (properties == null)
  25. {
  26. throw new ArgumentNullException("properties");
  27. }
  28.  
  29. SPSite site = properties.Feature.Parent as SPSite;
  30.  
  31. if (site != null)
  32. {
  33. SPWeb rootWeb = site.RootWeb;
  34.  
  35. // check in files
  36. SPList styleLibrary = rootWeb.Lists.TryGetList("Style Library");
  37. if (styleLibrary != null)
  38. {
  39.  
  40. var checkedOutFiles = from SPListItem item in styleLibrary.Items
  41. where string.Equals((string)item.Properties["FeatureId"], properties.Feature.DefinitionId.ToString(), StringComparison.OrdinalIgnoreCase)
  42. && (item.Level == SPFileLevel.Checkout || item.Level == SPFileLevel.Draft)
  43. select item;
  44.  
  45. foreach (SPListItem checkedOutFile in checkedOutFiles)
  46. {
  47. if (checkedOutFile.Level == SPFileLevel.Checkout)
  48. {
  49. // Check in a major version, not publishing enabled
  50. checkedOutFile.File.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
  51. }
  52. else if (checkedOutFile.Level == SPFileLevel.Draft)
  53. {
  54. // Publish files
  55. checkedOutFile.File.Publish(string.Empty);
  56. }
  57.  
  58. if (checkedOutFile.ModerationInformation != null && checkedOutFile.ModerationInformation.Status != SPModerationStatusType.Approved)
  59. {
  60. // Approve files
  61. SPFile file = checkedOutFile.Web.GetFile(checkedOutFile.File.UniqueId);
  62. file.Approve(string.Empty);
  63. }
  64. }
  65. }
  66. }
  67. }
  68.  
  69.  
  70. // Uncomment the method below to handle the event raised before a feature is deactivated.
  71.  
  72. //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  73. //{
  74. //}
  75.  
  76.  
  77. // Uncomment the method below to handle the event raised after a feature has been installed.
  78.  
  79. //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
  80. //{
  81. //}
  82.  
  83.  
  84. // Uncomment the method below to handle the event raised before a feature is uninstalled.
  85.  
  86. //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
  87. //{
  88. //}
  89.  
  90. // Uncomment the method below to handle the event raised when a feature is upgrading.
  91.  
  92. //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
  93. //{
  94. //}
  95. }
  96. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.