.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Updating Multiple Front End Servers Configuration in SharePoint

Posted By:Jean Paul       Posted Date: May 21, 2013    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

In this article we will explore the problems with updating multiple web configuration file and the usage of SPWebConfigModification model to resolve it.
 


In this article we can explore how to update multiple web configuration files in a Farm environment.  The configuration file which we are referring is the IIS (Internet Information Services) web application configuration file.  It is residing in the IIS folder.

For example: C:\inetpub\wwwroot\wss\VirtualDirectories\80\web.config

Abstract

SharePoint 2010 Farm Deployment may involve multiple Web Front End servers.  For example a typical farm environment is as following:

·  2 Web Front End Servers
·  1 Database Server
·  1 Search Index Server
In this case any update to web.config file should be done in the 2 servers.  Manually updating both the servers is tedious and not recommended once we are deploying a feature to a user. 

Solution


We can use the SPWebConfigModification server object model for this purpose.

Following are some of the entry types possible with this:

·  Safe Control
·  Connection String
·  Application Setting

SPWebConfigModification is residing in the namespace Microsfot.SharePoint.Administration.

Note: The configuration modification through SPWebConfigModification server object model is the recommended one as it performs a safe updating.

Important Properties


Following are the important properties of SPWebConfigModification class:

1.  Name to hold name of attribute / section
2.  Value to hold value of the item
3.  Path to hold the X Path to the node
4.  Sequence to specify the position of the entry

Adding a Safe Control


Following is the code for adding a Safe Control entry into the web.config.  Create a new SharePoint 2010 Console Application and execute the following code which creates an entry for 'TestAssembly'.

SPWebService service = SPWebService.ContentService;

SPWebConfigModification modification = new SPWebConfigModification();
 modification.Owner = "OwnerNameHere";
 modification.Path = "configuration/SharePoint/SafeControls";
 modification.Name = "SafeControl[@Assembly='TestAssembly'][@Namespace='TestNameSpace'][@TypeName='*'][@Safe='True']";
 modification.Sequence = 0;
 modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
 modification.Value = "<SafeControl Assembly='TestAssembly' Namespace='TestNameSpace' TypeName='*' Safe='True' />";

 service.WebConfigModifications.Add(modification);
 service.ApplyWebConfigModifications();
 service.Update(); 



Later you can open the web.config file and see the new entry there.



Code Explained


The SPWebConfigModification class instance holds the modification information like:

·  Name
·  Value
·  Path
·  Owner
·  Sequence

The WebConfigModifications property contains all the modification entries. (Modifications stored in the Database).  We are adding our modification entry into this collection.
The ApplyWebConfigModifications () applies all the Modifications to the servers in farm.

The Update() method saves the WebConfigModifications into the Content database.  This will help to re-deploy all the modifications in future.

Note: Please make sure you have set the Platform Target of the project as .Net 3.5 Framework.

Adding a Connection String


Now let us try adding a connection string into the web.config files of multiple servers in the farm.  Create a new method in our Console Application and add the following code to it.


private static void CreateConnectionString()
 {
   SPWebService service = SPWebService.ContentService;

   SPWebConfigModification modification = new SPWebConfigModification("CS1", "configuration/connectionStrings");
   modification.Owner = "OwnerZ";
   modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
   modification.Value = "<add name=\"CS1\" connectionString=\"Data Source=SERVER;User ID=USER;Password=PASSWORD;Initial Catalog=DBNAME;\" providerName=\"System.Data.SqlClient\"/>";

   service.WebConfigModifications.Add(modification);
   service.ApplyWebConfigModifications();
   service.Update();
 }

Later you can open the web.config file and see the new entry there.



Please note that the above code is performing the following:

·  Specify the Web Service
·  Specify the Modification Name and X Path
·  Specify the add tag with Connection String
·  Add to Modifications list
·  Apply Modification
·  Save Modifications list to content database

Note:  If you face any error saying the section does not exists, you can manually create an empty connection string section in the configuration file.


Adding an Application Setting


We can also add an Application Setting to the configuration file.  Create a new method named CreateAppSetting() inside the console application we have and use the following code for adding an application setting to the web application configuration file.

private static void CreateAppSetting()
 {
   SPWebService service = SPWebService.ContentService;

   SPWebConfigModification modification = new SPWebConfigModification("AppSetting", "configuration/appSettings");
   modification.Owner = "OwnerZ";
   modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
   modification.Value = "<add key=\"MYKEY\" value=\"MYVALUE\" />";

   service.WebConfigModifications.Add(modification);
   service.ApplyWebConfigModifications();
   service.Update();
 }

Later you can open the web.config file and see the new entry there.



Please note that the above code is performing the following:

·  Specify the Web Service
·  Specify the Modification Name and X Path
·  Specify the add tag with KEY & VALUE
·  Add to Modifications list
·  Apply Modification
·  Save Modifications list to content database


Importance of Owner Property


We are using the Owner property (which is a string) to specify the owner name of the modification.  In future the specified property can be used to filter modifications based on the Owner.

So setting the Owner property gives the following advantages:
·  Finding Modification entries by Owner
·  Updating Modification entries by Owner
·  Removing Modification entries by Owner
Following is the example code that will remove modification entries by owner.
  
private static void RemoveByOwner(string owner)
{
   SPWebService service = SPWebService.AdministrationService;

   service.WebConfigModifications.Where(m => m.Owner == owner).ToList().
   ForEach(m => service.WebConfigModifications.Remove(m));

   service.Update();
}


Under the Hood


Each of the Configuration Modification entry is stored in the SharePoint Content database.  This is to keep track of all the changes made using the SPWebConfigurationModification class.
Please note that there could be different SharePoint solutions creating configuration modification entries.

Why History of Modifications is maintained?


You might be wondering that why the history of modifications is maintained.  This is to apply all the changes to multiple front end servers.
Each of the Configuration Modification entry is stored in the Content database.  Later point of time when a new server is added to the farm, a quick updating is possible.
Without storing the entries in database it is difficult to parse a valid server configuration file and replicate the changes to another.


Various Locations of web.config


Please note that there are various locations for the web.config files depending on the context and element.

SharePoint Port 80 Application web.config is residing in:

  C:\inetpub\wwwroot\wss\VirtualDirectories\80

SharePoint Central Administration Application web.config is residing in:

  C:\inetpub\wwwroot\wss\VirtualDirectories\[Port of Central Administration]

SharePoint Port 80 Application Web Part web.config is residing in:

  C:\inetpub\wwwroot\wss\VirtualDirectories\80\wpresources

Additionally there is a CONFIG folder inside the 14 Hive

  C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG
This folder contains the following types of files:
1.  web.config
2.  *.config
3.  webconfig.*.xml



More information on these can be found through the References section.

Note The Safe Control entries can also be deployed to the CONFIG folder as a webconfig.*.xml file.  The entries will be merged with the web.config file. 

References


http://tinyurl.com/sp2010-spwbcfg

Summary


In this article we have explored the problems with updating multiple web configuration file and the usage of SPWebConfigModification model to resolve it. 
Following are the points worth noting:

·  SPWeConfigModification can be used to create modification entries
·  Update() method will save the entry to database
·  Multiple Modification entries are tracked in the database
·  ApplyWebConfigModifications() method applies the modification entries in database to servers in farm
·  Adding / Updating / Deleting of configuration entries is little tricky

In real world scenarios this model should give an advantage edge for the developer and it is the recommended way for doing configuration modification. 

Download the source code contains the example we have discussed.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend