Friday, June 20, 2008

Modifying the SharePoint Web.config

Modifying the web.config file for SharePoint 2007 is more complicated than a typical ASP.NET web application.  Although modifying the file by hand does work, if your production environment is a large farm, or if you need your solution to be easily re-deployable, manual modification is not the answer.  So SharePoint provides us with an SPWebConfigModification object to handle changes.

My example deals with adding appSetting keys.  Typically, these keys should be added when your feature is installed or activated.  I chose the FeatureActivated event for this example.

 
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPSite site = properties.Feature.Parent as SPSite)
    {
        SPWebApplication webApp = site.WebApplication;
        SPWebConfigModification configMod = new SPWebConfigModification();
        configMod.Name = "add[@key='" + key + "']";
        configMod.Path = @"configuration/appSettings";
        configMod.Value = @"<add key=""MyKeyName"" value=""MyValue"" />";
        // you can put whatever you want for the owner, but using the feature id is a good standard
        configMod.Owner = properties.Feature.DefinitionId.ToString();
        configMod.Sequence = 0;
        configMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
 
        if (webApp.WebConfigModifications.Contains(configMod))
            webApp.WebConfigModifications.Remove(configMod);
        webApp.WebConfigModifications.Add(configMod);
        // I have seen several examples that only call ApplyWebConfigModifications
        // or that make the Update call after calling ApplyWebConfigModifications.
        // But the only way it works for me is to call Update first.
        webApp.Update();
        webApp.WebService.ApplyWebConfigModifications();
    }
}

 

The ApplyWebConfigModifications works through a timer job, so there may be a slight delay in the update.

I stole this removal routine from Vincent Rothwell.  I like it because it keys off the owner and since I use the feature id as the owner, it knows exactly which entries to remove.  It also helps to clean up any goofs I may have made in the file earlier.


 
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    using (SPSite site = properties.Feature.Parent as SPSite)
    {
        RemoveAppSettingsByOwner(site, properties.Feature.DefinitionId.ToString());
    }
} 
 
public static void RemoveAppSettingsByOwner(SPSite site, string owner)
{
    SPWebApplication webApp = site.WebApplication;
    Collection<SPWebConfigModification> modifications = webApp.WebConfigModifications; 
 
    int modCount = modifications.Count; 
 
    for (int c = modCount - 1; c >= 0; c--)
    {
        SPWebConfigModification modification = modifications[c]; 
 
        if (modification.Owner == owner)
            modifications.Remove(modification); 
 
    } 
 
    if (modCount > modifications.Count)
    {
        webApp.Update();
        webApp.WebService.ApplyWebConfigModifications();
    }
} 

0 comments: