Removing web.config entries from SharePoint using SPWebConfigModification

Search

Accessible SharePoint WebSites
Download ARF

Removing web.config entries from SharePoint using SPWebConfigModification

http://blog.thekid.me.uk

Today I have been trying to remove web.config entries when a feature is de-activated in WSS. Adding them is easy and there are some good articles (Daniel Larson & Tony Bierman) on how to do that...however, removing them can be problematic when the entries are not configured properly.

I started by reading Daniel's post which includes details of removing entries from web.config, but I could not get it to work!!

It turns out that the Name property (poorly named IMO) of the SPWebConfigModification class is very important when it comes to managing the entry. Not only does it allow the entry to be removed when it is removed from the WebConfigModifications collection, but it also prevents duplicates from being added to web.config.

So my problem was simple...my Name property, which looked fine, was not! I had what appeared to be the correct XPath, add[name='TheKidHandler'], but this was incorrect as 'name' is actually an attribute and so the correct format was add[@name='TheKidHandler'].Once this was corrected everything worked great, including no more duplicates, which I was getting during testing.

The one thing which I did do differently to Daniel was when removing the entries. In his post he suggests you re-create the SPWebConfigModification entry and then call Remove on the WebConfigModifications collection using the new object. I however found it better to loop through the WebConfigModifications collection and remove them based upon the owner, which was my feature. The final code looks like this... 

protected void RemoveWebConfigEntries(SPWebApplication oWebApp, string owner)

{

    Collection<SPWebConfigModification> oCollection = oWebApp.WebConfigModifications;

 

    int iStartCount = oCollection.Count;

 

    for (int c = iStartCount - 1; c >= 0; c--)

    {

        SPWebConfigModification oModification = oCollection[c];

        if (oModification.Owner == owner)

            oCollection.Remove(oModification);

    }

 

    if (iStartCount > oCollection.Count)

    {

        oWebApp.Update();

        SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications();

    }

}

The morale of this story is that the Name property is really an XPath statement to uniquely identify your element within the parent node specified in the Path property.

Posted by Vincent Rothwell on Tuesday, 20 Mar 2007 23:35  - 84 Comments
Orininally printed from http://thekid.me.uk - Copyright Vincent Rothwell 2007
 

Comments

Sunday, 27 Jul 2008 10:36 by Andorra
Nice Site! http://google.com

Sunday, 27 Jul 2008 10:36 by Jaime López
Hi Vincent, This solutions is the best to remove modifications maded to web.config. I tried to do it using the SPWebConfigModification object properly configured but I didn't get it. Thanks to your post to bring me the light.

Sunday, 27 Jul 2008 10:36 by Jaime López
Could you give me some help??? This is my scenario: I'm adding three lines to SafeControl collection and two lines to appSettings section. This works fine without problems. Next, I want to uninstall them and using your code only removes the lines from SafeControl, appSettings is not modified and I have to remove them manually. Do you know if there are problems with appSettings section??? Thanks!

Sunday, 27 Jul 2008 10:36 by Jaime López
Could you give me some help??? This is my scenario: I'm adding three lines to SafeControl collection and two lines to appSettings section. This works fine without problems. Next, I want to uninstall them and using your code only removes the lines from SafeControl, appSettings is not modified and I have to remove them manually. Do you know if there are problems with appSettings section??? Thanks!

Sunday, 27 Jul 2008 10:36 by Jaime López
Alright, I got it thanks to my workmate. It was a character forbidden which made that the appSettings lines couldn't be removed. It is solved.

Sunday, 27 Jul 2008 10:36 by Vince
Glad you got it to work. --Vince

Sunday, 27 Jul 2008 10:36 by Russ Giddings
I'm running the exact code above trying to remove entries I've previously added in the '/configuration/system.web/compilation/assemblies' section. It doesn't appear to work. I've heard rumours before that there are issues using the SPWebConfigModification class when modifying this section. Has anyone else had or heard of any similar issues?

Sunday, 27 Jul 2008 10:36 by Russ Giddings
I'm running the exact code above trying to remove entries I've previously added in the '/configuration/system.web/compilation/assemblies' section. It doesn't appear to work. I've heard rumours before that there are issues using the SPWebConfigModification class when modifying this section. Has anyone else had or heard of any similar issues?

Sunday, 27 Jul 2008 10:37 by Mark Wagner
Great solution Vincent! I consider this a best practice.

