I recently worked on a client’s Kentico 10 website in which the question of where to store the static URLs of the website, such as the website URL, the company’s main street address, and social media links, came up. In most cases, static URLs like these in question would be put in the Web config. However, in this case there is a better option.
Web config is utilized by technical users and developers, but in most cases, content admin’s want the ability to change content and information as quickly and efficiently as possible. By creating a custom Kentico settings module, we put the control back in the content admin’s hands rather than being bound by the technical requirements of a developer; we give as much control as possible to the client over their own website.
Let’s create an example to demonstrate.
By creating a custom Kentico settings module and populating it with custom settings, we will be able to change the URLs as needed from the Kentico Admin interface in the settings application. Here is a helpful link from the Kentico documentation on creating a custom Kentico module
To begin, open an instance of the Kentico admin interface, then navigate to the settings application, it will look like this.
Duplicate the tab and navigate to the Kentico modules application as shown below.
We are going to create a new Kentico module like the below picture.
Go to the Sites tab and add your desired website from the window as shown. Don’t forget to save the changes.
Navigate to the Settings tab and add a new setting under the “Settings” tree node as shown below.
Add a New Settings Group and name it “Urls”. Save the changes, and your Kentico module should look like this.
Add a new settings key like the following, then repeat the process for as many settings keys as needed; for this demo, we will add several keys.
At this point, the custom Kentico module is complete and all our settings have been added as you can see below.
Flip over to the Kentico settings application and refresh the page to ensure the new module we created appears. Select the new module and ensure all the custom settings we added appear.
From here, open the Kentico project’s codebase so that we can reference the new settings we created. The client in question utilizes the Kentico Model View Controller (MVC) pattern, so we will create a model that contains the calls to pull the settings data. We will use the Kentico SettingsKeyInfoProvider to access the settings as shown below.
For the sake of this example, we will reference the data using a basic view model like this. Normally, we would simply add the needed fields into an existing view model for a view such as a home page view model.
Finally, we can reference the view model we populated in a basic view like the following.
The pattern shown in this example is effective when dealing with periodically changing API tokens, mail servers such as Mail Chimp, or situations in which you are dealing with multiple environments/servers where connection strings are constantly being changed; among other things. Pretty much, if there is a setting that you'd like a content editor to change, then you might want to think about using Kentico Settings rather than web.config keys. You could also use Resource Strings, but I will leave that for a future blog post.
Hopefully, this example will be helpful with your Kentico project, feel free to comment with any questions.