MSDeploy - Changing the ConnectionString parameter in web.config after deploying the package

MSDeploy gives you a lot of awesome deployment options. I’m going to illustrate one of these awesome options below by showing how to add a custom parameter to the package created within the IIS manager.

It is common for a developer to export the package from his development machine, and deploy it to the production server. But, there could be a few changes in the environment of the application, such as the database that is used by the application. The developer would have used a test database, but on the server we need to connect to the production database server. After deployment, you have to change it manually in the web.config file if you doing a copy/paste like the good old days. MSDeploy offers you an automated way of changing few values in the deployed application.

Step 1 : Exporting the package

Open your IIS manager, and click on the application you want to export. Choose “Export application” from the “Actions Pane” in the right hand side, that will give you the below screen.

clip_image001

Click on Next, which will take you to the “Select Parameters” screen where you will see all your parameters added based on your selection.

clip_image002

Now, we need to add a new parameter to replace the connectionstring during the deployment. Click on “Add Parameter”, and fill the details as shown in the image below. The Default Value is going to be your new connection string. You can change it during the deployment as well.

clip_image003

Click OK. So, now we have created a Parameter, we still have to create a ParameterEntry which would define what parameter we are trying to change. Select the Parameter that you have just added, and click on “Add Parameter Entry”. Select XmlFile from the list for the type, and \\web.config$ as the scope, and //connectionStrings/add/@connectionString like below:

clip_image004

Click OK, and then finish the wizard which will create a package for you.

This can also be done using the command line tool msdeploy.exe. Here is an example of the command:

msdeploy -verb:sync -source:iisApp="Default Web Site\TestDBRead" -dest:package=c:\myPackage.zip -declareParam:name=MyParam,kind=XmlFile,scope=\\web\.config$, match=//connectionStrings/add/@connectionString ,defaultValue=Server=myproductionsServer;Database=MyDB

Step #2 : Deploying it on the production server

To deploy the package, you should choose “Import Package” on the website, and select the package file. You should see the connection string parameter that you have added in the wizard like below.

clip_image005

After finishing the import, you should see that the connectionString has been changed with the new value that was specified while creating the package.

Hope this helps!