0

I have SP2010. I know web.config modifications is not always the best way to modify web.config according to some articles from Internet, but we have what we have.

My goal is to make the following work:

  1. Programmatically apply "modification1" which has "owner1" and sets value "false" of "MySetting" key which is placed inside appSettings element in web.config.

  2. Manually edit web.config, set value to "true".

  3. Programmatically apply "modification2" which has "owner2" and adds just another key inside appSettings element.

When I do it, I see that "MySetting" has value "false" just like it was when I applied the modification initially. I am losing my manual updates (provided in step #2).

Here is a simplified method which applies a web.config modification in my code:

private void ApplyWebConfigModifications(SPWebApplication webApplication, string name, string owner, string xpath, string value)
{
    SPWebConfigModification item = new SPWebConfigModification(name, xpath);
    item.Owner = owner;
    item.Sequence = 0;
    item.Value = value;
    item.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute;

     webApplication.WebConfigModifications.Add(item);

     Log.WriteInfo(string.Format("Web application {0} ({1}) has {2} web config modifications to apply.", webApplication.Name, m_WebApplicationUrl, webApplication.WebConfigModifications.Count));

     webApplication.WebService.ApplyWebConfigModifications();
     webApplication.Update();
}

I expect that I will have only one item in WebConfigModifications collection. Unfortunately, the collection on my side has 230 items. As a result all 230 modifications are applied. One of the 230 modifications is my initial modification which has "owner1" and sets "false" value. It explains why I am losing manual changes.

I do not want to remove all 230 modifications programmatically, because I am afraid if I do so, the modifications will be reverted/removed from web.config.

Could you tell me what am I doing wrong and how can I achieve my goal?

Thank you.

2 Answers 2

1

The thing you're doing wrong is to modify the web.config manually and think that'll work.

If you want to change a change to web.config setting where there exist a web config modification, then you should do that using a web config modification (with a higher sequence number). ALL web config modifications are applied every time you call ApplyWebConfigModifications.

2
  • Thank you Per, I do not think it will work in my case. I have "auth" and "runtime" web applications. The "auth" is used by editors to provide changes. The "runtime" is used by anonymous users. The configuration is: "auth" was extended to have "runtime". Both applications have the same database, but are placed in two different folders inside Inetpud\wss\VirtualDirectories. When I apply modifications to "auth", the same changes are applied to "runtime" web.config. Then I need to edit "runtime" web.config manually to put appropriate values, which are removed after next deployment. Thank you.
    – Antipod
    Mar 21, 2012 at 13:20
  • If you have two zones which needs different settings, then you're better of not using web config modifications for that setting. Or remember to change the setting everytime it's needed. Mar 21, 2012 at 13:25
1

If you are adding SPWebConfigModification items, you need to remove them when they are not longer required. Each time you call Add method on WebConfigModifications collection you are registering another modification in web.config.

When working with SPWebConfigModification I was quite successful with Steven Van de Craen project. The concept is based on feature activation (registering modifications) and deactivation (removing modifications from web.config). You just need to get the XPath and Name values right, as the whole SPWebConfigModification API is using them to identify unique nodes in the web.config XML structure.

If you want to apply modifications from UI you can check SharePoint 2010 : Winwise Web.config Modification.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.