Tuesday, August 24, 2010

SharePoint - Changing Web.Config for one time settings

During SharePoint deployments, it may be necessary for us to do one time web.config changes. SharePoint Object Model can be used during FeatureActivated event to achieve this. Please note that, this code just writes to the web.config but does not remove the entries on FeatureDeactivating event.

private void ChangeWebConfigurations(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;
    SPWebApplication webApp = site.WebApplication;
    SPWebConfigModification myModification;

    #region configuration/configSections

    //Include section for configuration/configSections[system.web.extensions]
    myModification = new SPWebConfigModification("sectionGroup[@name='system.web.extensions']", "configuration/configSections");
    myModification.Value = "";
    myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    myModification.Sequence = 0;
    webApp.WebConfigModifications.Add(myModification);

    //Include section for configuration/configSections/system.web.extensions/scripting
    myModification = new SPWebConfigModification("sectionGroup[@name='scripting']", "configuration/configSections/sectionGroup[@name='system.web.extensions']");
    myModification.Value = "";
    myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    myModification.Sequence = 0;
    webApp.WebConfigModifications.Add(myModification);

    //Include section for configuration/configSections/system.web.extensions/scripting/scriptResourceHandler
    myModification = new SPWebConfigModification("sectionGroup[@name='scriptResourceHandler']", "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']");
    myModification.Value = "
"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //Include section for configuration/configSections/system.web.extensions/scripting/webServices myModification = new SPWebConfigModification("sectionGroup[@name='webServices']", "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //Include section for configuration/configSections/system.web.extensions/scripting/jsonSerialization myModification = new SPWebConfigModification("sectionGroup[@name='jsonSerialization']", "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/sectionGroup[@name='webServices']"); myModification.Value = "
"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //Include section for configuration/configSections/system.web.extensions/scripting/profileService myModification = new SPWebConfigModification("sectionGroup[@name='profileService']", "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/sectionGroup[@name='webServices']"); myModification.Value = "
"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //Include section for configuration/configSections/system.web.extensions/scripting/authenticationService myModification = new SPWebConfigModification("sectionGroup[@name='authenticationService']", "configuration/configSections/sectionGroup[@name='system.web.extensions']/sectionGroup[@name='scripting']/sectionGroup[@name='webServices']"); myModification.Value = "
"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); #endregion #region system.web #region httpHanders //Include section for configuration/System.Web/httpHandlers/*.asmx myModification = new SPWebConfigModification("add [@path='*.asmx']", "configuration/system.web/httpHandlers"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //Include section for configuration/System.Web/httpHandlers/*_AppService.axd myModification = new SPWebConfigModification("add [@path='*_AppService.axd']", "configuration/system.web/httpHandlers"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //Include section for configuration/System.Web/httpHandlers/ScriptResource.axd myModification = new SPWebConfigModification("add [@path='ScriptResource.axd']", "configuration/system.web/httpHandlers"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); #endregion //system.web/httpHandlers #region httpModules //Include section for configuration/System.Web/httpModules/ScriptModule myModification = new SPWebConfigModification("add [@name='ScriptModule']", "configuration/system.web/httpModules"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //system.web/httpModules #endregion #region compilation //Include section for configuration/System.Web/compilation/assemblies myModification = new SPWebConfigModification("add[@assembly='System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']", "configuration/system.web/compilation/assemblies"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //Include section for configuration/System.Web/compilation/assemblies myModification = new SPWebConfigModification("add[@assembly='Microsoft.Office.Server, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c']", "configuration/system.web/compilation/assemblies"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); #endregion //system.web/compilation/assemblies #region controls //Make sure controls section is available myModification = new SPWebConfigModification("controls", "configuration/system.web/pages"); myModification.Value = "controls"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); //Include section for configuration/System.Web/pages/controls myModification = new SPWebConfigModification("add[@tagPrefix='asp'][@assembly='System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35']", "configuration/system.web/pages/controls"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Sequence = 0; webApp.WebConfigModifications.Add(myModification); #endregion //system.web/pages/controls #endregion //system.web #region SafeControls //Include safe control myModification = new SPWebConfigModification(); myModification.Path = "configuration/SharePoint/SafeControls"; myModification.Name = "SafeControl"; myModification.Sequence = 0; //myModification.Owner = this WebConfigModificationFeatureReceiver.OwnerId; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; myModification.Value = ""; #endregion #region system.webServer //create system.webServer section myModification = new SPWebConfigModification("system.webServer", "configuration"); myModification.Value = "system.webServer"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection; webApp.WebConfigModifications.Add(myModification); //Include validation myModification = new SPWebConfigModification("validation[@validateIntegratedModeConfiguration='false']", "configuration/system.webServer"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; //Create modules section myModification = new SPWebConfigModification("modules", "configuration/system.webServer"); myModification.Value = "modules"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection; webApp.WebConfigModifications.Add(myModification); //Create modules element myModification = new SPWebConfigModification("ScriptModule", "configuration/system.webServer/modules"); myModification.Value = ""; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; webApp.WebConfigModifications.Add(myModification); //Create handers section myModification = new SPWebConfigModification("handlers", "configuration/system.webServer"); myModification.Value = "handlers"; myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection; webApp.WebConfigModifications.Add(myModification); #endregion webApp.WebConfigModifications.Add(myModification); SPFarm.Local.Services.GetValue().ApplyWebConfigModifications(); webApp.Update(); }

No comments:

Post a Comment