Tuesday, May 29, 2012

Provisioning Themes to Site Collections and Sub Sites

Provisioning Themes to Site and his sub-sites have been a very request subject by users and i saw a lot of people talking about this subject with the SharePoint Features you should use like “Features”, “Elements”, "Stapling" and "WebProvisioned Handler", but I never saw Real World examples where i can redirect to a site to help them create the feature, that is the reason why I created this example.

This solution will add and activate a Custom Theme when a Site Collection associated to a Template is created (in this Case Standard Collection Sites STS#0,…)  and will inherit the Custom Theme every time a Sub site is created.
Create the Theme with PowerPoint
The first thing we need to do is create our Site Theme, one recommended way to create your Theme is using PowerPoint or using “Theme Builder” https://connect.microsoft.com/themebuilder
If you choose Powerpoint you can use one of the Theme that came out of the box using in the Ribbon option “Design > All Themes” there you can make the changes for your requirements.
After you make the changes you can select the option “save Current Theme” and save the file as “CustomTheme.Thmx”.
The file created will be used in our SharePoint Solution to add and activate the Custom Theme when a Site Collection or Sub Site is created.
Create SharePoint Solution to Provision the Theme
The solution will be created using the
  1. Visual Studio 2010
  2. SharePoint 2010
Will be created the following Features:
  1. Stapling STS Template
  1. Scope: WebApplication
  1. FeatureSiteTemplateAssociation (Id of “Provision Site Theme”)
  1. Provision Site Theme
  1. Scope: Site
  1. Feature (FeatureActivated)
  1. Provision Web Theme
  1. Scope: Web
  1. Event Handler (WebProvisioned)
SharePoint Solution
The SharePoint Solution will have 3 Features that will create dependency between them in a way to change the Theme in all process of a new Site Collection or Sub Site without actions of a Site Owner or Site Administrator.
Provision Stapling to Site Template
This Provision Action will add a custom Feature in each Site template, this action will give flexibility to change with the necessary actions and change the Theme to our custom Theme when a Site collection is created.
Create a New Feature, Right Click on Features Folder and select “Add Feature” and select the Scope “WebApplication”.
To add a custom Feature to the Site Template needs to add  Element.xml File where you configure the tag “FeatureSiteTemplateAssociation” and Template Name “STS#0, STS#1, STS#2” where will be the stapling and will be activated when a Site Collection is created.
Provision Site Theme
Create a New Feature, Right Click on Features Folder and select “Add Feature” and select the Scope “Site”.
This Feature will add the Theme file to the Root Site Folder, publish the new Theme and activate the provisioning Web Feature of the Sub Site using Event Receiver.
 Create a new Element.xml file and add the Theme CustomTheme.Thmx using a Module Tag.
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="ModuleMyTheme" Url="_catalogs/theme">
    <File Path="NewTheme\CustomTheme.thmx" Url="CustomTheme.thmx" />
This Tag will be use add and manage Files from the SharePoint Solution to be added in the SharePoint Sites and all themes files are stored in the Path “_catalog/theme”, you can view all Themes file using SharePoint Designer.
PS: Needs to be reference the parameter “ElementFile” in the File “CustomTheme.thmx”
and Activate Web Feature:
web.Features.Add(new Guid("1a14b521-aa1d-4f2e-bc23-df127e6e45bb"),true);

Provision Site Web
Create a New Feature, Right Click on Features Folder and select “Add Feature” and select the Scope “web”.
This Feature will inherit the Theme from the parent Site and activate the provisioning of the Sub Site. The provision of the Sub Site is made by Event receiver.
When we select the Event Handler we need to define what type of event we will use, since we want to change the Sub Site when is created we need to add Event in the SharePoint Object SPWeb.
This Event need to be executed after the Sub Site is created “A Site was provisioned” to change the Theme and add the next Event hander for the next Sub Site.
After we add to Visual Studio the Event Handler the properties are define to be global and deployed every time that needs to be call.
After we access to the Web Feature, the Event Hander “ApplyTheme” needs to be associated the Feature “Provision Web Theme” to be activated when a Sub site is created.
After the Event hander is added, need to define the actions associated to this Event,
The Actions will be apply the Theme to the Web,
And Activate the Web Feature “Provision Site Web”, when a sub Site is created the Event Handler for the Web Provisioning associated to the Site is created and the Theme is changed and this will replicate for all new Sub Sites Created.
You can use the SharePoint Manager to view the “Eventreceiver” created in the Object SPWeb, this Event will change the Theme and provision the same change for the Sub Sites.
After the 3 Feature are created you are prepare to Deploy as SharePoint Farm Solution and make the test.
Deploy and Activate Features
To Deploy a SharePoint Solution , you can use the Visual Studio Deployment option, Right Click on Provision Theme and select “Deploy”.
You can use PowerSheel commands to deploy this solution in the SharePoint Farm.
//Add solution to SharePoint Farm
Add-SPSolution -LiteralPath <SolutionPath>
Install-SPSolution -Identity <SolutionName> -WebApplication <URLname> -GACDeployment
To activate the Provision of the Theme in the Site Collection and SubWeb needs to access to SharePoint 2010 Central Administration > Manage Web Application > Select Web Application > Manage Features, there you need to activate Feature “Stapling STS Template”.

After the Feature “Stapling STS Template” is activated we can create a new site Collection to assume the new Theme Solution:
SharePoint 2010 Central Administration > Application Management > Create Site collections
After the Site Collections is created the SharePoint Site should appear with the new theme as expected.
You can validate the Custom Theme was changed accessing to “Site Actions > Site Settings > Site Theme”  and view the CustomTheme as selected in the Solution as expected.
To validate if the Site Collection Feature was activated need to access “Site Actions > Site Settings > Site Actions > Manage site features” and view “Provision Site Theme”.
If you access to “Site Actions > Site Settings > Site collections features” the Feature Provision Web Them will be Activated
After you validate The Root Page, you can create a sub Site and view if the Sub site have the Same Provisioning Handler and change Theme like the Root Site.
After the Sub Site is created, needs to wait some seconds to finish the provisioning and refresh the page to assume the changes in the Sub site.
Project Example
Reference Links:
Feature Stapling
SPWebEventReceiver Methods

PS: These last months a lot of good things are happening to me and that are taking me all time and can't make a lot more SharePoint articles, but the reasons are good and now I am just waiting to came to my arms : ).

Hope you like this article,
Andre Lage