Click here to Skip to main content
13,357,610 members (64,114 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 10 Oct 2012

Modifying WebConfig File programmatically

, 10 Oct 2012
Rate this:
Please Sign up or sign in to vote.
This article describes how to modyfy the webconfig file of a SharePoint site


While working in SharePoint, I know the difficulties a developer face due to security issues. Here is a simple example how to modify your SharePoint web config file programmatically.


If you are using some external DLL ( third party DLLs) then you may need to modify the "trust level" from wss_minimal to full. In that case you may want to do it programmatically insted of doing this manually.

Here is the code spinet for achieving this task.

Using the code

First of all I am doing all these things in feature activate of the feature.

Just put the following code in your feature activation block.
 // Gets the current webapplication from feature properties.                    

   SPWebApplication webApp = properties.Feature.Parent as SPWebApplication;  

   // Declairs the webConfigmodification variable.

   SPWebConfigModification myModification = newSPWebConfigModification("level", "configuration/system.web/trust");

   //Gets a collection of web config modification.

   System.Collections.ObjectModel.Collection<SPWebConfigModification> allModifications = webApp.WebConfigModifications;


         myModification.Value = "Full";

         myModification.Owner = "TapanKumar";

         myModification.Sequence = 1;    

         myModification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureAttribute; 

         allModifications.Add(myModification); // Add the modifications.



   webApp.Update(); // Update the Web application.

Remember this will work for the whole SPFarm.  If you want it for a single site only then use SPSite insted of SPFarm


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
crb900027-Oct-12 7:16
membercrb900027-Oct-12 7:16 
QuestionWorst Idea Ever Pin
crb900024-Oct-12 16:42
membercrb900024-Oct-12 16:42 
Questionhello Pin
zelinaa11-Oct-12 5:40
memberzelinaa11-Oct-12 5:40 
AnswerRe: hello Pin
Mr. Tapan Kumar18-Oct-12 2:09
memberMr. Tapan Kumar18-Oct-12 2:09 
AnswerRe: hello Pin
Mr. Tapan Kumar18-Oct-12 2:12
memberMr. Tapan Kumar18-Oct-12 2:12 
Question:) Pin
Member 950453611-Oct-12 2:19
memberMember 950453611-Oct-12 2:19 
AnswerRe: :) Pin
Mr. Tapan Kumar18-Oct-12 2:10
memberMr. Tapan Kumar18-Oct-12 2:10 
AnswerRe: :) Pin
Marcus Kramer18-Oct-12 11:01
memberMarcus Kramer18-Oct-12 11:01 
Newshelped me out Pin
Kill the BUG10-Oct-12 23:25
memberKill the BUG10-Oct-12 23:25 
GeneralRe: helped me out Pin
Marcus Kramer18-Oct-12 11:00
memberMarcus Kramer18-Oct-12 11:00 
Generalnice one Pin
tarun kumar sahoo10-Oct-12 23:24
membertarun kumar sahoo10-Oct-12 23:24 
hi It really help me out ............
Thanks a lot
GeneralRe: nice one Pin
Marcus Kramer18-Oct-12 10:57
memberMarcus Kramer18-Oct-12 10:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 10 Oct 2012
Article Copyright 2012 by Mr. Tapan Kumar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid