Monday, September 10, 2012

Programmatically get SharePoint Themes

In this article we will discuss how to get all sharepoint themes programmatically using SharePoint 2010 object model. Also you can check my last article on how to create custom theme in sharepoint 2010. Also you can check one more article on how to programmatically apply theme to sharepoint 2010 site.

For this example I am writting the code in a visual web part. All the Themes of the site will be printed in a Label.

Full code:

<asp:Label ID="lblThemes" runat="server"></asp:Label>

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Collections.ObjectModel;
using System.Text;

namespace SharePointPOC.VisualWebPart1
{
    public partial class VisualWebPart1UserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetAllTheme();
        }

void GetAllTheme()
        {
            StringBuilder sb = new StringBuilder();
            using (SPSite siteCollection = new SPSite("http://bsahoo3:2500/sites/TestSiteCollection/"))
            {
                using (SPWeb web = siteCollection.OpenWeb())
                {
                    ReadOnlyCollection themes = ThmxTheme.GetManagedThemes(siteCollection);
                    foreach (ThmxTheme theme in themes)
                    {
                        sb.Append("\n");
                        sb.Append(theme.Name);
                    }
                }
            }
            lblThemes.Text = sb.ToString();
        }
    }
}

Once you will deploy the code then you will able to see the list of all themes belongs to the site as shown in the figure below

No comments:

Post a Comment