1

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

4
  • have you tried this blogs.devhorizon.com/reza/2008/01/05/…
    – Waqas Sarwar MVP
    Jun 30, 2015 at 12:59
  • Tried everything. Is there any issue with my code.Even I tried to do it through Powershell and I came up with the same issue.
    – Mou9383
    Jun 30, 2015 at 13:21
  • the blog is talking about the name paramenter.did you try to add using within contructor?
    – Waqas Sarwar MVP
    Jun 30, 2015 at 13:25
  • Hi @WaqasSarwarMCSE, I have provided my code above..Can you please let me know if I had done any mistake? or there is some other issue.
    – Mou9383
    Jul 3, 2015 at 14:34

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.