none
PowerShell returning Null value for current applied theme of publishing site collection SP2013 RRS feed

  • Question

  • Hi All,

    I want to get the current applied theme of a site collection using Powershell. I used below code, but it is returning null values

              $spWeb = Get-SPWeb -Identity http://sp2013/sites/composedlooks
              [Microsoft.SharePoint.Utilities.ThmxTheme]::GetThemeUrlForWeb($spWeb.RootWeb)

    I also tried to use "[Microsoft.SharePoint.Utilities.ThmxTheme]::GetManagedThemes" to get all themes in theme gallery. This is also returning null.

    I'm not sure where I did wrong. Site collection is ofpublishing type. I applied theme to it from 'Change the Look' option in site settings

    I tested by applying default available theme and also by custom theme. In both cases, current theme is returned as null.

    Can anyone please let me know where it wrong    

    Wednesday, January 11, 2017 6:51 AM

Answers

  • Hi

    this script will give you the name of the current theme applied to the specified site

    To get for all sites, you include a little loop to parse all websites in a sitecollection

    $spweb1 = get-spsite -Identity http://wssp2013
    $spList = $spWeb1.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::DesignCatalog);
    
    
    $SPQuery =  New-Object Microsoft.SharePoint.SPQuery;
           $SPQuery.Query = "<Where><Eq><FieldRef Name='DisplayOrder'/><Value Type='Number'>0</Value></Eq></Where>";
           $SPQuery.RowLimit = 1;
           $spListItems = $spList.GetItems($SPQuery);
           $Current=$spListItems[0]
    
    
    $spList = $spWeb1.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::DesignCatalog);
    foreach ( $item in $spList.Items){
    #$item['DisplayOrder']
    #$item['ows_ThemeUrl']
    if(($item['ows_ThemeUrl'] -match $current['ows_ThemeUrl']) -and !($item['Name'] -match 'Current')){
    
    write-host " The current theme name is "$item.Name
    
    }
    }
    Let me know that is helpful for you


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.


    • Edited by romeo donca Thursday, January 12, 2017 7:34 PM
    • Marked as answer by Pavitra g Friday, January 13, 2017 5:35 AM
    Thursday, January 12, 2017 7:33 PM

