Programmatically change the site theme in SharePoint

In this article we will be seeing how to change the site theme in SharePoint using SharePoint object model.

Go to Site Actions => Site Settings => Look and Feel => Site theme.


Programmatically change the site theme:

1. Open Visual Studio 2010.

2. Go to File => New => Project.

3. Select Console Application from the installed templates.

4. Enter the Name and click on Ok.

5. Add the following Namespaces.

o using Microsoft.SharePoint.Utilities; o using System.Collections.ObjectModel;

6. Replace Program.cs with the following code.

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using System.Xml;

  7. using Microsoft.SharePoint.Utilities;

  8. using System.Collections.ObjectModel;

  9. namespace ChangeTheme

  10. {

  11. class Program   

  12. {

  13. static void Main(string[] args)       

  14. {

  15. using (SPSite siteCollection = new SPSite("http://serverName:1111/sites/Sample"))           

  16. {

  17. using (SPWeb web = siteCollection.OpenWeb())               

  18. {

  19. ThmxTheme theme =ThmxTheme.Open(siteCollection,"_catalogs/theme/Azure.thmx");                  

  20. theme.ApplyTo(web,false);                   

  21. web.Update();               

  22. }           

  23. }       

  24. }   

  25. }

  26. }

7. Build the solution.

8. Hit F5.


Output:


ThmxTheme class:

Represents a Microsoft Office XML theme file. The methods and properties of this class enable read and write operations on the theme name, the color list, and the font list. For more information referhttp://msdn.microsoft.com/en-us/library/ee658367.aspx.

ThmxTheme.ApplyTo Method (SPWeb, Boolean):

Applies the current theme to the given web. . For more information referhttp://msdn.microsoft.com/en-us/library/ee659982.aspx