SharePoint 2013 - SPLessons

Apply custom theme to SharePoint Site o...

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Apply custom theme to SharePoint Site once feature activated

Apply custom theme to SharePoint Site once feature activated

 

Description:  I will explain you how to apply our custom theme whenever the feature activated and revert back to the Sharepoint default theme once we deactivate the feature.

 

Step 1: I will explain you how to apply our custom theme whenever the feature activated and revert back to the Sharepoint default theme once we deactivate the feature.

Step 2:  We will achieve it by adding Event Reciever to our project.  Just Right click on the MainFeature1 of our project and select Add Event Receiver.

 

Step 3:  Open the MainFeature1.EventReceiver.cs file and

Uncomment the two functions 1) FeatureActivated, 2) FeatureDeactivating .

Add the below code to your project.


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

namespace SPLessonsBranding.Features.MainFeature1
{
    /// <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("a98faee3-a674-44f3-96ef-418c6665f23a")]
    public class MainFeature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {

            SPSite siteCollection = properties.Feature.Parent as SPSite;
            if (siteCollection != null)
            {
                SPWeb topLevelSite = siteCollection.RootWeb;

                // Calculate relative path to site from Web Application root.
                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/SPBranding1.master";
                    site.CustomMasterUrl = WebAppRelativePath +"_catalogs/masterpage/SPBranding1.master";
                    site.AlternateCssUrl = WebAppRelativePath +"SPCustomCss/css/customstyle.css";
                    site.UIVersion = 4;
                    site.Update();
                }

            }

        }

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

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {

            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/v4.master";
                    site.CustomMasterUrl = WebAppRelativePath +"_catalogs/masterpage/v4.master";
                    site.AlternateCssUrl = "";
                    site.SiteLogoUrl = "";
                    site.Update();
                }
            }
        }

        // 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)
        //{
        //}
    }
}

Step 4:  Deploy the solution and test the project by activate the feature.

Steps to active the feature.

1) Go the Top level site, Site Settings and Click on site collection features.

2) There you will find our feature named as MainFeature1, just click on active.