Search This Blog

Sunday, March 13, 2011

Programmatically apply a Theme to a Web in Sharepoint 2010

We can manage the theme from UI of Sharepoint also but sometimes we have a requirement like we need to manage theme through a feature i.e. on feature activation we need to apply some theme and on deactivation remove that theme and apply the default no-theme of Sharepoint.
Add the Microsoft.SharePoint.Utilities reference.

Below is the code by which we can iterate through all the themes and apply to the web
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.ObjectModel;
using Microsoft.SharePoint.Utilities;

namespace GetThemes
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite objSite = new SPSite("http://<Site Url>/"))
            {
                using (SPWeb objWeb = objSite.OpenWeb())
                {
                    ReadOnlyCollection<ThmxTheme> objThmxThemeList;
                    objThmxThemeList = ThmxTheme.GetManagedThemes(objSite);
                    foreach (ThmxTheme objThmxTheme in objThmxThemeList)
                    {
                        if (objThmxTheme.Name == "Graham")
                        {
                            ThmxTheme.SetThemeUrlForWeb(objWeb, objThmxTheme.ServerRelativeUrl);
                            break;
                        }
                    }
                 }           
            }
        }
    }
}

Now if you want to remove the applied theme from the web than pass NULL  instead of theme url as shown below.
ThmxTheme.SetThemeUrlForWeb(objWeb, null);

This will apply the default no theme to Sharepoint site.

No comments:

Post a Comment