Sitefinity Part 3 — Dynamically changing database connection string in Sitefinity

This is the third part of the Sitefinity series. For your convenience you can find other parts in the table of contents in Sitefinity Part 1 — Capturing logs

Sitefinity stores its database connection string in App_data\Sitefinity\Configuration\DataConfig.config so you can easily modify connection string there. But what if you want to extract the connection string in runtime? E.g., you want to read it from Azure Key Vault after application is started?

First, you need to execute some piece of code before actual Webapp executes. Add the following to AssemblyInfo.cs:

This will run your code before anything related to Sitefinity gets chance to work so you can override the connection string in the file:

The file is modified before application reads it so changes are visible in the application without restart.

Tested with Sitefinity version: 10.0.6411.0.