locked
Adding custom key value pair in web.config for a specific web application programmatically RRS feed

  • Question

  • Hi,

    I have 5 web applications in the Farm. I want to add the below key value pair in appsettings in one of the web applications

    <add Key="TestJob" Value="http://siteurl/" />

    I have created a empty sharepoint project. I added a feature, and then in feature activated event. Feature is scoped at web application. I tried the below code:

          var value = "http://siteurl/"; 
    var webApp = properties.Feature.Parent as SPWebApplication;
    
                var mySetting = new SPWebConfigModification
    
                {
    
    Path = "configuration/appSettings",
    Name = string.Format("add [@key='TestJob'] [@value='{0}']", value),
                    Sequence = 0,
    
                    Owner = "test",
    
      Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
    
                    Value = string.Format("", value)
    
                };
    
                webApp.WebConfigModifications.Add(mySetting);
    
                webApp.Update();

    The above code is not working. How to update the web.config using webconfigmodifications?

    Thanks

    Monday, March 21, 2016 12:45 PM

Answers

All replies

  • SPWebConfigModification mod = new SPWebConfigModification();
    mod.Name = @"add[@key='tempFilesPath'][@value='c:\temp']";
    mod.Path = "configuration/appSettings";
    mod.Sequence = 0;
    mod.Owner = Constants.OwnerModif;
    mod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
    mod.Value = @"<add key='tempFilesPath' value'c:\temp' />";
    webApp.WebConfigModifications.Add(mod);
    webApp.WebService.ApplyWebConfigModifications();

    webApp.Update();

    Reference

    https://smindreau.wordpress.com/2013/06/12/finally-the-way-to-add-web-config-modifications-to-sharepoint/

    https://msdn.microsoft.com/en-us/library/ff648376.aspx?f=255&MSPPError=-2147217396

    https://msdn.microsoft.com/en-us/library/office/bb861909%28v=office.14%29.aspx?f=255&MSPPError=-2147217396


    • Edited by sk2014 Monday, March 21, 2016 1:15 PM
    Monday, March 21, 2016 1:14 PM
  • Hi,

    Thanks for the reply.

    I am getting error:

    Failed to apply web.config modifications to Web application 'GUID'.
    Failed to apply web.config modifications to file 'C:\inetpub\wwwroot\wss\VirtualDirectories\PortNO.\web.config'.
    The Value property for EnsureChildNode cannot be set to an empty value.

    when the below line is hit :

    webApp.WebService.ApplyWebConfigModifications();

    I tried the below code as well:

      webApp.WebConfigModifications.Add(mod);
     webApp.Update();
    
    webApp.Farm.Servers.GetValue<SPWebService>().ApplyWebConfigModifications(); 
    

    But it is giving same error. How to fix this?

    Thanks


    • Edited by Venkatzeus Monday, March 21, 2016 2:02 PM
    Monday, March 21, 2016 1:58 PM
  • Hi Venkatzeus,

    I would suggest you can add the key value to web.config like the code snippet below:

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

    Here is a similiar thread for your reference:

    https://social.msdn.microsoft.com/Forums/en-US/90ab7af2-7bd7-4f8e-b18f-09430dbc4ba9/sharepoint2010-spwebconfigmodification-adding-new-nodesection-in-webconfig-error?forum=sharepointdevelopmentprevious

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Tuesday, March 22, 2016 3:12 PM