How to Use Deployment Slot Specific Config Files
When leveraging Deployment Slots for your PaaS Sitecore environment you may want differing configs per Deployment Slot. This article discusses why you would want differing configs and how to setup custom config files so they are applied per Slot. This assumes you have a single deployment source that can be deployed across differing targets such as a Production and Preview Slot… or even separate CD Servers utilized for Production or Preview.
Why Use Config Files Scoped to Deployment Slots
With deployment slots, you may have configurations that are specific to the slot, such as Solr indexes that are unique to a database (ex. Preview Database with Preview indexes). A custom configuration would allow you to target rebuilding indexes in Preview without affecting your Production slot.
Basically, anything tied to the database connected to your deployment slot that requires separation may be a candidate for a scoped configuration file. A common use case is a Preview slot with its own content that may not be published to the Production slot yet until the content is reviewed. This may be preferred over an in-place workflow on the Production slot for a full fidelity view of a website within the Preview slot.
Setup Your AppSettings Key
Within the <appSettings configBuilders=”SitecoreAppSettingsBuilder”> section of your web.config, set a key that defines something distinct such as “database” and set the value to the database in question such as “preview”.
This “key” is what you will reference in your custom config files akin to a role:require key entry. In my case, the connectionString file has the preview database defined and each slot in the Azure PaaS App Service has its own unique database and unique Solr indexes (ex. Preview slot with Preview Database and Preview Solr Indexes).
<add key="database:define" value="preview" />
Use the Key in Your Configuration File
Once you have your key established, you can use it like the role:require or any other key in your web.config to scope your configuration to only apply to the slot with the database that it is tied to, such as the preview slot that has the preview database.
An example for a custom Content Delivery Solr configuration that houses blog information in one of its Solr indexes is as follows. Note how it is scoped to only apply if the config is deployed on the slot with a preview database.
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/" xmlns:environment="http://www.sitecore.net/xmlconfig/environment/" xmlns:database="http://www.sitecore.net/xmlconfig/database/">
<sitecore role:require="ContentDelivery" environment:require="PROD" database:require="preview" >
<contentSearch>
<configuration type="Sitecore.ContentSearch.ContentSearchConfiguration, Sitecore.ContentSearch">
<indexes hint="list:AddIndex">
<index id="sitecore_blog_preview_index" type="Sitecore.ContentSearch.SolrProvider.SolrSearchIndex, Sitecore.ContentSearch.SolrProvider">
<param desc="name">$(id)</param>
<param desc="core">$(id)MainAlias</param>
<param desc="propertyStore" ref="contentSearch/indexConfigurations/databasePropertyStore" param1="$(id)" />
<configuration ref="contentSearch/indexConfigurations/BlogSolrIndexConfiguration" />
<strategies hint="list:AddStrategy">
<strategy ref="contentSearch/indexConfigurations/indexUpdateStrategies/onPublishEndPreviewAsyncSingleInstance" role:require="Standalone or (ContentManagement and Indexing) or (ContentDelivery and Indexing)" />
</strategies>
<locations hint="list:AddCrawler">
<crawler type="Sitecore.ContentSearch.SitecoreItemCrawler, Sitecore.ContentSearch">
<Database>preview</Database>
<Root>/sitecore</Root>
</crawler>
</locations>
<enableItemLanguageFallback>false</enableItemLanguageFallback>
<enableFieldLanguageFallback>false</enableFieldLanguageFallback>
</index>
</indexes>
</configuration>
</contentSearch>
</sitecore>
</configuration>