public static void SetThemeByUrl(this Web web, string paletteServerRelativeUrl, string fontServerRelativeUrl, string backgroundServerRelativeUrl, bool resetSubsitesToInherit = false, bool updateRootOnly = false)
        {
            var websToUpdate = new List<Web>();
            web.Context.Load(web, w => w.AllProperties, w => w.ServerRelativeUrl);
            web.Context.ExecuteQueryRetry();

            Log.Info(Constants.LOGGING_SOURCE, CoreResources.BrandingExtension_ApplyTheme, paletteServerRelativeUrl, web.ServerRelativeUrl);
            web.AllProperties[InheritTheme] = "False";
            web.Update();
            web.ApplyTheme(paletteServerRelativeUrl, fontServerRelativeUrl, backgroundServerRelativeUrl, shareGenerated: true);
            web.Context.ExecuteQueryRetry();
            //web.Context.Load(web, w => w.ThemedCssFolderUrl);
            //var themedCssFolderUrl = childWeb.ThemedCssFolderUrl;
            websToUpdate.Add(web);

            if (!updateRootOnly)
            {
                var index = 0;
                while (index < websToUpdate.Count)
                {
                    var currentWeb = websToUpdate[index];
                    var websCollection = currentWeb.Webs;
                    web.Context.Load(websCollection, wc => wc.Include(w => w.AllProperties, w => w.ServerRelativeUrl));
                    web.Context.ExecuteQueryRetry();
                    foreach (var childWeb in websCollection)
                    {
                        var inheritThemeProperty = childWeb.GetPropertyBagValueString(InheritTheme, "");
                        bool inheritTheme = false;
                        if (!string.IsNullOrEmpty(inheritThemeProperty))
                        {
                            inheritTheme = string.Equals(childWeb.AllProperties[InheritTheme].ToString(), "True", StringComparison.InvariantCultureIgnoreCase);
                        }

                        if (resetSubsitesToInherit || inheritTheme)
                        {
                            Log.Debug(Constants.LOGGING_SOURCE, "Inherited: " + CoreResources.BrandingExtension_ApplyTheme, paletteServerRelativeUrl, childWeb.ServerRelativeUrl);
                            childWeb.AllProperties[InheritTheme] = "True";
                            //childWeb.ThemedCssFolderUrl = themedCssFolderUrl;
                            childWeb.Update();
                            // TODO: CSOM does not support the ThemedCssFolderUrl property yet (Nov 2014), so must call ApplyTheme at each level.
                            // This is very slow, so replace with simply setting the ThemedCssFolderUrl property instead once available.
                            childWeb.ApplyTheme(paletteServerRelativeUrl, fontServerRelativeUrl, backgroundServerRelativeUrl, shareGenerated: true);
                            web.Context.ExecuteQueryRetry();
                            websToUpdate.Add(childWeb);
                        }
                    }
                    index++;
                }
            }
        }
