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:
Programmatically apply "modification1" which has "owner1" and sets value "false" of "MySetting" key which is placed inside appSettings element in web.config.
Manually edit web.config, set value to "true".
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.