I am using SPWebConfigModification thorugh webapplication feature everytime it shows error "The '[' character, hexadecimal value 0x5B, cannot be included in a name. Line 2, position 2727.
" I am very puzzled with this error. I have tried every possible solution given in Google with no help.The code I am using is given below: I am using a feature receiver.
[Guid("c35d2a13-980c-4afb-a832-26d9f7cec85f")]
public class WebConfigMod2EventReceiver : SPFeatureReceiver
{
// Uncomment the method below to handle the event raised after a feature has been activated.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
string Name = null;
string value = null;
string Owner = null;
string Path = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplication spWebApplication = properties.Feature.Parent as SPWebApplication;
Removemodifications(spWebApplication);
SPWebConfigModification myModification = new SPWebConfigModification();
myModification.Path = "configuration";
myModification.Name = "Location[@name='Test']";
myModification.Sequence = 0;
myModification.Owner = "Moumita";
myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureSection;
myModification.Value = "<location path='Test'></location>";
spWebApplication.WebConfigModifications.Add(myModification);
spWebApplication.Update();
spWebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
});
}
catch (Exception ex)
{
throw ex;
}
}
// Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
try
{
SPWebApplication spWebApplication = properties.Feature.Parent as SPWebApplication;
Removemodifications(spWebApplication);
spWebApplication.Update();
spWebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
catch (Exception ex)
{
throw ex;
}
}
public void Removemodifications(SPWebApplication webApp)
{
try
{
Collection<SPWebConfigModification> spWebConfigModifications = webApp.WebConfigModifications;
SPWebConfigModification spWebConfigModificationToRemove = null;
foreach (var spWebConfigModification in spWebConfigModifications)
{
if (spWebConfigModification.Name == "Location[@name='Test']" && spWebConfigModification.Path == "configuration")
{
spWebConfigModificationToRemove = spWebConfigModification;
break;
}
}
if (spWebConfigModificationToRemove != null)
{
webApp.WebConfigModifications.Remove(spWebConfigModificationToRemove);
}
}
catch (Exception ex)
{
throw ex;
}
}
Please help me asap