One of requirement in my project was to edit the web.config of the current SharePoint web application.After some goggling I came to know that SharePoint provides us the powerful API (SPWebConfigModification) to interact with web.config directly.So I will be explaining some points that how to use this API:I created a feature at WebApplication Level so that we will be able to Modify configuration file as soon as feature gets activated.Actually SPWebConfigModification Class has Members Like
private void ModifyAuthorization (SPWebApplication webApp){ SPWebConfigModification testEntry = new SPWebConfigModification ("users", "configuration/system.web/ authorization/ allow "); testEntry.Sequence = 0; testEntry.Owner = "somethingUniqueName"; testEntry.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute; testEntry.Value = "?"; webApp.WebConfigModifications.Add (testEntry);}If you noticed in Part 1 and Part 2 examples... type property of modification is different That is because when you want to add a section inside configuration file then use EnsureChildNode and when you want to set node values which are already in configuration file then use EnsureAttribute .Also while adding section in configuration file avoid using type as EnsureSection because sections created by using this type are permanent and cannot be removed from the configuration file.So calling these two methods inside feature receiver will be like :public override void FeatureActivated(SPFeatureReceiverProperties properties){ SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication; AddSafeControlEntry(webApplication); ModifyAuthorization(webApplication); webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); webApplication.Update();}Small Tip: After doing changes in configuration file one should call methodwebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); webApplication.Update();because by using this method changes to web.config file gets applied at farm level and also use this method call only once rather using after each addition because this method creates timer job on server so if you call this method more than once in single cycle then you might face an error like .. There is timer job already runningSo best practice is to call your methods to add your changes in configuration file and then finally call this method References: great post : http://www.crsw.com/mark/Lists/Posts/Post.aspx?ID=32and http://blog.thekid.me.uk/archive/2007/03/20/removing-web-config-entries-from-sharepoint-using-spwebconfigmodification.aspx Removing Entries From SharePoint Web Configuration File using SPWebConfigModificationIn this section I will be explaining short code about how to remove entries from web configuration file of SharePoint using API SPWebConfigModificationin following function passing SPWebApplication and name of owner as parameters.Then getting collection of all modifications in web configuration file and then while iterating through collection of these modifications , compare name of owner which is unique and remove that modification from collection and update web application.private void RemoveTestEntry(SPWebApplication webApplication, string owner){ Collection<SPWebConfigModification> allModifications = webApplication.WebConfigModifications; int initialCount = webApplication.Count; for (int i = initialCount - 1; i >= 0; i--) { SPWebConfigModification Testmodification = allModifications [i]; if (Testmodification.Owner == owner) allModifications.Remove(Testmodification); } if (initialCount > allModifications.Count) { webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); webApplication.Update(); }}
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: