private static void ProcessWebTemplateSettings(PublishingWeb publishingWeb, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = publishingWeb.Web;

            if (definition.InheritWebTemplates.HasValue && definition.InheritWebTemplates.Value)
            {
                publishingWeb.InheritAvailableWebTemplates();
            }
            else if (definition.UseAnyWebTemplate.HasValue && definition.UseAnyWebTemplate.Value)
            {
                publishingWeb.AllowAllWebTemplates(definition.ResetAllSubsitesToInheritWebTemplates.HasValue
                    ? definition.ResetAllSubsitesToInheritWebTemplates.Value
                    : false);
            }
            else if (definition.UseDefinedWebTemplates.HasValue && definition.UseDefinedWebTemplates.Value)
            {
                var currentLocaleId = (uint)web.CurrencyLocaleID;
                var webTemplates    = new List <SPWebTemplate>();

                webTemplates.AddRange(web.Site.GetWebTemplates(currentLocaleId).OfType <SPWebTemplate>());
                webTemplates.AddRange(web.Site.GetCustomWebTemplates(currentLocaleId).OfType <SPWebTemplate>());

                var selectedWebTemplates = new Collection <SPWebTemplate>();

                foreach (var selectedWebTemplateName in definition.DefinedWebTemplates)
                {
                    var targetWebTemplate =
                        webTemplates.FirstOrDefault(t => t.Name.ToUpper() == selectedWebTemplateName.ToUpper());

                    if (targetWebTemplate != null)
                    {
                        selectedWebTemplates.Add(targetWebTemplate);
                    }
                }

                if (selectedWebTemplates.Any())
                {
                    publishingWeb.SetAvailableWebTemplates(selectedWebTemplates, 0,
                                                           definition.ResetAllSubsitesToInheritWebTemplates.HasValue
                            ? definition.ResetAllSubsitesToInheritWebTemplates.Value
                            : false);
                }
            }
        }
        private static void ProcessWebTemplateSettings(PublishingWeb publishingWeb, PageLayoutAndSiteTemplateSettingsDefinition definition)
        {
            var web = publishingWeb.Web;

            if (definition.InheritWebTemplates.HasValue && definition.InheritWebTemplates.Value)
                publishingWeb.InheritAvailableWebTemplates();
            else if (definition.UseAnyWebTemplate.HasValue && definition.UseAnyWebTemplate.Value)
            {
                publishingWeb.AllowAllWebTemplates(definition.ResetAllSubsitesToInheritWebTemplates.HasValue
                    ? definition.ResetAllSubsitesToInheritWebTemplates.Value
                    : false);
            }
            else if (definition.UseDefinedWebTemplates.HasValue && definition.UseDefinedWebTemplates.Value)
            {
                var currentLocaleId = (uint)web.CurrencyLocaleID;
                var webTemplates = new List<SPWebTemplate>();

                webTemplates.AddRange(web.Site.GetWebTemplates(currentLocaleId).OfType<SPWebTemplate>());
                webTemplates.AddRange(web.Site.GetCustomWebTemplates(currentLocaleId).OfType<SPWebTemplate>());

                var selectedWebTemplates = new Collection<SPWebTemplate>();

                foreach (var selectedWebTemplateName in definition.DefinedWebTemplates)
                {
                    var targetWebTemplate =
                        webTemplates.FirstOrDefault(t => t.Name.ToUpper() == selectedWebTemplateName.ToUpper());

                    if (targetWebTemplate != null)
                        selectedWebTemplates.Add(targetWebTemplate);
                }

                if (selectedWebTemplates.Any())
                {
                    publishingWeb.SetAvailableWebTemplates(selectedWebTemplates, 0,
                        definition.ResetAllSubsitesToInheritWebTemplates.HasValue
                            ? definition.ResetAllSubsitesToInheritWebTemplates.Value
                            : false);
                }
            }
        }
Example #3
0
        /// <summary>
        /// Runs the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="keyValues">The key values.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public override int Execute(string command, StringDictionary keyValues, out string output)
        {
            output = string.Empty;



            SPBinaryParameterValidator.Validate("template", Params["template"].Value, "allowalltemplates",
                                                (Params["allowalltemplates"].UserTypedIn ? "true" : Params["allowalltemplates"].Value));

            string url              = Params["url"].Value.TrimEnd('/');
            string templateName     = Params["template"].Value;
            bool   resetAllSubsites = Params["resetallsubsites"].UserTypedIn;

            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
                {
                    uint lcid           = web.Language;
                    bool localeProvided = Params["lcid"].UserTypedIn;
                    if (localeProvided)
                    {
                        lcid = uint.Parse(Params["lcid"].Value);
                    }
                    bool exists = false;

#if MOSS
                    PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(web);

                    if (Params["allowalltemplates"].UserTypedIn)
                    {
                        pubweb.AllowAllWebTemplates(resetAllSubsites);
                        pubweb.Update();
                        return((int)ErrorCodes.NoError);
                    }
                    SPWebTemplateCollection existingLanguageNeutralTemplatesCollection  = pubweb.GetAvailableCrossLanguageWebTemplates();
                    SPWebTemplateCollection existingLanguageSpecificTemplatesCollection = pubweb.GetAvailableWebTemplates(lcid);
#else
                    if (Params["allowalltemplates"].UserTypedIn)
                    {
                        web.AllowAllWebTemplates();
                        web.Update();
                        return((int)ErrorCodes.NoError);
                    }
                    SPWebTemplateCollection existingLanguageNeutralTemplatesCollection  = web.GetAvailableCrossLanguageWebTemplates();
                    SPWebTemplateCollection existingLanguageSpecificTemplatesCollection = web.GetAvailableWebTemplates(lcid);
#endif


                    Collection <SPWebTemplate> newLanguageNeutralTemplatesCollection  = new Collection <SPWebTemplate>();
                    Collection <SPWebTemplate> newLanguageSpecificTemplatesCollection = new Collection <SPWebTemplate>();

                    foreach (SPWebTemplate existingTemplate in existingLanguageNeutralTemplatesCollection)
                    {
                        if (existingTemplate.Name == templateName && !localeProvided)
                        {
                            exists = true;
                            continue;
                        }
                        newLanguageNeutralTemplatesCollection.Add(existingTemplate);
                    }
                    foreach (SPWebTemplate existingTemplate in existingLanguageSpecificTemplatesCollection)
                    {
                        if (existingTemplate.Name == templateName && localeProvided)
                        {
                            exists = true;
                            continue;
                        }
                        newLanguageSpecificTemplatesCollection.Add(existingTemplate);
                    }


                    if (!exists)
                    {
                        output = "Template is not assigned.";
                        return((int)ErrorCodes.GeneralError);
                    }

                    if (newLanguageSpecificTemplatesCollection.Count == 0 && newLanguageNeutralTemplatesCollection.Count == 0)
                    {
                        output = "There must be at least one template available.";
                        return((int)ErrorCodes.GeneralError);
                    }
#if MOSS
                    pubweb.SetAvailableCrossLanguageWebTemplates(newLanguageNeutralTemplatesCollection, resetAllSubsites);
                    pubweb.SetAvailableWebTemplates(newLanguageSpecificTemplatesCollection, lcid, resetAllSubsites);
#else
                    web.SetAvailableCrossLanguageWebTemplates(newLanguageNeutralTemplatesCollection);
                    web.SetAvailableWebTemplates(newLanguageSpecificTemplatesCollection, lcid);
#endif
                }
            }

            return((int)ErrorCodes.NoError);
        }
        /// <summary>
        /// Runs the specified command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="keyValues">The key values.</param>
        /// <param name="output">The output.</param>
        /// <returns></returns>
        public override int Execute(string command, StringDictionary keyValues, out string output)
        {
            output = string.Empty;



            SPBinaryParameterValidator.Validate("template", Params["template"].Value, "allowalltemplates",
                                                (Params["allowalltemplates"].UserTypedIn ? "true" : Params["allowalltemplates"].Value));

            string url              = Params["url"].Value.TrimEnd('/');
            string templateName     = Params["template"].Value;
            bool   resetAllSubsites = Params["resetallsubsites"].UserTypedIn;

            using (SPSite site = new SPSite(url))
            {
                using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
                {
                    uint lcid = web.Language;
                    if (!string.IsNullOrEmpty(keyValues["lcid"]))
                    {
                        lcid = uint.Parse(keyValues["lcid"]);
                    }
                    bool localeProvided = keyValues.ContainsKey("lcid");

#if MOSS
                    PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(web);

                    if (Params["allowalltemplates"].UserTypedIn)
                    {
                        pubweb.AllowAllWebTemplates(resetAllSubsites);
                        pubweb.Update();
                        return((int)ErrorCodes.NoError);
                    }
#else
                    if (Params["allowalltemplates"].UserTypedIn)
                    {
                        web.AllowAllWebTemplates();
                        web.Update();
                        return((int)ErrorCodes.NoError);
                    }
#endif
                    SPWebTemplateCollection templateColl;
                    if (localeProvided)
                    {
                        templateColl = web.GetAvailableWebTemplates(lcid);
                    }
                    else
                    {
                        templateColl = web.GetAvailableCrossLanguageWebTemplates();
                    }

                    bool exists;
                    try
                    {
                        exists = (templateColl[templateName] != null);
                    }
                    catch (ArgumentException)
                    {
                        exists = false;
                    }
                    if (exists && !web.AllWebTemplatesAllowed)
                    {
                        output = "Template is already installed.";
                        return((int)ErrorCodes.GeneralError);
                    }

                    Collection <SPWebTemplate> list = new Collection <SPWebTemplate>();
                    if (!web.AllWebTemplatesAllowed)
                    {
                        foreach (SPWebTemplate existingTemplate in templateColl)
                        {
                            list.Add(existingTemplate);
                        }
                    }
                    SPWebTemplate newTemplate = GetWebTemplate(site, lcid, templateName);
                    if (newTemplate == null)
                    {
                        output = "Template not found.";
                        return((int)ErrorCodes.GeneralError);
                    }
                    else
                    {
                        list.Add(newTemplate);
                    }

#if MOSS
                    if (!localeProvided)
                    {
                        pubweb.SetAvailableCrossLanguageWebTemplates(list, resetAllSubsites);
                    }
                    else
                    {
                        pubweb.SetAvailableWebTemplates(list, lcid, resetAllSubsites);
                    }
#else
                    if (!localeProvided)
                    {
                        web.SetAvailableCrossLanguageWebTemplates(list);
                    }
                    else
                    {
                        web.SetAvailableWebTemplates(list, lcid);
                    }
#endif
                }
            }

            return((int)ErrorCodes.NoError);
        }