Sunday, 27 Jul 2008 10:37 by Tareqe
Vincent Rothwell, I am trying to add and remove web.config entry from several times; I also focused your way; but there is no result. I can add web.config entry successfully. But if I try to remove my entry, I can’t, I debug through the code and its showing successful smile and web.config file is also reloaded after a modification. But the entry is not removed. I can remove attribute of a node, but I can’t remove child node. It’s necessary to entry in system.web/httpHandlers in this path and also required to remove from that location. Is there any way to remove web.config entry? Thanks Tareqe

Sunday, 27 Jul 2008 10:37 by Vince
Tareqe, Is it possible that there are other entries which are adding the entry...it is possible to have several entries which add the same node and if you failed to remove one it may still be there. Have a look at this...http://blog.thekid.me.uk/archive/2007/03/24/web-config-modification-manager-for-sharepoint.aspx...it will show you all the modifications that are being made to web.config.

Monday, 29 Sep 2008 01:38 by Bharat
Removal is not working for Safecontrol entries... Please find the code below SPWebApplication myWebApp = site.WebApplication; Collection<SPWebConfigModification> collection = myWebApp.WebConfigModifications; int iStartCount = collection.Count; // Remove any modifications that were originally created by the owner. for (int c = iStartCount - 1; c >= 0; c--) { SPWebConfigModification configMod = collection[c]; if (configMod.Owner == "Test1") myWebApp.WebConfigModifications.Remove(configMod); } // Apply changes only if any items were removed. if (iStartCount > collection.Count) { myWebApp.Update(); myWebApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); } Code For Adding... SPWebApplication webApp = site.WebApplication; // Create a modification SPWebConfigModification Test= new SPWebConfigModification(); Test.Name = "add[@name='Test']"; Test.Path = "/configuration/SharePoint/SafeControls"; Test.Owner = "Test1"; Test.Sequence = 0; Test.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; Test.Value = @"<SafeControl Assembly=Test, Version=1.1.0.0, Culture=neutral, PublicKeyToken=2ed8dda03fe564cd' Namespace='Test' TypeName='*' Safe='True' />"; // Add the modification to the collection of modifications webApp.WebConfigModifications.Add(Test); // Apply the modification webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); // Serialize the web application state and propagate changes across the farm. webApp.Update(); Please let me know if there is any problem with my code. i am trying to do this in my feature activated and feature deactivated. Addition of safe control entries works well for me, But the problem is that it keeps on adding the SafeControl entries and on feature deactivation they are not getting removed. Thanks, Bharat

Monday, 24 Nov 2008 02:42 by Pill Store
Very good job. Add your blog to bookmarks.

Tuesday, 25 Nov 2008 03:10 by rüya tabiri
Thank you...

Sunday, 21 Dec 2008 03:13 by kids game
thanks

Sunday, 4 Jan 2009 04:13 by Chat
thank you

Thursday, 5 Mar 2009 09:04 by sohbet odalari
thank you very much

Saturday, 7 Mar 2009 02:24 by güzel sözler
thank you :)

Saturday, 7 Mar 2009 02:41 by güzel sözler
thank..

Saturday, 7 Mar 2009 03:09 by Sohbet
thankss

Sunday, 8 Mar 2009 06:19 by Sohbet Odalari
thankss you

Friday, 13 Mar 2009 03:49 by Chat
thank youu..

Saturday, 14 Mar 2009 01:42 by granit
Thank You...

Wednesday, 18 Mar 2009 12:22 by Ligtv izle
thank you cano..

Monday, 23 Mar 2009 06:38 by Mynet
thank you kanka

Tuesday, 24 Mar 2009 02:09 by Chat Odalari
thankss youuu

Wednesday, 25 Mar 2009 04:23 by Terry Walker
That’s great, I never thought about Removing web.config entries from SharePoint using SPWebConfigModification like that before.

Wednesday, 25 Mar 2009 02:55 by Sathya Narayanan
Wonderfull... I have been trying to find solution for more than 4 hours.. finally made it working by looking at your blog... Thanks a lot

Saturday, 28 Mar 2009 05:18 by maç izle
thanki cano

Monday, 6 Apr 2009 04:08 by ligtv izle
you are my desire..

Monday, 6 Apr 2009 04:20 by ligtv izle
you are my desire..

Sunday, 12 Apr 2009 12:28 by chat
thank you turkish chat34 !

