Changes to SharePoint web.config with SharePoint API

When installing SharePoint solutions in production environments, configuration often leads to assorted complications. Making installation manuals for non-technical customers, adding configuration parameters to the web.config file, or adding any necessary configuration of third-party components by hand, are things that can be avoided by managing the configuration matters within the solution to be delivered without requiring any manual configuration, all through SharePoint API.

This can be accomplished with a feature with farm scope, which is responsible for changing settings in the web.config. This feature should have FeatureActivated and FeatureDeactivating event receivers in order to add / remove configuration values ​​to the web.config all through SharePoint API.

Then we should create a feature dependency between the configuration feature and the rest of the features in the solution, so that they cannot be activated before activating the farm scoped feature which is the one that provides the necessary configuration (adding it to the web.config) for your solution to work.

Here’s an example event receiver:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSecurity.RunWithElevatedPrivileges(() =>
    {
        string owner = this.GetType().FullName;

        SPWebConfigModification chartImageAppSetting = new SPWebConfigModification();
        //XPath to parent element in which i want to add the child node
        chartImageAppSetting.Path = "configuration/appSettings";
        //XPath that identifies the element I am adding so it can be correctly updated or deleted afterwards
        chartImageAppSetting.Name = "add[@key='ChartImageHandler']";
        chartImageAppSetting.Sequence = 0;
        chartImageAppSetting.Owner = owner;
        chartImageAppSetting.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
        //XML node to add in the web.config
        chartImageAppSetting.Value = "<add key='ChartImageHandler' value='storage=memory;deleteAfterServicing=true;' />";

        SPWebConfigModification chartImageSystemWebHttpHandler = new SPWebConfigModification();
        chartImageSystemWebHttpHandler.Path = "configuration/system.web/httpHandlers";
        chartImageSystemWebHttpHandler.Name = "add[@path='ChartImg.axd']";
        chartImageSystemWebHttpHandler.Sequence = 0;
        chartImageSystemWebHttpHandler.Owner = owner;
        chartImageSystemWebHttpHandler.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
        chartImageSystemWebHttpHandler.Value = @"<add path='ChartImg.axd' 
                    verb='GET,HEAD,POST' 
                    type='System.Web.UI.DataVisualization.Charting.ChartHttpHandler, 
                          System.Web.DataVisualization, 
                          Version=3.5.0.0, 
                          Culture=neutral, 
                          PublicKeyToken=31bf3856ad364e35' 
                    validate='false' />";                               

        //Add the SPWebConfigModifications to the ContentService
        SPWebService service = SPWebService.ContentService;                 
        service.WebConfigModifications.Add(chartImageAppSetting);
        service.WebConfigModifications.Add(chartImageSystemWebHttpHandler);
        service.WebConfigModifications.Add(chartImageSystemWebServerHandler);
        service.Update();
        //All the modifications are applied to the web.configs in the farm
        service.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
    });          
}

 

Thanks for reading!

Federico Rodriguez

No Comments