Return to Snippet

Revision: 61288
at December 4, 2012 12:35 by rumremix


Initial Code
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Linq;

namespace Lilly.Navigation2012.Features.Feature1
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("5253cb53-9e84-478d-b425-86d2cd8bc09b")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            SPSite site = properties.Feature.Parent as SPSite;

            if (site != null)
            {
                SPWeb rootWeb = site.RootWeb;

                // check in files
                SPList styleLibrary = rootWeb.Lists.TryGetList("Style Library");
                if (styleLibrary != null)
                {

                    var checkedOutFiles = from SPListItem item in styleLibrary.Items
                                          where string.Equals((string)item.Properties["FeatureId"], properties.Feature.DefinitionId.ToString(), StringComparison.OrdinalIgnoreCase)
                                          && (item.Level == SPFileLevel.Checkout || item.Level == SPFileLevel.Draft)
                                          select item;

                    foreach (SPListItem checkedOutFile in checkedOutFiles)
                    {
                        if (checkedOutFile.Level == SPFileLevel.Checkout)
                        {
                            // Check in a major version, not publishing enabled
                            checkedOutFile.File.CheckIn(string.Empty, SPCheckinType.MajorCheckIn);
                        }
                        else if (checkedOutFile.Level == SPFileLevel.Draft)
                        {
                            // Publish files
                            checkedOutFile.File.Publish(string.Empty);
                        }

                        if (checkedOutFile.ModerationInformation != null && checkedOutFile.ModerationInformation.Status != SPModerationStatusType.Approved)
                        {
                            // Approve files
                            SPFile file = checkedOutFile.Web.GetFile(checkedOutFile.File.UniqueId);
                            file.Approve(string.Empty);
                        }
                    }
                }
            }
        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        //public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}

Initial URL


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

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

Initial Tags
event, sharepoint

Initial Language
C#