locked
SharePoint2010 SPWebConfigModification adding new node\section in web.config error RRS feed

  • Question

  • Hi,

    I am trying to add a new section “<baseAddressPrefixFilters>”
    to the web.config file

    Original Web.config

    <Configuration>

    <Sytem.ServiceModel>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

    </Sytem.ServiceModel>

    </Configuration>

    After modification web.config should look like below

    <Configuration>

    <Sytem.ServiceModel>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">

    <baseAddressPrefixFilters>

      <add prefix="https://portal100.sharepoint.com"/>

    </baseAddressPrefixFilters>

    </serviceHostingEnvironment>

    </Sytem.ServiceModel>

    </Configuration>

    So to add <baseAddressPrefixFilters> section. I have below code:-

    SPWebConfigModification myModification = new SPWebConfigModification();           
    myModification.Path =        "configuration/System.ServiceModel/ServiceHostingEnvironment";
                myModification.Name = "sectionGroup[@name= ‘baseAddressPrefixFilters’]";           
    myModification.Sequence = i;  

    myModification.Owner = ModifyOwner;           
    myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;           
    myModification.Value = @"";           
    webApp.WebConfigModifications.Add(myModification);

    webApp.Update();

    webApp.Farm.Services.GetValue().ApplyWebConfigModifications();

    But the above code throws following error:-

    Failed to apply web.config modifications to the file. Value property for EnsureChildNode
    cannot be set to an empty value.

    I think the above error is thrown because of this line below           
    myModification.Value = @"";

    So how to add a new node\section to web.config file?

    Secondly I need to check if <baseAddressPrefixFilters> already  added to web.config . I.e I need
    to add this new section only if it doesn’t already exists. How to check that?


    Thanks



    • Edited by vilas6_it Tuesday, March 20, 2012 3:40 PM
    Tuesday, March 20, 2012 5:42 AM

Answers

  • Cant we do it using c#? I am trying to add a new section named ‘baseAddressPrefixFilters". The below code errors out, it says Error Occured in deployment step : The '[' character , hexadecimal value 0x5B, cannot be included in a name. But that section will have a child node under it which is:

    <add prefix="https://portal100.sharepoint.com"/>

    But I can add it until I add the New Section 1st.

    The Below code snippet is embeded in "FeatureActivated" event. I tried to add even a SafeControl entry But it throws the same error:-

    error occurred in deployment step 'Add solution': The '[' character, hexadecimal value 0X5B, cannot be included in a name.

    Is any additional code is required in Feature Activated event. Like getting the handle of WebApplication? I think as this feature is Scoped at "WebApplication" it will automatically know about it

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
            { 
                SPWebApplication webApp = (SPWebApplication) properties.Feature.Parent;
                webApp.WebConfigModifications.Clear();
                SPFarm.Local.Servers.GetValue<SPWebService>().WebConfigModifications.Clear();

                SPWebConfigModification myModification = new SPWebConfigModification();
                myModification.Path = "configuration/system.serviceModel/serviceHostingEnvironment";
                myModification.Name = "baseAddressPrefixFilters";
                myModification.Sequence = 0;
                myModification.Owner = "User100";
                myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                myModification.Value = "<baseAddressPrefixFilters></baseAddressPrefixFilters>";

                webApp.WebConfigModifications.Add(myModification);

                /*Call Update and ApplyWebConfigModifications to save changes*/
                webApp.Update();
                webApp.Farm.Servers.GetValue<SPWebService>().ApplyWebConfigModifications();          

                SPWebConfigModification myModification1 = new SPWebConfigModification();
                myModification1.Path = "configuration/system.serviceModel/serviceHostingEnvironment/baseAddressPrefixFilters";
                myModification1.Name = "add[@prefix='test']";          
                myModification1.Sequence = 1;
                myModification1.Owner = "User100";
                myModification1.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                myModification1.Value = "<add prefix='test'/>";
                 
                webApp.WebConfigModifications.Add(myModification1);
                webApp.Update();
                webApp.Farm.Servers.GetValue<SPWebService>().ApplyWebConfigModifications();

             
            }


    Secondly is there a way to check if the new section "baseAddressPrefixFilters" already exists before adding it? and add the new section only if it dosent exist.











    • Marked as answer by vilas6_it Tuesday, March 20, 2012 9:38 PM
    • Edited by vilas6_it Wednesday, March 21, 2012 12:04 AM
    Tuesday, March 20, 2012 3:35 PM

All replies

  • you can work with Powershell scripts to add or modify entries in web.config file:

    http://ikarstein.wordpress.com/2010/09/02/add-web-config-modification-with-powershell-spwebconfigmodification/

    http://gallery.technet.microsoft.com/scriptcenter/c5ff9839-08c7-426b-ade4-9bc19da5377c

    http://get-powershell.com/post/2008/07/29/Editing-WebConfig-Files-with-PowerShell.aspx


    MCTS,MCPD Sharepoint 2010. My Blog- http://sharepoint-journey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Vote as Helpful


    Tuesday, March 20, 2012 8:01 AM
  • Cant we do it using c#? I am trying to add a new section named ‘baseAddressPrefixFilters". The below code errors out, it says Error Occured in deployment step : The '[' character , hexadecimal value 0x5B, cannot be included in a name. But that section will have a child node under it which is:

    <add prefix="https://portal100.sharepoint.com"/>

    But I can add it until I add the New Section 1st.

    The Below code snippet is embeded in "FeatureActivated" event. I tried to add even a SafeControl entry But it throws the same error:-

    error occurred in deployment step 'Add solution': The '[' character, hexadecimal value 0X5B, cannot be included in a name.

    Is any additional code is required in Feature Activated event. Like getting the handle of WebApplication? I think as this feature is Scoped at "WebApplication" it will automatically know about it

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
            { 
                SPWebApplication webApp = (SPWebApplication) properties.Feature.Parent;
                webApp.WebConfigModifications.Clear();
                SPFarm.Local.Servers.GetValue<SPWebService>().WebConfigModifications.Clear();

                SPWebConfigModification myModification = new SPWebConfigModification();
                myModification.Path = "configuration/system.serviceModel/serviceHostingEnvironment";
                myModification.Name = "baseAddressPrefixFilters";
                myModification.Sequence = 0;
                myModification.Owner = "User100";
                myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                myModification.Value = "<baseAddressPrefixFilters></baseAddressPrefixFilters>";

                webApp.WebConfigModifications.Add(myModification);

                /*Call Update and ApplyWebConfigModifications to save changes*/
                webApp.Update();
                webApp.Farm.Servers.GetValue<SPWebService>().ApplyWebConfigModifications();          

                SPWebConfigModification myModification1 = new SPWebConfigModification();
                myModification1.Path = "configuration/system.serviceModel/serviceHostingEnvironment/baseAddressPrefixFilters";
                myModification1.Name = "add[@prefix='test']";          
                myModification1.Sequence = 1;
                myModification1.Owner = "User100";
                myModification1.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
                myModification1.Value = "<add prefix='test'/>";
                 
                webApp.WebConfigModifications.Add(myModification1);
                webApp.Update();
                webApp.Farm.Servers.GetValue<SPWebService>().ApplyWebConfigModifications();

             
            }


    Secondly is there a way to check if the new section "baseAddressPrefixFilters" already exists before adding it? and add the new section only if it dosent exist.











    • Marked as answer by vilas6_it Tuesday, March 20, 2012 9:38 PM
    • Edited by vilas6_it Wednesday, March 21, 2012 12:04 AM
    Tuesday, March 20, 2012 3:35 PM
  • I am facing the same issue. Do you have any resolution?

    Tuesday, June 30, 2015 12:56 PM
  • I tried your script and get error: "System.Xml.XPath.XPathException: 'Expression must evaluate to a node-set.'", any idea ?.
    Monday, December 2, 2019 9:41 AM