Gorgo.Live.ToString()

Mariusz, Gorzoch tech Blog

Tweaking up web.config thru SharePoint API

leave a comment »

Each day bring some changes. This is even more true when you are working with SharePoint as the API is so huge and reach that sometimes I do not believe that one person can know and understand all. Today I needed to write a feature which on activation will disable unnecessary HttpModules from web.config. To achieve that you need special class “SPWebConfigModification”. This class let you update selected areas of SharePoint configuration file and spread those changes across your entire farm.

Here are main part of my solution:

features.xml

<Feature xmlns="http://schemas.microsoft.com/sharepoint/"
         Id="{77490000-6157-4ef0-A70B-6B463BFCFD26}"
         Title="HotFix 01 date 2010-05-25"
         Scope="WebApplication"
         Hidden="FALSE"
         ImageUrl="HotFix.jpg"
         Version="1.0.0.0"
         ReceiverAssembly="Hempel.OneHempel.HotFixes.HF01D20100525, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c4c727ecf8ef0913"
         ReceiverClass="Hempel.OneHempel.HotFixes.HF01D20100525.Code.EventReceiver">
</Feature>

Please notice here what is the scope of this feature. As our solution will tweak web.config, we need to have scope = “WebApplication”.

Second part of this solution is a ReceiverClass which actually is doing tweak up during feature activation:

public override void FeatureActivated(
                SPFeatureReceiverProperties properties)
{
    SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;

    SPWebConfigModification modification01 = new SPWebConfigModification("add[@name=’FormsAuthentication’]", "configuration/system.web/httpModules");
    modification01.Owner = ModificationOwner;
    modification01.Sequence = 0;
    modification01.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode ;
    modification01.Value = "<!– <add name=\"FormsAuthentication\" type=\"System.Web.Security.FormsAuthenticationModule\" /> –>";

    SPWebConfigModification modification02 = new SPWebConfigModification("add[@name=’UrlAuthorization’]", "configuration/system.web/httpModules");
    modification02.Owner = ModificationOwner;
    modification02.Sequence = 0;
    modification02.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    modification02.Value = "<!– <add name=\"UrlAuthorization\" type=\"System.Web.Security.UrlAuthorizationModule\" /> –>";

    // Add my new web.config modification.
    webApplication.WebConfigModifications.Add(modification01);
    webApplication.WebConfigModifications.Add(modification02);

    // Save web.config changes.
    webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

    // Serialize the web application state and propagate changes across the farm.
    webApplication.Update();

}

public override void FeatureDeactivating(
                SPFeatureReceiverProperties properties)
{
    SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
    Collection<SPWebConfigModification> collection = webApplication.WebConfigModifications;

    int iStartCount = collection.Count;

    // Remove any modifications that were originally created by the owner.
    for (int c = iStartCount – 1; c >= 0; c–)
    {
        SPWebConfigModification configMod = collection[c];

        if (configMod.Owner == ModificationOwner)
            collection.Remove(configMod);
    }

    // Apply changes only if any items were removed.
    if (iStartCount > collection.Count)
    {
        webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        webApplication.Update();
    }
}

You can find a trick in above code. In all examples I found on the net, class “SPWebConfigModification” is used to update/add attributes. None of those examples didn’t show how to remove section. The trick I found was to replace section with the same section included in comments mark. So this way I manager to write a feature which you can use to disable/remove httpModules : “FormsAuthentication” and “UrlAuthentication”.

have fun playing with that. Here you have excellent post around details : Mark Wagner blog

Advertisements

Written by Mariusz Gorzoch

25 May 2010 at 21:50

Posted in SharePoint

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: