Online Training On SharePoint
                      
Showing posts with label Finding all themes using Object Model. Show all posts
Showing posts with label Finding all themes using Object Model. Show all posts

Saturday, 2 January 2010

Applying SharePoint Themes using SharePoint Object Model

I had a requirment where I need to get all the available themes and apply or remove a themes in SharePoint using SharePoint Object Model. Here is the code which helps to do this:
// To get all available themes using SharePoint Object Model
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    ReadOnlyCollection<ThmxTheme> themes = ThmxTheme.GetManagedThemes(site);
                    foreach (ThmxTheme theme in themes)
                    {
                        Console.Write("Theme Name: {0}\nServer Relative Url: {1}\n\n", theme.Name, theme.ServerRelativeUrl);
                    }
                }
            }

            // To apply a theme to a SharePoint Web using SharePoint Object Model
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    ThmxTheme theme = ThmxTheme.Open(site, "/_catalogs/theme/Viewpoint.thmx");
                    theme.ApplyTo(web, false);
                    try
                    {
                        web.Update();
                        Console.Write("\nUpdated web with theme: {0}", theme.Name);
                    }
                    catch (Exception e)
                    {
                        Console.Write("Error: {0}\nStack Trace: {1}\n\n", e.Message, e.StackTrace);
                    }
                }
            }

            //  To remove a theme to a SharePoint Web using SharePoint Object Model
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        ThmxTheme.RemoveThemeFromWeb(web, false);
                        web.Update();
                        Console.Write("\nRemoved theme from SPWeb");
                    }
                    catch (Exception e)
                    {
                        Console.Write("\nError: {0}\nStack Trace: {1}\n", e.Message, e.StackTrace);
                    }                    
                }
            }
If you like this blog please visit the sponsors from SharePointAds.com @
Related Posts with Thumbnails