All replies

  • Hi

    use this Powershell scripts

    $spWeb= Get-SPsite "http://sp2013/sites/composedlooks"
    $web = $spWeb.OpenWeb()
    $theme = [Microsoft.SharePoint.Utilities.ThmxTheme]::GetManagedThemes($spWeb)

    let me know if it's working


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, January 11, 2017 7:26 AM
  • Hi Romeo Donca,

    Thank you for the reply. Even above code is returning null for me

    

    Wednesday, January 11, 2017 8:21 AM
  • HI Pavitra,

    instead of PS Script. can you try with Console Application using .NEt Code. Please refter the below link

    http://www.c-sharpcorner.com/uploadfile/anavijai/programmatically-get-all-the-available-sharepoint-site-themes/

    Thanks,

    Pons

    Wednesday, January 11, 2017 8:31 AM
  • Hi

    Check for the root site collection , because the themes are stored there


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, January 11, 2017 9:00 AM
  • Hi Ponvannan,

    Thank you for the reply. Even in console application I'm getting 0 themes count

    As per my understanding, GetManagedThemes should return all the below themes that are available for site. Please correct me if I'm wrong

    Wednesday, January 11, 2017 9:00 AM
  • Hi Romeo Donca,

    Even the root site collection is giving 0 themes count :(

    Wednesday, January 11, 2017 9:13 AM
  • Hi

    check if you really have SP Themes in your site collection Gallery

    ttp://SiteCollectionName/_catalogs/theme/15/


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, January 11, 2017 9:24 AM
  • Hi Romeo,

    Yes I have spfont and spcolor files in Themes gallery and also its respective themes in Composed looks gallery

    

    Also in PS,  ThemeInfoURL for web object is giving some info but not Theme property. 

    I'm getting same null values in other dev env also. Plz suggest me if I'm missing any step or any dev env configuration miss or anyone got the correct output. I've bee working on this from past  2 days :(

    • Edited by Pavitra g Wednesday, January 11, 2017 12:32 PM
    Wednesday, January 11, 2017 9:33 AM
  • Hi

    Because a lab is better than the prod env :)

    here is you answer to get all themes , all existing themes when you access the url

    _layouts/15/designgallery.aspx

    $spsite = get-spsite -Identity http://YourSITEURL
    $Design = ($spsite.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::DesignCatalog))
    foreach ( $Theme in $Design.Items){$Theme.Name}

    Let me know if this helps and if you need more details as well


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.


    • Edited by romeo donca Wednesday, January 11, 2017 7:33 PM spelling
    Wednesday, January 11, 2017 7:22 PM
  • Hi Pavitra,

    The following code for your reference:

    namespace ConsoleApplication1
    {
        class Program
        {        
            /// <summary>
            /// Method to query the design catalog and find the matching theme for the given
            /// theme URL and Master Page URL
            /// </summary>
            /// <param name="designGallery">Design gallery</param>
            /// <param name="themeURLValue">Theme URL Value</param>
            /// <param name="masterPageURLValue">Master Page URL value</param>
            /// <returns>Applied themes</returns>
            private static SPListItemCollection GetAppliedThemeName(SPList designGallery, string themeURLValue, string masterPageURLValue)
            {
                var matchingThemeQuery = new SPQuery();
                matchingThemeQuery.RowLimit = 1;
                var themeURLQuery = "<And><Eq><FieldRef Name='ThemeUrl'/><Value Type='URL'>" + themeURLValue + "</Value></Eq>";
                var masterUrl = "<Eq><FieldRef Name='MasterPageUrl'/><Value Type='URL'>" + masterPageURLValue + "</Value></Eq></And>";
                var displayOrder = "<Neq><FieldRef Name='DisplayOrder'/><Value Type='Number'>0</Value></Neq>";
                matchingThemeQuery.Query = "<Where><And>" + themeURLQuery + masterUrl + displayOrder + "</And></Where>";
                matchingThemeQuery.ViewFields = "<FieldRef Name='Name'/>";
                matchingThemeQuery.ViewFieldsOnly = true;
                var matchingThemeItems = designGallery.GetItems(matchingThemeQuery);
                return matchingThemeItems;
            }
    
            /// <summary>
            /// Method to query the design catalog and get the current theme item
            /// </summary>
            /// <param name="designGallery">Design gallery</param>
            /// <returns>Current Theme</returns>
            private static SPListItemCollection GetCurrentTheme(SPList designGallery)
            {
                var currentTheme = new SPQuery();
                currentTheme.RowLimit = 1;
                currentTheme.Query = "<Where><Eq><FieldRef Name='DisplayOrder'/><Value Type='Number'>0</Value></Eq></Where>";
                currentTheme.ViewFields = "<FieldRef Name='ThemeUrl'/><FieldRef Name='MasterPageUrl'/>";
                currentTheme.ViewFieldsOnly = true;
                var currentItems = designGallery.GetItems(currentTheme);
                return currentItems;
            }
    
            /// <summary>
            /// Method to get theme and master page URL in the format of array
            /// </summary>
            /// <param name="columnName">Column Name of the list item</param>
            /// <param name="item">List item</param>
            /// <returns>Array with URL and its description value</returns>
            private static string[] GetArrayValue(string columnName, SPItem item)
            {
                return (item[columnName] != null) ? item[columnName].ToString().Split(',') : new string[0];
            }
            static void Main(string[] args)
            {
                using (SPSite site = new SPSite("http://sp2013/sites/team"))
                {             
                    using(SPWeb web=site.OpenWeb())
                    {
                        var designGallery = web.GetCatalog(SPListTemplateType.DesignCatalog);
                        var CurrentThemeItems = GetCurrentTheme(designGallery);
                        if (CurrentThemeItems != null && CurrentThemeItems.Count > 0)
                        {
                            foreach (SPItem item in CurrentThemeItems)
                            {
                                var themeURLArray = GetArrayValue("ThemeUrl", item);
                                var masterPageURLArray = GetArrayValue("MasterPageUrl", item);
                                if (themeURLArray.Length > 1 && masterPageURLArray.Length > 1)
                                {
                                    var themeURLValue = themeURLArray[1].Trim();
                                    var masterPageURLValue = masterPageURLArray[1].Trim();
                                    var AppliedThemes = GetAppliedThemeName(designGallery, themeURLValue, masterPageURLValue);
                                    if (AppliedThemes != null && AppliedThemes.Count > 0)
                                    {
                                        var name = AppliedThemes[0]["Name"];
                                        if (name != null)
                                        {                                       
                                            Console.WriteLine("CurrentThemeName:{0}",name);
                                        }
                                    }
                                }
                            }
                        }
                    }              
                }          
            }
        }
    }
    

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, January 12, 2017 8:05 AM
    Moderator
  • Hi Romeo,

    yeah, I'm getting all the existing themes using ($spsite.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::DesignCatalog)) 

    But my requirement is to get current applied theme for all sites of all site collections in webapplication. As GetThemeUrlForWeb is not working, I may have to query the designcatalog output to get current theme for each site. But won't it impact the performance of script?


    Thursday, January 12, 2017 10:06 AM
  • Hi Dennis,

    Thank you for the reply. Actually I need to do this in PowerShell script for all sites in webapplication. As  [Microsoft.SharePoint.Utilities.ThmxTheme]::GetThemeUrlForWeb($spWeb.RootWeb) is not returning any value, I may have to go with querying designcatalog. I'm afraid it will impact the performance of query. Have you ever tried to get current theme directly using GetThemeUrlForWeb?

    Thursday, January 12, 2017 10:23 AM
  • Hi

    this script will give you the name of the current theme applied to the specified site

    To get for all sites, you include a little loop to parse all websites in a sitecollection

    $spweb1 = get-spsite -Identity http://wssp2013
    $spList = $spWeb1.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::DesignCatalog);
    
    
    $SPQuery =  New-Object Microsoft.SharePoint.SPQuery;
           $SPQuery.Query = "<Where><Eq><FieldRef Name='DisplayOrder'/><Value Type='Number'>0</Value></Eq></Where>";
           $SPQuery.RowLimit = 1;
           $spListItems = $spList.GetItems($SPQuery);
           $Current=$spListItems[0]
    
    
    $spList = $spWeb1.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::DesignCatalog);
    foreach ( $item in $spList.Items){
    #$item['DisplayOrder']
    #$item['ows_ThemeUrl']
    if(($item['ows_ThemeUrl'] -match $current['ows_ThemeUrl']) -and !($item['Name'] -match 'Current')){
    
    write-host " The current theme name is "$item.Name
    
    }
    }
    Let me know that is helpful for you


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.


    • Edited by romeo donca Thursday, January 12, 2017 7:34 PM
    • Marked as answer by Pavitra g Friday, January 13, 2017 5:35 AM
    Thursday, January 12, 2017 7:33 PM
  • Thank you for the script Romeo. yeah will loop through all sites n site collection and try to get the data
    Friday, January 13, 2017 5:36 AM