Example #2
0
        private static void SetThemeToWebImplementation(this Web web, Web rootWeb, string themeName)
        {
            if (rootWeb == null)
                throw new ArgumentNullException("rootWeb");

            if (string.IsNullOrEmpty(themeName))
                throw new ArgumentNullException("themeName");

            LoggingUtility.Internal.TraceInformation((int)EventId.SetTheme, CoreResources.BrandingExtension_SetTheme, themeName, web.Context.Url);

            // Let's get instance to the composite look gallery
            List themeList = rootWeb.GetCatalog((int)ListTemplateType.DesignCatalog);
            rootWeb.Context.Load(themeList);
            LoggingUtility.Internal.TraceVerbose("Getting theme list (catalog 124)");
            rootWeb.Context.ExecuteQuery();

            // Double checking that theme exists
            if (rootWeb.ThemeEntryExists(themeName, themeList))
            {
                // Let's update the theme name accordingly
                CamlQuery query = new CamlQuery();
                // Find the theme by themeName
                string camlString = string.Format(CAML_QUERY_FIND_BY_FILENAME, themeName);
                query.ViewXml = camlString;
                var found = themeList.GetItems(query);
                rootWeb.Context.Load(found);
                LoggingUtility.Internal.TraceVerbose("Getting theme: {0}", themeName);
                rootWeb.Context.ExecuteQuery();
                if (found.Count > 0)
                {
                    ListItem themeEntry = found[0];

                    //Set the properties for applying custom theme which was just uploaded
                    string spColorURL = null;
                    if (themeEntry["ThemeUrl"] != null && themeEntry["ThemeUrl"].ToString().Length > 0)
                    {
                        spColorURL = UrlUtility.MakeRelativeUrl((themeEntry["ThemeUrl"] as FieldUrlValue).Url);
                    }
                    string spFontURL = null;
                    if (themeEntry["FontSchemeUrl"] != null && themeEntry["FontSchemeUrl"].ToString().Length > 0)
                    {
                        spFontURL = UrlUtility.MakeRelativeUrl((themeEntry["FontSchemeUrl"] as FieldUrlValue).Url);
                    }
                    string backGroundImage = null;
                    if (themeEntry["ImageUrl"] != null && themeEntry["ImageUrl"].ToString().Length > 0)
                    {
                        backGroundImage = UrlUtility.MakeRelativeUrl((themeEntry["ImageUrl"] as FieldUrlValue).Url);
                    }

                    LoggingUtility.Internal.TraceVerbose("Apply theme '{0}', '{1}', '{2}'.", spColorURL, spFontURL, backGroundImage);
                    // Set theme for demonstration
                    // TODO: Why is shareGenerated false? If deploying to root an inheriting, then maybe use shareGenerated = true.
                    web.ApplyTheme(spColorURL,
                                        spFontURL,
                                        backGroundImage,
                                        false);
                    web.Context.ExecuteQuery();
                    LoggingUtility.Internal.TraceVerbose("Theme applied");

                    // Let's also update master page, if needed
                    if (themeEntry["MasterPageUrl"] != null && themeEntry["MasterPageUrl"].ToString().Length > 0)
                    {
                        var masterUrl = UrlUtility.MakeRelativeUrl((themeEntry["MasterPageUrl"] as FieldUrlValue).Url);

                        web.SetMasterPageForSiteByUrl(masterUrl);
                        web.SetCustomMasterPageForSiteByUrl(masterUrl);
                    }
                }
                else
                {
                    LoggingUtility.Internal.TraceError((int)EventId.ThemeMissing, CoreResources.BrandingExtension_ThemeMissing, themeName);
                }
            }
            else
            {
                LoggingUtility.Internal.TraceError((int)EventId.ThemeMissing, CoreResources.BrandingExtension_ThemeMissing, themeName);
            }
        }
Example #3
0
        private static void SetThemeToWebImplementation(this Web web, Web rootWeb, string themeName)
        {
            LoggingUtility.Internal.TraceInformation((int)EventId.SetTheme, "Setting theme '{0}' for '{1}'", themeName, web.Context.Url);

            // Let's get instance to the composite look gallery
            List themeList = rootWeb.GetCatalog(124);
            rootWeb.Context.Load(themeList);
            LoggingUtility.Internal.TraceVerbose("Getting theme list (catalog 124)");
            rootWeb.Context.ExecuteQuery();

            // Double checking that theme exists
            if (rootWeb.ThemeEntryExists(themeName, themeList))
            {
                CamlQuery query = new CamlQuery();
                string camlString = @"
                <View>
                    <Query>
                        <Where>
                            <Eq>
                                <FieldRef Name='Name' />
                                <Value Type='Text'>{0}</Value>
                            </Eq>
                        </Where>
                     </Query>
                </View>";

                // Let's update the theme name accordingly
                camlString = string.Format(camlString, themeName);
                query.ViewXml = camlString;
                var found = themeList.GetItems(query);
                rootWeb.Context.Load(found);
                LoggingUtility.Internal.TraceVerbose("Getting theme");
                rootWeb.Context.ExecuteQuery();
                if (found.Count > 0)
                {
                    ListItem themeEntry = found[0];
                    //Set the properties for applying custom theme which was just uploaded
                    string spColorURL = null;
                    if (themeEntry["ThemeUrl"] != null && themeEntry["ThemeUrl"].ToString().Length > 0)
                    {
                        spColorURL = UrlUtility.MakeRelativeUrl((themeEntry["ThemeUrl"] as FieldUrlValue).Url);
                    }
                    string spFontURL = null;
                    if (themeEntry["FontSchemeUrl"] != null && themeEntry["FontSchemeUrl"].ToString().Length > 0)
                    {
                        spFontURL = UrlUtility.MakeRelativeUrl((themeEntry["FontSchemeUrl"] as FieldUrlValue).Url);
                    }
                    string backGroundImage = null;
                    if (themeEntry["ImageUrl"] != null && themeEntry["ImageUrl"].ToString().Length > 0)
                    {
                        backGroundImage = UrlUtility.MakeRelativeUrl((themeEntry["ImageUrl"] as FieldUrlValue).Url);
                    }

                    LoggingUtility.Internal.TraceVerbose("Apply theme '{0}', '{1}', '{2}'.", spColorURL, spFontURL, backGroundImage);
                    // Set theme for demonstration
                    // TODO: Why is shareGenerated false? If deploying to root an inheriting, then maybe use shareGenerated = true.
                    web.ApplyTheme(spColorURL,
                                        spFontURL,
                                        backGroundImage,
                                        false);
                    web.Context.ExecuteQuery();
                    LoggingUtility.Internal.TraceVerbose("Theme applied");

                    // Let's also update master page, if needed
                    if (themeEntry["MasterPageUrl"] != null && themeEntry["MasterPageUrl"].ToString().Length > 0)
                    {
                        var masterUrl = UrlUtility.MakeRelativeUrl((themeEntry["MasterPageUrl"] as FieldUrlValue).Url);

                        web.SetMasterPageForSiteByUrl(masterUrl);
                        web.SetCustomMasterPageForSiteByUrl(masterUrl);
                    }
                }
                else
                {
                    LoggingUtility.Internal.TraceError((int)EventId.ThemeMissing, "Theme '{0}' not found.", themeName);
                }
            }
            else
            {
                LoggingUtility.Internal.TraceError((int)EventId.ThemeMissing, "Theme '{0}' does not exist.", themeName);
            }
        }
Example #4
0
        /// <summary>
        /// FunciĆ³n para aplicar temas
        /// ejemplo de variables que debemos de utilizar 
        /// var composedLookName = "Sareb";
        ///var paletteUrl = "_catalogs/theme/15/sareb.spcolor";
        ///var fontSchemeUrl = "_catalogs/theme/15/sarebfont.spfont";
        /// </summary>
        /// <param name="web">Objeto SPNmame</param>
        /// <param name="composedLookName"> Nombre del ComposedName</param>
        /// <param name="paletteUrl">Url donde esta la paleta</param>
        /// <param name="fontSchemeUrl"> Url donde esta la fuente</param>
        /// <returns></returns>
        public static bool ApplyTheme(this SPWeb web, string composedLookName, string paletteUrl, string fontSchemeUrl)
        {
            var result = true;
            try
            {
                var serverRelativeUrl = web.ServerRelativeUrl;
                if (!serverRelativeUrl.EndsWith("/"))
                {
                    serverRelativeUrl = string.Concat(serverRelativeUrl, "/");
                }
                var rootRealiveUrl = web.Site.RootWeb.ServerRelativeUrl;
                if (!rootRealiveUrl.EndsWith("/"))
                {
                    rootRealiveUrl = string.Concat(rootRealiveUrl, "/");
                }
                var masterPageUrl = web.MasterUrl;
                var list = web.GetCatalog(SPListTemplateType.DesignCatalog);
                var query = new SPQuery
                {
                    Query =
                        string.Format(
                            @"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>",
                            composedLookName),
                    RowLimit = 10,
                    ViewAttributes = "Scope=\"Recursive\""
                };
                var found = list.GetItems(query);
                if (found.Count == 0)
                {
                    var item = list.AddItem();

                    item["Title"] = composedLookName;
                    item["Name"] = composedLookName;

                    var masterUrl = new SPFieldUrlValue
                    {
                        Url = string.Concat(serverRelativeUrl, masterPageUrl),
                        Description = string.Concat(serverRelativeUrl, masterPageUrl)
                    };
                    item["MasterPageUrl"] = masterUrl;

                    var themeUrl = new SPFieldUrlValue
                    {
                        Url = string.Concat(rootRealiveUrl, paletteUrl),
                        Description = string.Concat(rootRealiveUrl, paletteUrl)
                    };
                    item["ThemeUrl"] = themeUrl;

                    var imageUrl = new SPFieldUrlValue { Url = string.Empty, Description = string.Empty };
                    item["ImageUrl"] = imageUrl;

                    var fieldFontSchemeUrl = new SPFieldUrlValue { Url = fontSchemeUrl, Description = string.Empty };
                    item["FontSchemeUrl"] = string.Concat(rootRealiveUrl, fieldFontSchemeUrl);

                    item["DisplayOrder"] = 1;
                    item.Update();
                }
                web.ApplyTheme(string.Concat(rootRealiveUrl, paletteUrl), string.Concat(rootRealiveUrl, fontSchemeUrl),
                    null, true);
            }
            catch (Exception exception)
            {
                result = false;
                Logger.Error(string.Concat("Error ApplyTheme", exception.Message));
            }
            return result;
        }
