Thursday 22 May 2014

Hide Site templates when a new site is created by using web feature receiver..

In order to hide certain templates when we create a new site, we need to create a event receiver for web

Following is the code to accomplish this.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = null;
            try
            {
                //get the SPWeb object from the feature properties as it returns SPWeb object because the scope specified is Web
                web = (SPWeb)properties.Feature.Parent;
                if (web != null)
                {
                    HideTemplates(web);
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (web != null)
                {
                    web.Dispose();
                }
            }
        }
        # endregion
        # region Helper Methods
        /// <summary>
        /// This method reads the corresponding web.config files appsetting entries and
        /// excludes the specified site templates from the current web site
        /// </summary>
        /// <param name="rootWeb">SPWeb</param>
        private void HideTemplates(SPWeb rootWeb)
        {
            //Initialize the variables
            string configFilePath = string.Empty;
            XmlDocument webConfigDoc = null;
            string appSettingVal = string.Empty;
            string xpath = string.Empty;
            Collection<SPWebTemplate> availableTemplates = null;
            SPWebTemplateCollection webTemplateCollection = null;
           
            try
            {
                configFilePath = rootWeb.Site.WebApplication.GetIisSettingsWithFallback(Microsoft.SharePoint.Administration.SPUrlZone.Default).Path.FullName.ToString() + "\\web.config";
                webConfigDoc = new XmlDocument();
                webConfigDoc.Load(configFilePath);
                availableTemplates = new Collection<SPWebTemplate>();
                webTemplateCollection = rootWeb.GetAvailableWebTemplates(rootWeb.Language);
                bool IsHideTemplate = false;

                foreach (SPWebTemplate template in webTemplateCollection)
                {
                    //Reading web.config attributes
                    xpath = "configuration/appSettings/add[@key='" + template.Title + "']";
                    XmlNode appSetting = webConfigDoc.SelectSingleNode(xpath);

                    if (appSetting != null)
                    {
                        IsHideTemplate = true;
                        //gets the value for the key equal to template.Title from appsettings section.
                        appSettingVal = appSetting.Attributes["value"].Value;
                    }

                    if ((appSettingVal != "") && (template.Title == appSettingVal))
                    {
                        //Excluding the site template which is read from web.config file
                    }
                    else
                    {
                        availableTemplates.Add(template);
                    }
                }

                //Update only if web.config appsetting entry for site templates not empty
                if (IsHideTemplate)
                {
                    //Set new site template collection to current web available templates
                    rootWeb.SetAvailableCrossLanguageWebTemplates(availableTemplates);
                    rootWeb.Update();
                }
            }
            catch (Exception ex)
            {
             }
            finally
            {
                if (rootWeb != null)
                {
                    rootWeb.Dispose();
                }
            }
        }
        # endregion

No comments:

Post a Comment