Modifying web.config in SharePoint

Modifying web.config in SharePoint
3 votes, 5.00 avg. rating (97% score)

Introduction


This post covers how to modify web.config through object model in SharePoint. Changes to the web.config should not be done manually which is a bad practice.
Modifications should be done through SPWebConfigModification class object and the complete track of changes will be saved in configuration database. This is the best practice to be used

Skill Level – Medium

Modifying web.config in SharePoint

Code sample to add a safe control entry to the safe controls section in web.config throughout the server farm.


SPWebService myService = SPWebService.ContentService;

SPWebConfigModification myModification = new SPWebConfigModification();
myModification.Path = "configuration/SharePoint/SafeControls";
myModification.Name = "SafeControl[@Assembly='MyCustomAssembly'][@Namespace='MyCustomNamespace'][@TypeName='*'][@Safe='True']";
myModification.Sequence = 0;
myModification.Owner = WebConfigModificationFeatureReceiver.OwnerId;
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
myModification.Value = "<SafeControl Assembly='MyCustomAssembly' Namespace='MyCustomNamespace' TypeName='*' Safe='True' />";

myService.WebConfigModifications.Add(myModification);
myService.Update(); 
myService.ApplyWebConfigModifications();


Following is the sample code to modify web.config in feature activation event that will add key

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
    SPWebService webService = webApp.WebService;

    // Create new modification
    SPWebConfigModification myModification = new SPWebConfigModification();
    myModification.Path = "configuration/system.web/siteMap/providers";
    myModification.Name = "add[@name='CustomSiteMapProvider'][@siteMapFile="+
							"'_layouts/DelegateControlDemo/custom.sitemap'] "+
							" [@type='Microsoft.SharePoint.Navigation.SPXmlContentMapProvider, "+
							"Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, "+
							"PublicKeyToken=71e9bce111e9429c']";
	myModification.Sequence = 0;
	myModification.Owner = properties.Feature.DefinitionId.ToString();
	myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
	myModification.Value = "<add name='CustomSiteMapProvider'"+
							"siteMapFile='_layouts/DelegateControlDemo/custom.sitemap'"+
							"type='Microsoft.SharePoint.Navigation.SPXmlContentMapProvider,"+
							"Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, "+
							"PublicKeyToken=71e9bce111e9429c' />";

    // Add the modification and save the changes
    webService.WebConfigModifications.Add(myModification);
    webService.Update();
    webService.ApplyWebConfigModifications();
}

 

We track the modification, by assigning our feature’s definition id as the owner of the modification. On feature deactivation, we loop through all the modification objects and get the one modified by our feature. Finally we call the ApplyWebConfigModifications method to save the changes, as demonstrated in the following code sample:

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    SPWebConfigModification modificationFound = null;
    SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;
    SPWebService webService = webApp.WebService;
    Collection<SPWebConfigModification> modificationCollection = webService.WebConfigModifications;

    // Find the modification by owner
    foreach (SPWebConfigModification modification in modificationCollection)
    {
        if (modification.Owner == properties.Feature.DefinitionId.ToString())
        {
            modificationFound = modification;
            break;
        }
    }

    // Remove the modification and save the changes
    webService.WebConfigModifications.Remove(modificationFound);
    webService.Update();
    webService.ApplyWebConfigModifications();
}

Conclusion

The owner related information that we are using in the preceding example to track the modifications by owner will not be present in the web.config itself and would be maintained in the configuration database. Due to this reason, manual modifications to the web.config should be avoided as these will not be persisted in the configuration database and application of a Service Pack might actually wipe these out.

November 2, 2013 · Adi · No Comments
Tags: , , ,  · Posted in: C#, Sharepoint 2010

Leave a Reply

What is 9 + 4 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)