Click here to Skip to main content
15,868,016 members
Articles / Web Development / ASP.NET
Tip/Trick

Modifying WebConfig File programmatically

Rate me:
Please Sign up or sign in to vote.
4.17/5 (6 votes)
10 Oct 2012CPOL 20.7K   6   12
This article describes how to modyfy the webconfig file of a SharePoint site

Introduction

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.

Background

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.
C#
//
 // 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.

 

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

   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

License

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



Comments and Discussions

 
GeneralMy vote of 1 Pin
crb900027-Oct-12 6:16
crb900027-Oct-12 6:16 
QuestionWorst Idea Ever Pin
crb900024-Oct-12 15:42
crb900024-Oct-12 15:42 
Questionhello Pin
zelinaa11-Oct-12 4:40
zelinaa11-Oct-12 4:40 
AnswerRe: hello Pin
Mr. Tapan Kumar18-Oct-12 1:09
Mr. Tapan Kumar18-Oct-12 1:09 
AnswerRe: hello Pin
Mr. Tapan Kumar18-Oct-12 1:12
Mr. Tapan Kumar18-Oct-12 1:12 
Question:) Pin
Member 950453611-Oct-12 1:19
Member 950453611-Oct-12 1:19 
AnswerRe: :) Pin
Mr. Tapan Kumar18-Oct-12 1:10
Mr. Tapan Kumar18-Oct-12 1:10 
AnswerRe: :) Pin
fjdiewornncalwe18-Oct-12 10:01
professionalfjdiewornncalwe18-Oct-12 10:01 
sock puppet.
I wasn't, now I am, then I won't be anymore.

Newshelped me out Pin
Kill the BUG10-Oct-12 22:25
Kill the BUG10-Oct-12 22:25 
GeneralRe: helped me out Pin
fjdiewornncalwe18-Oct-12 10:00
professionalfjdiewornncalwe18-Oct-12 10:00 
Generalnice one Pin
tarun kumar sahoo10-Oct-12 22:24
tarun kumar sahoo10-Oct-12 22:24 
GeneralRe: nice one Pin
fjdiewornncalwe18-Oct-12 9:57
professionalfjdiewornncalwe18-Oct-12 9: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.