Web Config Updates

The SPWebConfigModification class for SharePoint provides an easy way of updating the web config for a web application. Sometimes, however, I’ve seen it used incorrectly.

For example, it’s not advisable to try and write to the web config on Web or Site feature activation as you will more than likely receive an access denied error. Even running the code with elevated permissions will not resolve this as the majority of the time the executing application pool user will not have sufficient access to the web config file, certainly not in a production environment.

The ideal place to host this type of call is in a Web Application or Farm scoped feature’s FeatureActivated method. Unless the farm has been locked down, this will run with the required permissions to be able to update the web config file(s).

An easy way to update the configurations for all web applications hosting content is to use the following from a Farm scoped feature:

SPWebService.ContentService.WebConfigModifications.Add(authorizedTypeActivity);
SPWebService.ContentService.Update(true);
SPWebService.ContentService.ApplyWebConfigModifications();

Or, to update a specific web application from a Web Application scoped feature:

SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
 
SPWebService.ContentService.WebApplications[webApplication.Id].WebConfigModifications.Add(authorizedTypeActivity);
SPWebService.ContentService.WebApplications[webApplication.Id].Update(true);
SPWebService.ContentService.ApplyWebConfigModifications();
This entry was posted in Configuration, SharePoint and tagged , . Bookmark the permalink.
0 0 votes
Article Rating
Subscribe
Notify of
guest

Solve the maths problem shown below before posting: *

0 Comments
Inline Feedbacks
View all comments