Sunday, 12 Apr 2009 12:46 by shady
thanks for your work but please answer people those couldn't make it work.. like me:( If I added this twice all the application got damaged. <remove name="PublishingHttpModule" /> so I want to remove it before adding it. or just adding it once.. please advice.

Thursday, 16 Apr 2009 05:55 by Pavan Raj
Hi Jaime López, I see that you added few safetags and appSettings tags in the web.config file , i used your code to do that --> SPWebApplication webApp = site.WebApplication; // Create a modification SPWebConfigModification Test= new SPWebConfigModification(); Test.Name = "add[@name='Test']"; Test.Path = "/configuration/SharePoint/SafeControls"; Test.Owner = "Test1"; Test.Sequence = 0; Test.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode; Test.Value = @"<SafeControl Assembly=Test, Version=1.1.0.0, Culture=neutral, PublicKeyToken=2ed8dda03fe564cd' Namespace='Test' TypeName='*' Safe='True' />"; // Add the modification to the collection of modifications webApp.WebConfigModifications.Add(Test); // Apply the modification webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); // Serialize the web application state and propagate changes across the farm. webApp.Update(); Expression must evaluate to a node-set. but when i try to activate the feature i see "Expression must evaluate to a node-set." error , please help me out in this issue

Thursday, 16 Apr 2009 06:00 by Pavan Raj
Hi Jaime López, Can you please send me those few lines of code by which you could add added few safetags and appSettings tags in the web.config file ? I will be very thanks full to it.

Saturday, 18 Apr 2009 12:15 by ligtv izle
thank you

Sunday, 19 Apr 2009 03:20 by TRsohbet
Thanks

Sunday, 19 Apr 2009 03:33 by TRsohbet
Thank You

Tuesday, 28 Apr 2009 03:29 by chat odaları
thanks

Sunday, 3 May 2009 09:44 by sohbet
thanks

Sunday, 3 May 2009 11:24 by

Monday, 4 May 2009 06:31 by justin tv
turkish justin tv..

Monday, 11 May 2009 05:24 by Sohbet Chat Ask Sevgi Arkadaşlık
thanks...

Friday, 22 May 2009 08:10 by lig tv izle
thank you

Monday, 6 Jul 2009 01:05 by thang
I read on some blogs and use this way to custom web.config, but it does not change anything public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPSite currentSite = properties.Feature.Parent as SPSite; SPWebApplication _chosenWebApp = currentSite.WebApplication; currentSite.RootWeb.Update(); RemoveWebConfigEntries(_chosenWebApp, "ModifyWebConfig"); } protected void RemoveWebConfigEntries(SPWebApplication oWebApp, string owner) { Collection<SPWebConfigModification> oCollection = oWebApp.WebConfigModifications; int iStartCount = oCollection.Count; for (int c = iStartCount - 1; c >= 0; c--) { SPWebConfigModification oModification = oCollection[c]; if (oModification.Owner == owner) oCollection.Remove(oModification); } if (iStartCount > oCollection.Count) { oWebApp.Update(); SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); } }

Monday, 6 Jul 2009 01:53 by thang
I read on some blogs and use this way to custom web.config, but it does not change anything public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { SPSite currentSite = properties.Feature.Parent as SPSite; SPWebApplication _chosenWebApp = currentSite.WebApplication; currentSite.RootWeb.Update(); RemoveWebConfigEntries(_chosenWebApp, "ModifyWebConfig"); } protected void RemoveWebConfigEntries(SPWebApplication oWebApp, string owner) { Collection<SPWebConfigModification> oCollection = oWebApp.WebConfigModifications; int iStartCount = oCollection.Count; for (int c = iStartCount - 1; c >= 0; c--) { SPWebConfigModification oModification = oCollection[c]; if (oModification.Owner == owner) oCollection.Remove(oModification); } if (iStartCount > oCollection.Count) { oWebApp.Update(); SPFarm.Local.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); } }

Wednesday, 8 Jul 2009 08:49 by Brandano DiSalle
j4Lwxi Oh hell yeah� no graphical smilies around here.,

Wednesday, 8 Jul 2009 08:50 by Sc Sorensen
Good Thank you It�s very beautifully,

Wednesday, 8 Jul 2009 08:51 by Danny McElroy
nice job, very thanks�,

Wednesday, 8 Jul 2009 08:52 by Brtty Robertson
Good Thank you It�s very beautifully,

Wednesday, 8 Jul 2009 08:52 by Nikone Keosongseng
Don�t you think he is a little late for the game?,