Example #5
0
        private static void SetThemeToWebImplementation(this Web web, Web rootWeb, string themeName)
        {
            // Let's get instance to the composite look gallery
            List themeList = rootWeb.GetCatalog(124);
            rootWeb.Context.Load(themeList);
            rootWeb.Context.ExecuteQuery();

            // Double checking that theme exists
            if (rootWeb.ThemeEntryExists(themeName, themeList))
            {
                CamlQuery query = new CamlQuery();
                string camlString = @"
                <View>
                    <Query>
                        <Where>
                            <Eq>
                                <FieldRef Name='Name' />
                                <Value Type='Text'>{0}</Value>
                            </Eq>
                        </Where>
                     </Query>
                </View>";

                // Let's update the theme name accordingly
                camlString = string.Format(camlString, themeName);
                query.ViewXml = camlString;
                var found = themeList.GetItems(query);
                rootWeb.Context.Load(found);
                rootWeb.Context.ExecuteQuery();
                if (found.Count > 0)
                {
                    ListItem themeEntry = found[0];
                    //Set the properties for applying custom theme which was jus uplaoded
                    string spColorURL = null;
                    if (themeEntry["ThemeUrl"] != null && themeEntry["ThemeUrl"].ToString().Length > 0)
                    {
                        spColorURL = UrlUtility.MakeRelativeUrl((themeEntry["ThemeUrl"] as FieldUrlValue).Url);
                    }
                    string spFontURL = null;
                    if (themeEntry["FontSchemeUrl"] != null && themeEntry["FontSchemeUrl"].ToString().Length > 0)
                    {
                        spFontURL = UrlUtility.MakeRelativeUrl((themeEntry["FontSchemeUrl"] as FieldUrlValue).Url);
                    }
                    string backGroundImage = null;
                    if (themeEntry["ImageUrl"] != null && themeEntry["ImageUrl"].ToString().Length > 0)
                    {
                        backGroundImage = UrlUtility.MakeRelativeUrl((themeEntry["ImageUrl"] as FieldUrlValue).Url);
                    }

                    // Set theme for demonstration
                    web.ApplyTheme(spColorURL,
                                        spFontURL,
                                        backGroundImage,
                                        false);

                    // Let's also update master page, if needed
                    if (themeEntry["MasterPageUrl"] != null && themeEntry["MasterPageUrl"].ToString().Length > 0)
                    {
                        web.MasterUrl = UrlUtility.MakeRelativeUrl((themeEntry["MasterPageUrl"] as FieldUrlValue).Url); ;
                    }

                    web.Context.ExecuteQuery();
                }
            }
        }