C# – Sharepoint web.config changes using SPWebConfigModification

csharepointweb.config

I've written a sharepoint application that needs to change web.config

I have a feature that is supposed to make all these configurations. The code for that feature is like this:

SPSite site = properties.Feature.Parent as SPSite;
List<SPWebConfigModification> modifications = new List<SPWebConfigModification>();
modifications.AddRange(CustomErrorsModeConfig.Modifications);
webConfigModificationHelper.AddWebConfigModifications(site.WebApplication, modifications);

CustomErrorsModeConfig.Modifications property contains this code:

public static SPWebConfigModification[] Modifications = {
new SPWebConfigModification()
{ 
Owner = WebConfigModificationOwner,
Name = "mode",
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute, 
Path = "system.web/customErrors",
Sequence = 0,
Value = "Off"
}
};

Then finally the webConfigModificationHelper.AddWebConfigModifications method:

foreach (SPWebConfigModification modification in modifications)
{
webApp.WebConfigModifications.Add(modification);
}
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.Update();

The problem is that I keep getting this error:

Name cannot begin with the ''' character, hexadecimal value 0x27. Line 1, position 1453

Could this be a problem with the web.config before I try to apply my changes ?

Could the SPWebConfigModification property be incorrectly defined ?

Is there some glitch in my code that leads to this error ?

Might there be some property I am missing (e.g. web.AllowUnsafeUpdates) ?

Some sharepoint site configuration ?

I've been trying to solve this issue for some time now with no luck 🙁 Any ideas ?

Best Answer

I can recommend using stsadmwebconfig for making changes to web.config files. I've implemented this in many features and it has always been a pain, especially while developing. Using this tool makes it a lot easier.