Often we at Sharepoint implementation comes across this requirement where we need to update your Sharepoint web application’s web.config file. For those who worked in the pre-VSS Sharepoint extention era, you wont forgot the times where you had to write those SafeControl tags in the web.config.
Yes, Sharepoint Extension for Visual Studio (v 1.3, the last before Visual Studio 2010) had made it easy for us. WSP takes care of adding those tags while you deploy the sharepoint project from VSS. Question is what if you want to update web.config after the web Application is deployed on to the server. And what are the options to enable this updates across a sharepoint farm. Well there are few different options, you want to consider. Each method has its own merits and demerits according to the scenario you are facing.
Here are the 5 ways to update the config file. Feel free to jump to option 4 (last) as it my recommended method
- Quick but sometimes complex, specially to those admins with no much xml dev skills, is Manual Updation. You can always open the web.config file in a notepad (or a much prettier XML editor) to update the sections you want. You need an iisreset or AppPool recycle to get the affect of the changes. Major disadvantage of this approach is that there is no way to propogate these changes to the farm.
- Asp.Net and Sharepoint supports extension to web.config files. Extensions are xml files named with a pattern webconfig.<name>.xml. You can have <actions> elements inside this xml file which either adds or removes the elements you need to be added to web.config file. This extension file has to go in the 12 hive Config folder. Here is an example
<actions> <add path="configuration/SharePoint/SafeControls"> <SafeControl Assembly="System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Namespace="System.Web.UI.WebControls" TypeName="*" Safe="True"/> </add> <remove path="configuration/SharePoint/RuntimeFilter"/>The breakout sildenafil online india quickly brought the S&P 500 to the downside target of 1,140-1,150. ED medicine is ought to be taken with a glass of water between 30 and 60 minutes, before viagra sans prescription canada planning lovemaking and the effect of maintaining erection stays for next 4 hours. It is always better to cook eggs without yolk as it is the storehouse go to web-site buying cialis in australia of cholesterol. Researches say that the supplements made of levitra sale http://cute-n-tiny.com/cute-animals/silly-fluffy-pups/ medicinal herbs are the best for curing the problem of semen leakage. <add path="configuration/SharePoint"> <RuntimeFilter Assembly="Company.Product, Version=1.0.1000.0, Culture=neutral, PublickKeyToken=1111111111" Class="MyRuntTimeFilter", BuilderUrl="MyBuilderUrl"/> </add> </actions>
You can find more about this at http://msdn.microsoft.com/en-gb/library/ms439965.aspx
Well, it seems nice and clean until you realize that, in Sharepoint this works only during the creation of a WebApplication. In SharePoint, when a new Web Application is created using Central Admin (or admin script), SharePoint, takes a copy of web.config file from its 12 hive CONFIG folder. During this operation, if it finds an extension xml file, the configuration entries will be processed and web.config file will be updated accordingly. Once the Web Application is created any changes you make to this extension file will never be reflected on to web.config until you recreate (you, crazy) the web application.
- What we really need is a solution that can be re-run as many times you want to make further changes when you require. SharePoint library for SharePoint Administration provides a nice class to do Web.Config updates on the fly. So, What about building a simple C# executable which can read an XML file and update the web.config file on the fly using this namespace (Microsoft.SharePoint.Administration)?
We can achieve this by writing an exe or a windows application which reads XML elements from a string or an XML file and updates the Web.Config file using elevated previlege. Code would look something similar to this. (a complete sample is attached)
private static SPWebConfigModification[] modifications = {
new SPWebConfigModification(“xhtmlConformance”, “configuration/system.web”)
{ Owner = “motion10XhtmlConformance”, Sequence = 0, Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode, Value = “<xhtmlConformance />” },
new SPWebConfigModification(“mode”, “configuration/system.web/xhtmlConformance”)
{ Owner = “motion10XhtmlConformance”, Sequence = 0, Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute, Value = “Strict” }
webApp.WebConfigModifications.Add(modifications[0]);
webApp.Update();
webApp.WebService.ApplyWebConfigModifications();Sounds good? Here is the shortcoming though. This method is external to SharePoint and may need an Admin to log on to the Server to run this successfully. And if you are planning to use this as part of your Development where you may need continues changes be updated, this may not be the right choice. Well, what about making this part of your SharePoint solution?
- Feature to update web.config files:
This solution involves creating a Feature (it can be scoped at Farm (farm), WebApplication (Web application), Site (site collection) or Web (Web site) level) and a feature activated event handler. Event handler reads an XML file and uses Microsoft.SharePoint.Administration namespace to update web.config file. Xml elements can also be read from a string. Feature is activated when the solution is deployed if the feature is scoped at site level. And when the feature is deactivated these additional elements will be deleted from the web.config file. Once deployed, you can update the XML file from the 12 Hive and reactivate the feature to get the affect.
Here is the code snippet:
public class ConfigUpdater : ConfigUpdaterBase
{private const string SPWebConfigModificationOwner = “OnEportalDeployUser”;
private static readonly string ConfigXMLPath = “PortalConfigs.xml”;//Add to this list if you want to ensure we don’t create an SPWebConfig Mod for a node.
private static readonly string[] NodesToIgnore = { “configSections”, “system.web”, “appSettings” };public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWebApplication application = GetCurrentWebApplication(properties);
try
{
if (application != null)
{
//Clean out any old ones first, in case we had an issue previously
RemoveWebConfigModificationsByOwner(application, SPWebConfigModificationOwner);
//This will parse the string and convert to all spwebmods with EnsureChildNode as the type.
//List mods = CreateModificationsFromXMLString(ConfigXML, SPWebConfigModificationOwner, NodesToIgnore);
//This will parse the XML file and convert to all spwebmods with EnsureChildNode as the type.
List mods = CreateModificationsFromXMLFile(ConfigXMLPath, SPWebConfigModificationOwner, NodesToIgnore);
AddWebConfigModifications(application, mods);
}
}
catch (Exception ex)
{
EventLog.WriteEntry(“ConfigUpdater”, “FeatureActivated- ” + ex.Message); throw ex;
}
}public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication application = GetCurrentWebApplication(properties);
if (application != null)
{
RemoveWebConfigModificationsByOwner(application, SPWebConfigModificationOwner);
}
} catch (Exception ex)
{
EventLog.WriteEntry(“ConfigUpdater”, “FeatureDeactivating- ” + ex.Message);
throw ex;
}
}
}See the attached sample for the Base Class definition.
You can also download a sample solution from here.. Sample needs Visual Studio 2008, Visual Studio SharePoint extension 1.3 and WSS 3.0
Feel free to add your comments, questions and suggestions.
I would like to extend my sincere thanks to Mr. David San Filippo for his initial work.
Thanks alot!..