Programmatically get active theme path?

Posted by Community Admin on 04-Aug-2018 19:02

Programmatically get active theme path?

All Replies

Posted by Community Admin on 21-Feb-2013 00:00

Is there anything in the API that gives me back the path to the theme directory?

I am hoping to get back something like this: ~/Sitefinity/WebsiteTemplates/FalafelSitefinity/App_Themes/FalafelSitefinity

Anyone know of something built-in for that?

Posted by Community Admin on 21-Feb-2013 00:00

Hey Basem,

var t = pageManager.GetPageData(p.PageId).Template;
var tt = pageManager.GetPageData(p.PageId).Template.Theme;
var tm = t.MasterPage;


Should get you template, masterpage and theme applied.

With something like

ttheme = Config.Get<AppearanceConfig>().FrontendThemes;

you should get all the frontend themes installed in the system. 


If you do something like this:

foreach (Telerik.Sitefinity.Web.Configuration.ThemeElement te in ttheme)

Response.Write(te.Path);
Response.Write(te.Name);
Response.Write(te.TagName);


You'll notice that both name and TagName are similar (at least in my quick mock) to which you need to verify against, I'm not sure...

Jochem.

Posted by Community Admin on 25-Feb-2013 00:00

Thanks a mil Jochem, this is what I ended up with:

/// <summary>
/// Gets the current theme.
/// </summary>
/// <returns></returns>
public static ThemeElement GetCurrentTheme()
    //GET CURRENT TEMPLATE
    var template = new PageManager()
        .GetPageNode(new Guid(SiteMapBase.GetActualCurrentNode().Key))
        .Page
        .Template;
 
    //GET THEME IF APPLICABLE
    if (template != null)
    
        string theme = GetCurrentPage().Page.Template.Theme;
        if (!string.IsNullOrWhiteSpace(theme) && theme != "notheme")
        
            //RETURN THEME BY NAME
            return Config.Get<AppearanceConfig>().FrontendThemes[theme];
        
    
 
    //NO TEMPLATE OF THEME SELECTED
    return null;
 
/// <summary>
/// Gets the current theme path.
/// </summary>
/// <returns></returns>
public static string GetCurrentThemePath()
    //GET CURRENT THEME
    var theme = GetCurrentTheme();
 
    //GET THEME PATH IF APPLIABLE
    if (theme != null)
    
        //GET PATH
        string path = theme.Path;
 
        //GET WEB PATH OR RETURN NAMESPACE
        if (!string.IsNullOrWhiteSpace(path))
        
            //REMOVE APP_DATA FROM CLIENT URL
            return path.StartsWith("~/App_Data", StringComparison.OrdinalIgnoreCase)
                ? VirtualPathUtility.ToAbsolute("~/" + path.Substring(10))
                : path;
        
        else
        
            //TODO: VIRTUAL PATH FOR NAMESPACE?
            return theme.Namespace;
        
    
 
    //NO THEME SELECTED OR FOUND
    return string.Empty;

This thread is closed