Domanda

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.

È stato utile?

Soluzione

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.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top