如何在sharepoint 2013中获取当前主题的名称

时间:2013-06-06 19:29:42

标签: sharepoint sharepoint-2013

我试图将当前主题应用于该网站。我厌倦了不同的方式来获得它,但没有运气。

1)SPTheme theme1 = SPTheme.OpenAppliedTheme(SPContext.Current.Web);

2)string theme = SPContext.Current.Web.Theme;(Obsolute)

3)using (SPSite site = new SPSite("http://example.com/"))

using (SPWeb web = site.RootWeb)

using (ThmxTheme theme = ThmxTheme.Open(site, ThmxTheme.GetThemeUrlForWeb(web))) { theme.Name = "My Theme"; }

有没有办法得到这个名字?

2 个答案:

答案 0 :(得分:0)

从网站获取主题集:

ReadOnlyCollection<ThmxTheme> themes = ThmxTheme.GetManagedThemes(site);

foreach (ThmxTheme theme in themes)
{
    if (theme.Name == themeName)
    {
        //Do your stuff here
    }
}

获取当前主题

string themeUrl = ThmxTheme.GetThemeUrlForWeb(web);
if (!string.IsNullOrEmpty(themeUrl))
{
    ThmxTheme currentTheme = ThmxTheme.Open(web.Site, themeUrl);
    string themeName = currentTheme.Name;
}

答案 1 :(得分:0)

正如您所说,以上所有API都没有给出当前的主题名称值,因此我从设计目录中获取此值。

您可以尝试以下代码,获取主题名称并告诉我这是否适合您,或者我们还有其他更好的方法来实现相同目标。

    /// <summary>
    /// Method to get the theme from desing catalog
    /// </summary>
    /// <returns>Theme name</returns>
    private static string GetThemeFromDesignCatalog()
    {
        var web = SPContext.Current.Web;
        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)
                        {
                            return name.ToString().Trim();
                        }
                    }
                }
            }
        }

        return string.Empty;
    }

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

此致 卡莱。