Modify web.config settings on feature activation in SharePoint

13. April 2011 02:14 by Rashid Aziz in Sharepoint blogs  //  Tags: ,   //   Comments (1)

In SharePoint 2010 there is a way to modify web.config settings file. You need to use SPWebConfigModification class which is a part of Microsoft.SharePoint.Administration namespace. This class allows us to register entities dynamically.

Adding new settings in web.config

I am going to use SPWebModification class inside “Feature Activating” method of SharePoint feature to register a custom assembly.

 public override void FeatureActivated(SPFeatureReceiverProperties properties)

{

SPWebService newService = SPWebService.ContentService;

SPWebConfigModification customModification = new SPWebConfigModification();
customModification.Path = "configuration/SharePoint/SafeControls";
customModification.Name = "SafeControl[@Assembly='CustomAssembly'][@Namespace='CustomNamespace'][@TypeName='*'][@Safe='True']";
customModification.Sequence = 0;
customModification.Owner = "UserName";
customModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
customModification.Value = "<SafeControl Assembly='CustomAssembly' Namespace='CustomNamespace' TypeName='*' Safe='True' />";
newService.WebConfigModifications.Add(customModification);
 
newService.Update();
newService.ApplyWebConfigModifications();

}

Now you need to deploy and activate the feature. After that open web.config of your SharePoint web application where you deployed the feature and will find the following setting being added.J

<SafeControl Assembly="CustomAssembly" Namespace="CustomNamespace" TypeName="*" Safe="True" />

Comments (1) -

kkryczka
kkryczka
7/1/2011 11:06:00 AM #

one comment to add is that you cannot call twice ApplyWebConfigModifications(); in one feature receiver while working in farm SharePoint installation,  at least 2 WFE.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading