Partager via


SPWebConfigModification adds the entry in web.config, but does not remove it

I was working with one of my colleague on an issue. It was a weird one, where customer was using SPWebConfigModification class to add the safe control entry into the web.config. The sample code being used was adapted from the SPWebConfigModification page on MSDN.

    1: static void Main(string[] args)
    2: {
    3:  
    4:     SPSite oSite = new SPSite("https://manpreet2");
    5:     SPWebApplication _webApp = oSite.WebApplication;
    6:     string strName = "SafeControl[@Assembly=\"MyCompany.Name.SamplePart\"]";
    7:  
    8:     SPWebConfigModification SafeControl = new SPWebConfigModification(strName, "configuration/SharePoint/SafeControls");
    9:     SafeControl.Owner = "OwnerName";
   10:     SafeControl.Sequence = 0;
   11:     SafeControl.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
   12:     SafeControl.Value = "<SafeControl Assembly=\"MyCompany.Name.SamplePart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cb3c72729d0875cd\" Namespace=\"MyCompany.Name\" TypeName=\"*\" Safe=\"True\" />";
   13:  
   14:     if (args[0] == "/add")
   15:     {
   16:         Console.WriteLine("Adding...");
   17:         _webApp.WebConfigModifications.Add(SafeControl);
   18:     }
   19:     else
   20:     {
   21:         Console.WriteLine("Removing...");
   22:         _webApp.WebConfigModifications.Remove(SafeControl);
   23:     }
   24:     _webApp.WebService.ApplyWebConfigModifications();
   25:  
   26:     Console.WriteLine("Done...");
   27:     Console.ReadLine();

Surprisingly when we were using this code to add the web.config entry, it was adding it properly but when we were trying to remove it, through web.config file was being modified but entry was not being removed.

This was really surprising, if we took the exact code from the MSDN page, it worked perfectly fine.

Then started the journey to find the root cause for this.

Digging into my older emails, I remember that something similar we had done earlier also but how it was resolved, was not known. Found that old email and check that code, that also worked perfectly fine.

That brought us to line by line comparison of the code and see what is happening and we found the culprit code!

In the following code at line 6, we are giving the assembly name with only the first part of the 4 part name of assembly.

    6: string strName = "SafeControl[@Assembly=\"MyCompany.Name.SamplePart\"]";

But the entry for the SafeControl was done with full 4 part assembly name at line 12.

    12: SafeControl.Value = "<SafeControl Assembly=\"MyCompany.Name.SamplePart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cb3c72729d0875cd\" Namespace=\"MyCompany.Name\" TypeName=\"*\" Safe=\"True\" />";

So when adding, it added the entry but when removing, SharePoint is not able to find any entry with only first part of the 4 part assembly name. That is why, it is not able to remove it.

Why the code at MSDN page is working fine?

It is because in SPWebConfigModification.Value and SPWebConfigModification.Name, both contain only the assembly name and NOT the full 4 part assembly name.

 

Finally 1 more issue resolved and got to understand to look at the code more closely and not to miss the obvious.

 

As always… Happy Coding

 

-Manpreet

Comments

  • Anonymous
    May 18, 2009
    PingBack from http://asp-net-hosting.simplynetdev.com/spwebconfigmodification-adds-the-entry-in-webconfig-but-does-not-remove-it/

  • Anonymous
    June 08, 2009
    Why would you add safecontrols using SPWebConfigModification when you can do it in a solution package manifest?

  • Anonymous
    June 08, 2009
    There is no particular reason why customer was doing this. I have taken it as an example. The customer was having number of other custom entries in web.config and we were seeing issue with some of them also. The basic idea is, to use the full attribute value when doing the modification.