In this tutorial, you will learn how to add custom settings to a Sitecore CMS-powered website. When building a website you will inevitably need to store configuration data somewhere. This raises the question, where is the best place to store settings in Sitecore? Below, you will learn about all the options available to you for settings management. You will also learn how you can access those settings in code. Understanding the most appropriate place to store a setting is key to ensuring you build an optimal architecture for your new project. If you are new to Sitecore and are keen to master settings management, this is the tutorial for you 🔥🔥🔥
Web.Config
As Sitecore is powered by ASP.NET Framework, you can always use the app settings section in your web.config
to store settings:
Using this code you can then access those settings:
When it comes to web.config
my recommendation is to try and keep it as mean and lean as possible in a Sitecore project. The reason for this is upgrading. Upgrading Sitecore CMS is a pain and is not fun. If you want to upgrade your Sitecore instance to a future version, it is pretty much guaranteed you wil need to merge your web.config
with a web.config
built againt the version you would like to upgrade to. When the Sitecore team releases a new upgrade, they tend to add lots of new web.config
configurations, settings and sections. Sitecore is not shy in ripping bits out of the web.config
in later versions. Keeping your web.config
lean will make it that much easier when you need to do this merging later on.
SiteCore Setting
Sitecore also provides an easy API to write your own APP settings file. This can be really useful if you want to deploy your site in different environments. To create your own Sitecore App settings file, first, create a new file within your webroot here:
App_Config
➡ Include
You can call this file whatever you like. The naming convention that I tend to follow is ProjectName.Settings.config
. In your custom settings file, you can add in custom settings by using the following convention:
You can access settings contained within this file, using this code:
If you encounter any issue accessing your settings, you can check the Sitecore config page to make sure your file has been correctly read by Sitecore. You access this page in your web browser by typing in this URL:
www.website.com/sitecore/admin/showconfig.aspx
You can also use the Sitecore API to get access to normal App settings using this code snippet:
You are now a Sitecore config ninja 🐱👤. There are two main ways of getting and setting website settings in Sitecore, web.config
and sitecore.config
. I recommend you favour the Sitecore way as it will make your life slightly easier when you need to upgrade your website later on. Both ways are super simple to access in code, so do what makes you happy ❤️. Happy Coding 🤘