More in this section

Forums / Developing with Sitefinity / Programmatically get active theme path?

Programmatically get active theme path?

3 posts, 1 answered
  1. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    21 Feb 2013
    Link to this post

    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?

  2. Jochem Bökkers
    Jochem Bökkers avatar
    787 posts
    Registered:
    13 Aug 2007
    21 Feb 2013 in reply to Basem
    Link to this post

    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.

    Answered
  3. Basem
    Basem avatar
    131 posts
    Registered:
    22 Dec 2010
    25 Feb 2013 in reply to Jochem Bökkers
    Link to this post

    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;
    }

3 posts, 1 answered