Here are the steps,
Step 1: Create Provider Hosted App using Visual Studio as in the following,
Go to Visual Studio, Office/SharePoint Tab, Apps and select App for SharePoint. Give some unique name “SharePointBranding”.

Select Hosting option as “Provider Hosted” as in the following,

Select Web Application type “ASP.NET Web Form Application” as in the following screenshot,

Select Authentication Type as “Certificate” as in the following,:

Step 2: Solution Structure looks like the following,
- Add folder named “CustomJS” with new custom JavaScript file named “App.js”.
- //function to get a parameter value by a specific key
- function getQueryStringParameter(urlParameterKey)
- {
- var params = document.URL.split('?')[1].split('&');
- var strParams = '';
- for (var i = 0; i < params.length; i = i + 1)
- {
- var singleParam = params[i].split('=');
- if (singleParam[0] == urlParameterKey) return singleParam[1];
- }
- }
- function Branding()
- {
- console.log("Branding");
- }
- $(document).ready(function ()
- {
- //Get the URI decoded SharePoint site url from the SPHostUrl parameter.
- var spHostUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
- //Build absolute path to the layouts root with the spHostUrl
- var layoutsRoot = spHostUrl + '/_layouts/15/';
- //load all appropriate scripts for the page to function
- $.getScript(layoutsRoot + 'SP.Runtime.js', function ()
- {
- $.getScript(layoutsRoot + 'SP.js', function ()
- {
- //Execute the correct script based on the isDialog
- //Load the SP.UI.Controls.js file to render the App Chrome
- $.getScript(layoutsRoot + 'SP.UI.Controls.js', renderSPChrome);
- });
- });
- });
- Add folder named “Resources” with two files “NewCustom.spcolor” and “NewCustombg.jpg”

Step 3: Default.aspx page
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SharePointBrandingWeb.Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src="../Scripts/jquery-1.9.1.min.js" type="text/javascript"></script>
- </head>
- <body>
- <form id="form1" runat="server">
- <asp:ScriptManager ID="ScriptManager1" runat="server" EnableCdn="True" />
- <div id="divSPChrome"></div>
- <div style="left: 40px; position: absolute;">
- <table>
- <tr>
- <td>Set Out Of Box theme to host site </td>
- <td>
- <asp:DropDownList runat="server" ID="drpThemes" Width="100px">
- <asp:ListItem Text="Orange" Value="Orange" Selected="True"></asp:ListItem>
- <asp:ListItem Text="Green" Value="Green"></asp:ListItem>
- <asp:ListItem Text="Nature" Value="Nature"></asp:ListItem>
- <asp:ListItem Text="Blossom" Value="Blossom"></asp:ListItem>
- <asp:ListItem Text="Office" Value="Office"></asp:ListItem>
- <asp:ListItem Text="Breeze" Value="Breeze"></asp:ListItem>
- <asp:ListItem Text="Immerse" Value="Immerse"></asp:ListItem>
- </asp:DropDownList>
- </td>
- <td>
- <asp:Button runat="server" ID="btnSetThemeForHost" Text="Set out of the box theme" OnClick="btnSetThemeForHost_Click" /> </td>
- </tr>
- <tr>
- <td colspan="3"> </td>
- </tr>
- <tr>
- <td colspan="3">
- <asp:Label ID="lblStatus1" runat="server" /> </td>
- </tr>
- </table>
- <br />
- <table>
- <tr>
- <td>Deploy a new theme and apply to host site </td>
- <td colspan="2">
- <asp:Button runat="server" ID="btnDeployTheme" Text="Deploy a custom theme" OnClick="btnDeployTheme_Click" /> </td>
- </tr>
- <tr>
- <td colspan="3"> </td>
- </tr>
- <tr>
- <td colspan="3">
- <asp:Label ID="lblStatus2" runat="server" /> </td>
- </tr>
- </table>
- </div>
- <script src="../CustomJS/App.js" type="text/javascript"></script>
- </form>
- </body>
- </html>