Wednesday, 8 Jul 2009 09:41 by islami chat
thank you very much my friends..

Friday, 10 Jul 2009 11:39 by dwillz
DcvygB nice job, very thanks�,

Monday, 13 Jul 2009 12:10 by jason agulto
OJNdTi Great work �. Thanks for your ideas.,

Monday, 13 Jul 2009 12:10 by Bernard Cissell
Good Thank you It�s very beautifully,

Monday, 13 Jul 2009 12:11 by David Lawson
Don�t you think he is a little late for the game?,

Monday, 13 Jul 2009 12:11 by Kathleen Tjunin
Good Thank you It�s very beautifully,

Monday, 13 Jul 2009 12:12 by Chris Martinek
Very interesting!I�d like to see how far he goes and how well the word will get out.,

Monday, 13 Jul 2009 07:37 by Ravonda James
5XBi3a Oh hell yeah� no graphical smilies around here.,

Monday, 13 Jul 2009 07:37 by Barrie Kiser
Oh hell yeah� no graphical smilies around here.,

Thursday, 30 Jul 2009 01:00 by jeremy srstka
nice job, very thanks�,

Thursday, 30 Jul 2009 01:01 by Brian Hill
Hi! The post is really interesting! I�ve read your blog and can say it�s a good job.thanks,

Thursday, 30 Jul 2009 01:01 by Marinko Novak
Thanks for your insights � I couldn�t agree more.,

Thursday, 30 Jul 2009 01:01 by Roy Reid
Very interesting!I�d like to see how far he goes and how well the word will get out.,

Thursday, 30 Jul 2009 01:02 by C.K. Ku
Thanks for your insights � I couldn�t agree more.,

Thursday, 30 Jul 2009 01:02 by James McCollum
Very interesting!I�d like to see how far he goes and how well the word will get out.,

Thursday, 30 Jul 2009 01:03 by Tracy Schoelman
Good Thank you It�s very beautifully,

Friday, 31 Jul 2009 01:22 by sebastien kreutzmann
Hi! The post is really interesting! I�ve read your blog and can say it�s a good job.thanks,

Friday, 31 Jul 2009 01:23 by Joseph Jenkinson
Very interesting!I�d like to see how far he goes and how well the word will get out.,

Friday, 31 Jul 2009 01:24 by Penny Toy
Oh hell yeah� no graphical smilies around here.,

Friday, 31 Jul 2009 01:24 by ursula sanchez
Don�t you think he is a little late for the game?,

Friday, 31 Jul 2009 01:24 by kirk gillespie
Great work �. Thanks for your ideas.,

Friday, 31 Jul 2009 01:24 by wesley keasler
Hi! The post is really interesting! I�ve read your blog and can say it�s a good job.thanks,

Tuesday, 4 Aug 2009 03:09 by alcak
thanks yours goood free thanks

Saturday, 8 Aug 2009 06:31 by PRoivUQWiPbnzZRmHCQ
tr2.txt;20;50

Thursday, 27 Aug 2009 02:24 by Sai
Initially I also faced problem in removeing the SPWebconfig modifications from the config file. But the follwing link helped me ----- http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebconfigmodification.aspx ---- as per this the name should be parsed based on the value property. Worth to give a try.

Tuesday, 1 Sep 2009 08:55 by Sohbet
thanx Admins

Friday, 9 Oct 2009 07:11 by chat
chat rooms turkish thank you very much..

Tuesday, 17 Nov 2009 03:10 by kayserispor
thanks your article..

Tuesday, 17 Nov 2009 03:11 by ligtv izle
ligtv izle derki bu yazı gerçekten güzel olmuş. ben bi türk webmaster olarak tebrik ediyorum. iyi günler dilerim.

Sunday, 21 Feb 2010 10:32 by Bhushan
Hi, thanks for the great post!! what I wanted to know was that, how you will remove some entries for which you are not owner or the entries which are added by sharepoint itself in web.config?

Thursday, 24 Jun 2010 08:13 by el biler
Great solution Vincent! This is for sure best practice. /Elbiler

Thursday, 24 Jun 2010 08:15 by Marielyst
Thank you Vince! /Marielyst

Thursday, 1 Jul 2010 01:45 by Dave Milner
Vincent, I know this was posted 3 years ago, but it still is the best approach to removing web config modifications in SP 2010 that I could find. I am going to include code heavily based upon your approach in a book I am writing, and I will credit this blog as the source. Rgds, Dave Milner



Url

Email

Comments