none
SPWebConfigMOdifications reverts back the entries in web.config

    Question

  • Hi All,

    I have written following appsettings entries using SPWebconfigModification with blank values which the Admin will enter after all the sites are created.

    <add key="DBName" value=" " />
    <add key="LogServiceURL" value="  " />
    <add key="WCFServiceURL" value=" "/>

    Once the site is provisioned, the Admin enters the following values manually:

    <add key="DBName" value="TestDB" />
    <add key="LogServiceURL" value="http://localhost:400/LOGService.svc" />
    <add key="WCFServiceURL" value="http://localhost:200/WCFService.svc"/>

    Now the Admin can also change the value of the 'Trace' attribute for one of the custom section through UI to enable/disable tracing. When the user hits the submit button,I modify the value using the following code:

    SPWebApplication webApp = SPContext.Current.Site.WebApplication;

    // Create a new modification object to store our change

    SPWebConfigModification traceModification = new SPWebConfigModification("Trace", "configuration/customConfig/TraceConfig");

    traceModification.Sequence = 0;

    traceModification.Owner = HttpContext.Current.User.Identity.Name;

    traceModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;

    // Specify the value

    traceModification.Value = rdbList.SelectedItem.Value

    // Add my new web.config modification.

    webApp.WebConfigModifications.Add(traceModification);

    // Save web.config changes.

    webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

    // Serialize the web application state and propagate changes across the farm.

    webApp.Update();

    But when this code executes, the values which the user had put manually are reverted back to the blank values:
    <add key="DBName" value=" " />
    <add key="LogServiceURL" value="  " />
    <add key="WCFServiceURL" value=" "/>

    As the web modifications are stored in config database, these values are being reverted back to their original values from config DB.

    If I use the following line of code
    webApp.WebConfigModifications.Clear();

    Then all the entries are deleted from web.config

    Can anyone help me with this issue?
    Is there any way through which the values are not lost and also the entries are not deleted?

     

    Thursday, January 27, 2011 6:14 AM

Answers

  • Hi,

    Don't modify the settings created by using the SPWebConfigModification in web.config manually.

    All operatons done by using the SPWebConfigModification object are recorded in database.

    So in fact that it wouldn't change the settings in database even you modify the web.config manually and after you call the following code, it would be rolled back again.

    myService.Update();
     myService.ApplyWebConfigModifications();
    

    Microsoft Online Community Support
    • Marked as answer by KeFang Chen Wednesday, February 02, 2011 7:33 AM
    Friday, January 28, 2011 3:35 AM

All replies

  • Hi,

    Don't modify the settings created by using the SPWebConfigModification in web.config manually.

    All operatons done by using the SPWebConfigModification object are recorded in database.

    So in fact that it wouldn't change the settings in database even you modify the web.config manually and after you call the following code, it would be rolled back again.

    myService.Update();
     myService.ApplyWebConfigModifications();
    

    Microsoft Online Community Support
    • Marked as answer by KeFang Chen Wednesday, February 02, 2011 7:33 AM
    Friday, January 28, 2011 3:35 AM
  • Hi,

    So what is the solution for maintaining manual entries after web.config is modified using SPWebConfigModifications?

    Or

    Is there any way through which we can avoid manual values being overwritten from database?

    Friday, January 28, 2011 6:09 AM
  • Hi,

    No built-in procedure can be adopted to maintaining the settings created with SPWebConfigModifications and modified manually.

    So either you add the settings manually ,or you create a SPWebConfigModifications object with value.


    Microsoft Online Community Support
    Friday, January 28, 2011 6:21 AM