1

I've been trying to add a line to my web.config through a EventReceiver on my webpart's Feature. I tried to follow some 'tutorials' (this, this and this). It wasn't working, but at least i could deploy the solution.

Sometimes it would delete everything on my web.config and leave only the line that i wanted to add. It was a simple line on <configuration><appSettings><add key="searchPath" value="http://myVirtualMachineSearchPath" />

After the update, it would leave me empty with just that one single line on the web.config.

Right now, my code is this:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            InsertWebConfigEntries(properties);
        }

        //add all web.config entries 
        private void InsertWebConfigEntries(SPFeatureReceiverProperties properties)
        {
            if (!CheckEntries(properties))
            {
                using (SPSite site = properties.Feature.Parent as SPSite)
                {
                    SPWebConfigModification dsEntry = GenerateWebConfigEntry("New config key", "configuration/appSettings",
                        "<add key=\"searchPath\" value=\"http://myVirtualMachineSearchPath\" />", 100);

                    SPWebApplication currentWebApp = site.WebApplication;

                    currentWebApp.WebConfigModifications.Add(dsEntry);
                    currentWebApp.WebService.ApplyWebConfigModifications();
                    currentWebApp.Update();
                }
            }
        }

        //check if the web.config entry exists. If you don't call this method you will get duplicate entries 
        private bool CheckEntries(SPFeatureReceiverProperties properties)
        {
            using (SPSite site = properties.Feature.Parent as SPSite)
            {
                SPWebApplication currentWebApp = site.WebApplication;

                Collection<SPWebConfigModification> webConfigEntries = currentWebApp.WebConfigModifications;

                foreach (SPWebConfigModification entry in webConfigEntries)
                {
                    if (entry.Owner == "Owner")
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        //new web.config entry 
        private SPWebConfigModification GenerateWebConfigEntry(string modName, string sXpath, string sKey, uint iSequence)
        {
            SPWebConfigModification configMod = new SPWebConfigModification(modName, sXpath);
            configMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            configMod.Name = string.Format("add[@name='{0}']", modName);
            configMod.Owner = "Owner";

            configMod.Value = sKey;
            configMod.Sequence = iSequence;

            return configMod;
        } 

But now, when I try to deploy the solution, it gives me an error:

Error occurred in deployment step 'Activate Features': Failed to apply web.config modifications to Web application '3f79bb09-9e0f-4aed-a400-5debe6346440'.
Failed to apply web.config modifications to file 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config'.
Failed to apply a web.config modification to file 'C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config'.  
The specified node "configuration/appSettings" was not found in the web.config file.

When i debug the code, my SPWebApplication.WebConfigModifications already contains 18 elements that i added on my previous attempts, but i deleted that code and for some reason, i can't evoke the WebConfigModifications.Clear() method with success, every time i try to use the SPWebApplication.Update() it gives me the error above, even when i don't add any other SPWebConfigModification.

Anyone have a idea of what I can do to work with this issue?

I'm sorry if the post is a little confusing, but I'm pretty lost at the moment.

2
  • 1
    Have you tried to make the same modifications via PowerShell only? I'd highly recommend doing them in PowerShell over a Feature in a WSP, as the SPWebConfigModification is extremely flaky, many developers have problems with it.
    – James Love
    Jan 19, 2013 at 13:11
  • I might have to go that way James, specially now that I know i'll have to input code on the central administration's web.config too. Jan 30, 2013 at 16:05

0

Your Answer

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

Browse other questions tagged or ask your own question.