Tuesday, April 01, 2014

Working Tips with SPWebConfigModification Class in SharePoint 2013

Hi Folks,

I was working on updating a SharePoint web.config programmatically in SharePoint 2013 Farm Solution. Applying web.config modifications in SharePoint is straight forward task by adding the modifications into a collection of the web application and then submit your changes.

Since this class is poorly documented, I was trying to update a specific web application web.config and not all web.config in this farm. all the code samples in MSDN and other blogs don't provide a necessary understanding how it works so before taking the MSDN code and apply it into your feature or wsp you should know the following essential tips:

1) You can apply your web config modifications either on the web application level or the content service level which affects all web config files in your farm.

2) When your code has the following line of code, this means that the changes will be taken effect into all web applications in the farm:

SPWebApplication webApp = new SPSite('siteUrl').WebApplication;
WebApp.Farm.Services.GetValue<SPWebService>.ApplyWebConfigModifications();

Even though you are using a web application object, but this code will update all web.config files, which you should avoid when you apply solutions unless you are applying Farm updates.


3) If you want to update a specific web application web.config:

SPWebApplication webApp = new SPSite('siteUrl').WebApplication;
WebApp.Update();  // update specific web.config for this web application
webApp.WebService.ApplyWebConfigModifications();


line #3 will only update the web.config for a specific web application and not all unlike
SPWebService.ContentService.ApplyWebConfigModifications();

Hope this helps!

**References:
1) Add or remove entries in web.config programmatically:
http://msdn.microsoft.com/en-us/library/bb861909(v=office.14).aspx



No comments: