예제 #1
0
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                web.IsMultilingual = true;
                web.Context.Load(web, w => w.SupportedUILanguageIds);
                web.Update();
                web.Context.ExecuteQueryRetry();

                var isDirty = false;

                foreach (var id in web.SupportedUILanguageIds)
                {
                    var found = template.SupportedUILanguages.Any(sl => sl.LCID == id);

                    if (!found)
                    {
                        web.RemoveSupportedUILanguage(id);
                        isDirty = true;
                    }
                }
                if (isDirty)
                {
                    web.Update();
                    web.Context.ExecuteQueryRetry();
                }

                foreach (var id in template.SupportedUILanguages)
                {
                    web.AddSupportedUILanguage(id.LCID);
                }
                web.Update();
                web.Context.Load(web, w => w.SupportedUILanguageIds);
                web.Context.ExecuteQueryRetry();
            }

            return(parser);
        }
        public override TokenParser ProvisionObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateApplyingInformation applyingInformation)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                web.IsMultilingual = true;
                web.Context.Load(web, w => w.SupportedUILanguageIds);
                web.Update();
                web.Context.ExecuteQuery();

                var isDirty = false;

                foreach (var id in web.SupportedUILanguageIds)
                {
                    var found = template.SupportedUILanguages.Any(sl => sl.LCID == id);

                    if (!found)
                    {
                        web.RemoveSupportedUILanguage(id);
                        isDirty = true;
                    }
                }
                if (isDirty)
                {
                    web.Update();
                    web.Context.ExecuteQueryRetry();
                }

                foreach (var id in template.SupportedUILanguages)
                {
                    web.AddSupportedUILanguage(id.LCID);
                }
                web.Update();
                web.Context.ExecuteQueryRetry();
            }

            return parser;
        }
예제 #3
0
        private static void EnsureWebLanguages(Web web, ProvisioningTemplate template, PnPMonitoredScope scope)
        {
            List <int> neededLanguages      = new List <int>();
            int        neededSourceLanguage = 0;

            foreach (var page in template.ClientSidePages.Where(p => p.Translations.Any()))
            {
                if (neededSourceLanguage == 0)
                {
                    neededSourceLanguage = page.LCID;
                }
                else
                {
                    // Source language should be the same for all pages in the template
                    if (neededSourceLanguage != page.LCID)
                    {
                        string error = "The pages in this template are based upon multiple source languages while all pages in a site must have the same source language";
                        scope.LogError(error);
                        throw new Exception(error);
                    }
                }

                foreach (var translatedPage in page.Translations)
                {
                    if (!neededLanguages.Contains(translatedPage.LCID))
                    {
                        neededLanguages.Add(translatedPage.LCID);
                    }
                }
            }

            // No translations found, bail out
            if (neededLanguages.Count == 0)
            {
                return;
            }

            try
            {
                // Enable the MUI feature
                web.ActivateFeature(ObjectClientSidePages.MultilingualPagesFeature);
            }
            catch (Exception ex)
            {
                scope.LogError($"Multilingual pages feature could not be enabled: {ex.Message}");
                throw;
            }

            // Check the "source" language
            web.EnsureProperties(p => p.Language, p => p.IsMultilingual);
            int sourceLanguage = Convert.ToInt32(web.Language);

            if (sourceLanguage != neededSourceLanguage)
            {
                string error = $"The web has source language {sourceLanguage} while the template expects {neededSourceLanguage}";
                scope.LogError(error);
                throw new Exception(error);
            }

            // Ensure the needed languages are available on this site
            if (!web.IsMultilingual)
            {
                web.IsMultilingual = true;
                web.Context.Load(web, w => w.SupportedUILanguageIds);
                web.Update();
            }
            else
            {
                web.Context.Load(web, w => w.SupportedUILanguageIds);
            }
            web.Context.ExecuteQueryRetry();

            var  supportedLanguages = web.SupportedUILanguageIds;
            bool languageAdded      = false;

            foreach (var language in neededLanguages)
            {
                if (!supportedLanguages.Contains(language))
                {
                    web.AddSupportedUILanguage(language);
                    languageAdded = true;
                }
            }

            if (languageAdded)
            {
                web.Update();
                web.Context.ExecuteQueryRetry();
            }
        }