WSS3 Custom web.config settings

When we need to add custom settings or sections to the web.config file of a web application in WSS3 and MOSS, there is a way to do this quite automatically.

If you need several settings that you would like to have added to the web application's web.config when creating/extending the web application, custom xml config files can be used for this.

You can create a file in the format webconfig.[name].xml, put it in the \\Program Files\Common Files\Microsoft Shared\web server extensions\12\CONFIG folder and the settings in the file will be merged with the web.config file on any new web application.
Of course if your web application is already created and you put the file in the \config folder no changes are applied. This is really only when creating/extending new web applications.

Even so, this is a very interesting feature as you can automate any custom settings you would like on all your web applications in WSS3.

In my case I needed a custom appSettings element and a new custom connection string. The XML to be used consists of elements placed into the <actions> elements. The syntax is straightforward:

webconfig.myname.xml contents:

<?xml version="1.0" encoding="utf-8" ?>

<actions>

<add path="configuration/appSettings">

<add key="MyFilePath" value="C:\temp\path\" />

</add>

<add path="configuration">

<connectionStrings />

</add>

<add path="configuration/connectionStrings">

<remove name="MySqlServerConnection" />

<add name="MySqlServerConnection" connectionString="server=[server];database=[db];Integrated Security=SSIP;" providerName="System.Data.SqlClient" />

</add>

</actions>

To deploy this file to the CONFIG you can manually copy the file.

Another approach is adding the file to a solution. A file to be deployed to the CONFIG folder can be inserted by using the <RootFile> element:

manifest.xml example:

<Solution xmlns="http://schemas.microsoft.com/sharepoint/" SolutionId="GUIDHERE">

<RootFiles>

   <RootFile Location="CONFIG\webconfig.myname.xml"/>

</RootFiles>

<!-- rest of solution manifest here
<FeatureManifests>... and other elements
-->

</Solution>

This is my approach to automating web.config settings. Just remember the config settings are only merged with the web.config file when you create a new web application, nothing is changed on existing web applications.
Contrary to the <SafeControls> elements you can use in a solution manifest, these are always merged to the web.config file when you deploy the solution.

MSDN reference for custom configuration settings: http://msdn2.microsoft.com/en-gb/library/ms439965.aspx


 

Technorati tags:

Feedback

Posted on 03 June 2007 @ 03:07

Katrien,

Great post. I still find it a bit inconvenient though that the web application has to be created after you deploy the solution. If the application is already created one way to modify web.config consistently across all farm servers is to use SPWebApplication.WebConfigModifications.

Posted on 03 June 2007 @ 16:14

Mikhail,
I agree with you, this functionality is really nice for new installations and deployments. However, once the application has been created it has no use anymore. I used this method for our first deployment but now that we are working on phase 2 of the project it is no longer an option. Thanks for your tip, I will have a look at the SPWebConfigModification class.

Posted on 22 January 2008 @ 13:09

Hi Katrien,
Its a nice post.

I added the below section in my manifest.xml file as you mentioned in your post. when i try to deploy the solution its giving error as "Error: Cannot find this file specified in the manifest file: CONFIG\webconfig.RMP.xml"
Could you please tell me how to rectify this error.Its urgent......

Posted on 22 January 2008 @ 18:58

Hi Meera,
The error you mention seems to be related to the fact that the file is missing in the .wsp (cabinet) file. To check whether the file is really there you can rename your .wsp file to .zip.
Then you can check if you have a CONFIG folder with the .xml file you mention in the manifest.

Hope this helps,
Katrien

Posted on 24 January 2008 @ 10:18

Hi Katrien
Thank u for your suggestion. Its working fine now.

I have one more problem. when i am creating new webapplication its automatically adding all the tags which i placed in weconfig.RMP.xml file;Except the elements in "Appsettings" tag. They are not adding to the web.config file of webapplication.
What could be the problem. Plz guid me........

Meera

Posted on 27 March 2008 @ 04:52

Hi Katrien,
Great Post, but I am having the same problem as Meera, and I am trying to rename my .wsp to .zip, but it tells me that the 'file is invalid or corrupt' and when i try to extract it, it tells me that there are no files to extract...I know iam missing something here, please help me.

Thanks
Nas

Posted on 23 April 2008 @ 10:00

Actually, .wsp files have the same structure as .cab files. So you may try to rename the file to .cab. What I have also done is, exported the key HKEY_CLASSES_ROOT\.cab from system registry, changed all the occurences of .cab to .wsp and re-imported.

Posted on 12 June 2008 @ 00:00

Katrien,
Thank you very much, i was hitting my head for two days because of "Cannot find this file specified in the manifest file". Now i solved my problem by renaming the *.wsp into *.cab and look into the zipped files.

Thanks you very much for saving my head,
Esen

Posted on 04 August 2008 @ 19:15

"I still find it a bit inconvenient though that the web application has to be created after you deploy the solution."

Why not just use copyappbincontent as described in the MSDN page linked at the bottom of the article?

"You can retroactively apply changes to the Web.config files of the server by running the copyappbincontent Stsadm command-line operation. You must run the operation on each front-end Web server in the deployment."

Posted on 15 August 2008 @ 12:06

Katrien,
Thanks a lot for the great post. Just to add if you wanted to add the web.config changes to your existing web applications you could use "stsadm –o copyappbincontent". This will do the trick

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Copyright © 2007 Katrien De Graeve.