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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<span style="color: #200080; font-weight: bold;">public</span> <span style="color: #200080; font-weight: bold;">override</span> <span style="color: #200080; font-weight: bold;">void</span> FeatureActivated<span style="color: #308080;">(</span>SPFeatureReceiverProperties properties<span style="color: #308080;">)</span> <span style="color: #406080;">{</span> SPSecurity<span style="color: #308080;">.</span>RunWithElevatedPrivileges<span style="color: #308080;">(</span><span style="color: #308080;">(</span><span style="color: #308080;">)</span> <span style="color: #308080;">=</span><span style="color: #308080;">></span> <span style="color: #406080;">{</span> <span style="color: #200080; font-weight: bold;">string</span> owner <span style="color: #308080;">=</span> <span style="color: #200080; font-weight: bold;">this</span><span style="color: #308080;">.</span>GetType<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #308080;">.</span>FullName<span style="color: #406080;">;</span> SPWebConfigModification chartImageAppSetting <span style="color: #308080;">=</span> <span style="color: #200080; font-weight: bold;">new</span> SPWebConfigModification<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span> <span style="color: #595979;">//XPath to parent element in which i want to add the child node</span> chartImageAppSetting<span style="color: #308080;">.</span>Path <span style="color: #308080;">=</span> <span style="color: #800000;">"</span><span style="color: #1060b6;">configuration/appSettings</span><span style="color: #800000;">"</span><span style="color: #406080;">;</span> <span style="color: #595979;">//XPath that identifies the element I am adding so it can be correctly updated or deleted afterwards</span> chartImageAppSetting<span style="color: #308080;">.</span>Name <span style="color: #308080;">=</span> <span style="color: #800000;">"</span><span style="color: #1060b6;">add[@key='ChartImageHandler']</span><span style="color: #800000;">"</span><span style="color: #406080;">;</span> chartImageAppSetting<span style="color: #308080;">.</span>Sequence <span style="color: #308080;">=</span> <span style="color: #008c00;">0</span><span style="color: #406080;">;</span> chartImageAppSetting<span style="color: #308080;">.</span>Owner <span style="color: #308080;">=</span> owner<span style="color: #406080;">;</span> chartImageAppSetting<span style="color: #308080;">.</span>Type <span style="color: #308080;">=</span> SPWebConfigModification<span style="color: #308080;">.</span>SPWebConfigModificationType<span style="color: #308080;">.</span>EnsureChildNode<span style="color: #406080;">;</span> <span style="color: #595979;">//XML node to add in the web.config</span> chartImageAppSetting<span style="color: #308080;">.</span>Value <span style="color: #308080;">=</span> <span style="color: #800000;">"</span><span style="color: #1060b6;"><add key='ChartImageHandler' value='storage=memory;deleteAfterServicing=true;' /></span><span style="color: #800000;">"</span><span style="color: #406080;">;</span> SPWebConfigModification chartImageSystemWebHttpHandler <span style="color: #308080;">=</span> <span style="color: #200080; font-weight: bold;">new</span> SPWebConfigModification<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span> chartImageSystemWebHttpHandler<span style="color: #308080;">.</span>Path <span style="color: #308080;">=</span> <span style="color: #800000;">"</span><span style="color: #1060b6;">configuration/system.web/httpHandlers</span><span style="color: #800000;">"</span><span style="color: #406080;">;</span> chartImageSystemWebHttpHandler<span style="color: #308080;">.</span>Name <span style="color: #308080;">=</span> <span style="color: #800000;">"</span><span style="color: #1060b6;">add[@path='ChartImg.axd']</span><span style="color: #800000;">"</span><span style="color: #406080;">;</span> chartImageSystemWebHttpHandler<span style="color: #308080;">.</span>Sequence <span style="color: #308080;">=</span> <span style="color: #008c00;">0</span><span style="color: #406080;">;</span> chartImageSystemWebHttpHandler<span style="color: #308080;">.</span>Owner <span style="color: #308080;">=</span> owner<span style="color: #406080;">;</span> chartImageSystemWebHttpHandler<span style="color: #308080;">.</span>Type <span style="color: #308080;">=</span> SPWebConfigModification<span style="color: #308080;">.</span>SPWebConfigModificationType<span style="color: #308080;">.</span>EnsureChildNode<span style="color: #406080;">;</span> chartImageSystemWebHttpHandler<span style="color: #308080;">.</span>Value <span style="color: #308080;">=</span> @"<span style="color: #308080;"><</span>add path<span style="color: #308080;">=</span>'ChartImg<span style="color: #308080;">.</span>axd' verb<span style="color: #308080;">=</span>'GET<span style="color: #308080;">,</span>HEAD<span style="color: #308080;">,</span>POST' type<span style="color: #308080;">=</span>'System<span style="color: #308080;">.</span>Web<span style="color: #308080;">.</span>UI<span style="color: #308080;">.</span>DataVisualization<span style="color: #308080;">.</span>Charting<span style="color: #308080;">.</span>ChartHttpHandler<span style="color: #308080;">,</span> System<span style="color: #308080;">.</span>Web<span style="color: #308080;">.</span>DataVisualization<span style="color: #308080;">,</span> Version<span style="color: #308080;">=</span><span style="color: #008000;">3.5</span><span style="color: #308080;">.</span><span style="color: #008000;">0.0</span><span style="color: #308080;">,</span> Culture<span style="color: #308080;">=</span>neutral<span style="color: #308080;">,</span> PublicKeyToken<span style="color: #308080;">=</span>31bf3856ad364e35' validate<span style="color: #308080;">=</span>'<span style="color: #200080; font-weight: bold;">false</span>' <span style="color: #308080;">/</span><span style="color: #308080;">></span>"<span style="color: #406080;">;</span> <span style="color: #595979;">//Add the SPWebConfigModifications to the ContentService</span> SPWebService service <span style="color: #308080;">=</span> SPWebService<span style="color: #308080;">.</span>ContentService<span style="color: #406080;">;</span> service<span style="color: #308080;">.</span>WebConfigModifications<span style="color: #308080;">.</span>Add<span style="color: #308080;">(</span>chartImageAppSetting<span style="color: #308080;">)</span><span style="color: #406080;">;</span> service<span style="color: #308080;">.</span>WebConfigModifications<span style="color: #308080;">.</span>Add<span style="color: #308080;">(</span>chartImageSystemWebHttpHandler<span style="color: #308080;">)</span><span style="color: #406080;">;</span> service<span style="color: #308080;">.</span>WebConfigModifications<span style="color: #308080;">.</span>Add<span style="color: #308080;">(</span>chartImageSystemWebServerHandler<span style="color: #308080;">)</span><span style="color: #406080;">;</span> service<span style="color: #308080;">.</span>Update<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span> <span style="color: #595979;">//All the modifications are applied to the web.configs in the farm</span> service<span style="color: #308080;">.</span>Farm<span style="color: #308080;">.</span>Services<span style="color: #308080;">.</span>GetValue<span style="color: #308080;"><</span>SPWebService<span style="color: #308080;">></span><span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #308080;">.</span>ApplyWebConfigModifications<span style="color: #308080;">(</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span> <span style="color: #406080;">}</span><span style="color: #308080;">)</span><span style="color: #406080;">;</span> <span style="color: #406080;">}</span> |
Thanks for reading!
Federico Rodriguez