Ejemplo n.º 1
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);
        }