views:

641

answers:

3

Hi

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 ?

+1  A: 

Ive seen this before when the file format is not correctly set between the file and the declaration.

Open the web.config file into a advanced text editor (Notepad++ or Visual Studio) and manually force the file type to match what is specified. Usually its going to be UTF-8.

For more info: http://www.dumpsterdoggy.com/tutorials/?xmlexception-name-cannot-begin-with

AdamSane
A: 

Try taking the List template and for loop out and set the property using straightforward syntax. Here's a post for setting the property in your example, see if you can get this to work and then progress to building up a more generic solution with a List and iteration over the items in the list.

http://www.sharepointkings.com/2008/05/how-to-modify-webconfig-file-in.html

Mike Knowles
+2  A: 

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.

ArjanP