Monday 3 March 2014

Event Receiver Set Default MasterPage Sharepoint

using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System.Security;



namespace Sample.Intranet.Branding.Features.Sample.Branding
{

   
    /// <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("de44aef9-a605-42af-b575-d184a048aaba")]
    public class SampleEventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
     
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {

                SPSite siteCollection = properties.Feature.Parent as SPSite;
                if (siteCollection != null)
                {
                    SPWeb toplevelsite = siteCollection.RootWeb;
                    string WebAppRelativePath = toplevelsite.ServerRelativeUrl;
                    if (!WebAppRelativePath.EndsWith("/"))
                    {
                        WebAppRelativePath += "/";


                    }
                    // Enumerate through each site and apply branding.
                  
                    foreach (SPWeb site in siteCollection.AllWebs)
                    {
                        site.MasterUrl = WebAppRelativePath +
                                         "_catalogs/masterpage/Sample/Sample.master";
                        site.CustomMasterUrl = WebAppRelativePath +
                                               "_catalogs/masterpage/Sample/Sample.master";

                        site.UIVersion = 4;
                        site.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


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

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSite siteCollection = properties.Feature.Parent as SPSite;
                if (siteCollection != null)
                {
                    SPWeb topLevelSite = siteCollection.RootWeb;

                    // Calculate relative path of site from Web Application root.
                    string WebAppRelativePath = topLevelSite.ServerRelativeUrl;
                    if (!WebAppRelativePath.EndsWith("/"))
                    {
                        WebAppRelativePath += "/";
                    }

                    // Enumerate through each site and remove custom branding.
                    foreach (SPWeb site in siteCollection.AllWebs)
                    {
                        site.MasterUrl = WebAppRelativePath +
                                         "_catalogs/masterpage/seattle.master";
                        site.CustomMasterUrl = WebAppRelativePath +
                                               "_catalogs/masterpage/seattle.master";
                        site.AlternateCssUrl = "";
                        site.SiteLogoUrl = "";
                        site.Update();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


      
    }
}

No comments:

Post a Comment