Step 4: Register Script on page.
On page load we are going to register script named “BasePageScript” into page as shown below.
- protected void Page_Load(object sender, EventArgs e)
- {
- // The following code gets the client context and Title property by using TokenHelper.
- // To access other properties, the app may need to request permissions on the host web.
- var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
- using(var clientContext = spContext.CreateUserClientContextForSPHost())
- {
- clientContext.Load(clientContext.Web, web => web.Title);
- clientContext.ExecuteQuery();
- Response.Write(clientContext.Web.Title);
- }
- // define initial script, needed to render the chrome control
- string script = @ "
- function chromeLoaded()
- {
- $('body').show();
- }
- //function callback to render chrome after SP.UI.Controls.js loads
- function renderSPChrome()
- {
- //Set the chrome options for launching Help, Account, and Contact pages
- var options = {
- 'appTitle': document.title,
- 'onCssLoaded': 'chromeLoaded()'
- };
- console.log('renderSPChrome ...');
- //Load the Chrome Control in the divSPChrome element of the page
- var chromeNavigation = new SP.UI.Controls.Navigation('divSPChrome', options);
- chromeNavigation.setVisible(true);
- }
- ";
- //register script in page
- Page.ClientScript.RegisterClientScriptBlock(typeof (Default), "BasePageScript", script, true);
- }
- Select any theme from dropdown menu and click on button “Set out Of Box Theme”.
- On button click “btnSetThemeForHost_Click()” method get called, which in turn call “SetThemeBasedOnName()” method.
- public void SetThemeBasedOnName(Web web, string themeName)
- {
- // Let's get instance to the composite look gallery
- List themeList = web.GetCatalog(124);
- web.Context.Load(themeList);
- web.Context.ExecuteQuery();
- // We are assuming that the theme exists
- CamlQuery query = new CamlQuery();
- string camlString = @ " < View > < Query > < Where > < Eq > < FieldRef Name = 'Name' / > < Value Type = 'Text' >
- {
- 0
- } < /Value> < /Eq> < /Where> < /Query> < /View>";
- // Let's update the theme name accordingly
- camlString = string.Format(camlString, themeName);
- query.ViewXml = camlString;
- var found = themeList.GetItems(query);
- web.Context.Load(found);
- web.Context.ExecuteQuery();
- if (found.Count > 0)
- {
- Microsoft.SharePoint.Client.ListItem themeEntry = found[0];
- //Set the properties for applying custom theme which was jus uplaoded
- string spColorURL = null;
- if (themeEntry["ThemeUrl"] != null && themeEntry["ThemeUrl"].ToString().Length > 0)
- {
- spColorURL = MakeAsRelativeUrl((themeEntry["ThemeUrl"] as FieldUrlValue).Url);
- }
- string spFontURL = null;
- if (themeEntry["FontSchemeUrl"] != null && themeEntry["FontSchemeUrl"].ToString().Length > 0)
- {
- spFontURL = MakeAsRelativeUrl((themeEntry["FontSchemeUrl"] as FieldUrlValue).Url);
- }
- string backGroundImage = null;
- if (themeEntry["ImageUrl"] != null && themeEntry["ImageUrl"].ToString().Length > 0)
- {
- backGroundImage = MakeAsRelativeUrl((themeEntry["ImageUrl"] as FieldUrlValue).Url);
- }
- // Set theme to host web
- web.ApplyTheme(spColorURL, spFontURL, backGroundImage, false);
- // Let's also update master page, if needed
- if (themeEntry["MasterPageUrl"] != null && themeEntry["MasterPageUrl"].ToString().Length > 0)
- {
- web.MasterUrl = MakeAsRelativeUrl((themeEntry["MasterPageUrl"] as FieldUrlValue).Url);
- }
- // Execute the needed code
- web.Context.ExecuteQuery();
- }
- }
- private string MakeAsRelativeUrl(string urlToProcess)
- {
- Uri uri = new Uri(urlToProcess);
- return uri.AbsolutePath;
- }
- protected void btnSetThemeForHost_Click(object sender, EventArgs e)
- {
- var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
- using(var clientContext = spContext.CreateUserClientContextForSPHost())
- {
- if (clientContext != null)
- {
- Web web = clientContext.Web;
- string selectedTheme = drpThemes.SelectedValue;
- SetThemeBasedOnName(web, selectedTheme);
- lblStatus1.Text = string.Format("Theme '{0}' has been applied to the <a href='{1}'>host web</a>.", selectedTheme, spContext.SPHostUrl.ToString());
- }
- }
- }
- Existing theme resides inside ‘Composite Look Gallery’, so we first need to get access to composite look gallery for that the following line of code is used.
- List themeList = web.GetCatalog(124);
- web.Context.Load(themeList);
- web.Context.ExecuteQuery();
- Using CAML Query object, get list item corresponding to selected theme.
- Let’s find “ThemeURL”, “ImageURL”, “MasterPageURL” which is needed to apply theme to the site.
- // Set theme to host web
- web.ApplyTheme(spColorURL, spFontURL, backGroundImage, false);

Step 6: Output of applying existing theme to SharePoint Site.

Step 7: Deploy Custom Theme





Santhakumar MunuswamyPosted Nov 28, 2015, 2:39 AM
Nice share
Vinodh NarayananPosted Nov 25, 2015, 4:10 AM
thanks for sharing
Rupali ShindePosted Nov 25, 2015, 3:57 AM
Thank you sir :)
Prasham SabadraPosted Nov 25, 2015, 2:22 AM
Thanks for Sharing :)
Raja TPosted Nov 25, 2015, 1:20 AM
Nice, Thanks for sharing