Uncategorized

Cahange Web Config in Sharepoint 2010


In my last task I need to modify the webconfig (Web Configuration) of my web application, as we know that SharePoint Create a webconfig for each web application created by user via by power shall or by using Central Administration or by object model. SharePoint Create a virtual directory of each web application, In my last task I need to create a web part which take a server name of the database and create a dynamic connection with the db server and fetch data. Although I have many other options to create a dynamic connection, but client needs to be save in to the webconfig. In this lab I will explain how to update the webconfig of the web application.

Note :

If you open the 80 folder you can find the web.config file. To add a connection string to the web.config file you can simply add a connectionStrings section between the closing elementand the opening elementin the web.config.

configuration web config

configuration web config

You can read the connection string during runtime in your web part by using the WebConfigurationManager object available in the System.Web .Configuration namespace and use its ConnectionStrings collection to access the element containing the connection string. All you need to do is reference System.Configuration to your code and add a using directive for it.

 

configuration manaer

configuration manaer

Now let’s start to update the webconfig by object model.
Create an Empty SharePoint Project.
(1) This project needs to be farm base solution because we need to have full trusted on our application, because we are accessing the webconfig which is on physical location, sandbox as we know that it has low security privileges.
(2) Add new web part.
(3) Add the following code
(4) SPWebService myService = SPWebService.ContentService;
(5) SPWebConfigModification mymodification = new SPWebConfigModification();
(6) mymodification.Path = “configuration/connectionStrings”;
(6.a) // below we are adding a new key to app setting
(7) mymodification.Name = “add[@name=’MyConnectionString]”
(8) +”[@connectionstring=’Data Source=servername;”
(9) + “Initial Catalog=’TestDB’; ”
(10) +”Persist Security Info=True ”
(11) + “User ID=userName;Password=’Password;’]”;
(12) //
(13) mymodification.Sequence = 0;
(14) mymodification.Owner = “Ownersite”;
//ensure that the appSettings section is added to the Web.config file if it does not already exist
(15) mymodification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
(16) mymodification.Value = “”;
(21) myService.WebConfigModifications.Add(mymodification);
(22) myService.Update();
(23) myService.ApplyWebConfigModifications();

next time turn to event receivers with object model……

Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s