Click here to Skip to main content
Click here to Skip to main content

Modifying WebConfig File programmatically

By , 10 Oct 2012
 

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.
//
 // 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)

About the Author

Mr. Tapan Kumar
Unknown
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1membercrb900027 Oct '12 - 6:16 
See comment "Worst Idea Ever"
QuestionWorst Idea Evermembercrb900024 Oct '12 - 15:42 
Seriously, this is one of the single worst ideas ever. I manage development on a large enterprise installation for the government. If you as a developer don't have access to the server, and you shouldn't unless it is a development server, you as the developer should never override built in protections.
 
If any of my developers suggest anything like this they will be out of a job before they get done explaining it. If I find one of my contract companies trying anything like this, they'd lose their rating to work with the government.
 
By the way, if you are a developer working on a developer server, you should learn to do things the proper way so you have an understanding of the deployment documentation your IT staff will use to deploy your solution the PROPER WAY.
Questionhellomemberzelinaa11 Oct '12 - 4:40 
wow Kumar you are great
AnswerRe: hellomemberMr. Tapan Kumar18 Oct '12 - 1:09 
Hi,
 
Thanks
AnswerRe: hellomemberMr. Tapan Kumar18 Oct '12 - 1:12 
Hi,
If my article helps you really, you can vote for it
Thanks
Tapa kumar
Question:)memberMember 950453611 Oct '12 - 1:19 
Nice one Tapan ...
Keeep it up
AnswerRe: :)memberMr. Tapan Kumar18 Oct '12 - 1:10 
Thanks dear, for reading my article.
AnswerRe: :)memberMarcus Kramer18 Oct '12 - 10:01 
sock puppet.
I wasn't, now I am, then I won't be anymore.

Newshelped me outmemberKill the BUG10 Oct '12 - 22:25 
thanks a lot for sharing this..........
GeneralRe: helped me outmemberMarcus Kramer18 Oct '12 - 10:00 
sock-puppet.
I wasn't, now I am, then I won't be anymore.

Generalnice onemembertarun kumar sahoo10 Oct '12 - 22:24 
hi It really help me out ............
Thanks a lot
GeneralRe: nice onememberMarcus Kramer18 Oct '12 - 9:57 
My vote of 1: Sock puppet vote.
I wasn't, now I am, then I won't be anymore.

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

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