Sunday, September 19, 2010

Adding Nested web.config entries using the SPWebConfigModification in SharePoint 2010

In SharePoint 2010, you can’t change the correctly the web.config modification entries because we got a new security feature called “RemoteAdministraorAccessDenied” which is by default set to true.

While creating the nested the entries in the web.config there are two issues :-
First is resolved by using the setting the RemoteAdministratorAccessDenied to false.
   1: SPWebService webservice = SPWebService.ContentService;
   2: webservice.RemoteAdministratorAccessDenied = false;
   3:  
   4: webservice.Update();
   5: Console.WriteLine("Navdeep Done");

Second - select “EnsureChildNode” rather using the “EnsureSection” in the modification type.

For e.g. if you would like to add a new section group in the ConfigSection, you have to add the Section group first then you have to provide the XPath to the ParentNode which “configuration/configSections/sectionGroup[@name='CustomURLRewrite']” in my case to add a nested node in the web.config.

   1: SPWebApplication webApp = new SPSite("http://blre3r08-12b:7000/Pages/default.aspx").WebApplication;
   2:  
   3: SPWebConfigModification modification = new SPWebConfigModification("CustomURLRewrite", "configuration/configSections");
   4:  modification.Owner = "SimpleSampleUniqueOwnerValue";
   5:  modification.Sequence = 0;
   6:  modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
   7:  modification.Value = "<sectionGroup name='CustomURLRewrite'>sectionGroup>";
   8:  
   9:  // Add my new web.config modification. 
  10:  webApp.WebConfigModifications.Add(modification);
  11:  
  12:  SPWebConfigModification modification1 = new SPWebConfigModification("CustomURLRewrite", "configuration/configSections/sectionGroup[@name='CustomURLRewrite']");
  13:  modification1.Owner = "SimpleSampleUniqueOwnerValue";
  14:  modification1.Sequence = 1;
  15:  modification1.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
  16:  modification1.Value = "<section name='navdeep' type='Microsoft.SharePoint.ApplicationRuntime.SafeControlsConfigurationHandler, Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' />";

Member name
Description

EnsureChildNode Specifies that the web.config modification must ensure a child node of the node to which the XPath expression points. Value = 0.

EnsureAttribute Specifies that the web.config modification must ensure the value of a particular attribute. Value = 1.

EnsureSection
Ensures a singleton node. This node is only written once, even if multiple entities register multiple instances of EnsureSection to ensure a single section. Value = 2.

Source: MSDN

No comments:

Post a Comment