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