次の方法で共有


Programmatically retrieving all themes of a site and apply a theme

In SharePoint 2010 if you want to play with the theme via code it is different than the way it was in MOSS 2007 / WSS 3.0. We have to use Microsoft.SharePoint.Utilities.ThmxTheme class.

In this post I will show how we can retrieve all themes for a particular site and how we can set a theme to the website via code.

As you can see in the screen shot, I am using Default theme for my SharePoint website.

image

In SharePoint 2010 all themes are based on .thmx, because of that even if you create a theme in PowerPoint 2010 you can directly use that theme in your SharePoint website.

First I will show how we can download and upload a theme from PowerPoint 2010 to SharePoint 2010 and after that will show how we can apply it via code ( we can do it through UI easily but my primary objective is to show how we can manage theme via object model).

       1. Open a new instance of PowerPoint 2010 and click on the design menu and select a theme and then click on “Save Current Theme” as selected in the below

screen shot.

I have saved the file with name as MyPowerPointTheme.thmx to the hard drive.

image 

2. Open SharePoint site and then go to the theme gallery : site actions à site settings à Galleries à Themes, Click on “add a new item” and then upload the saved thmx file from the hard drive to the theme gallery list.

image 

3. Now you can see “MyPowerPointTheme.thmx” uploaded and showing as a new item in the list.

image

Alright now we can see how we can pull all themes information via code and apply the custom theme to the SharePoint website. Below code is self-explanatory ( I think so J ) , It will output the details of all themes and apply the custom theme that I got from PowerPoint 2010.

<code>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace ChangeTheme
{
    class Program
    {
        static void Main(string[] args)
        {           
            using(SPSite oSite = new SPSite("https://sps-wfe1"))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    System.Collections.ObjectModel.ReadOnlyCollection<ThmxTheme> oThemes = ThmxTheme.GetManagedThemes(oSite);
                    // list out all themes of this particular site
                    foreach (ThmxTheme oTheme in oThemes)
                    {                       
                        Console.WriteLine("Name: " + oTheme.Name + "\n" + "File Name: " + oTheme.File.Name + "\n" + "Accessible Descriptoin: " + oTheme.AccessibleDescription + "\n");
                        Console.WriteLine("************************");

                        // Apply my custom theme to the current website
                        if (oTheme.Name == "MyPowerPointTheme")
                        {
                            ThmxTheme.SetThemeUrlForWeb(oWeb, oTheme.ServerRelativeUrl);                           

                        }
                    }
                }

            }
            Console.ReadLine();

        }
    }
}

</code>

Output

image

Now we can see that the current site theme changed to my custom theme

image

Comments

  • Anonymous
    March 30, 2011
    I have applied your code but i am getting this error. Updates are currently disallowed on GET requests.  To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb. Even i have allowed unsafe updates to true but still i am getting the same error. Please advise