In the last blog post, I've written about the creation of custom Settings within the Content Hub. It allows for customization of our code, outside the script. This will give us more flexibility when writing our code. In this blog post, I will show you how we can use the setting within our Scripts.
Before we dive into the code, let us first take a look at the Setting we're going to use. I've created the following setting:
{ "groupMapping": [ { "external": "Azure\\Group A", "internal": "Content Hub Group A" }, { "external": "Azure\\Group B", "internal": "Content Hub Group B" }, { "external": "Azure\\Group C", "internal": "Content Hub Group C" } ] }
Let us move on to the actual coding. For this example, we will create the following script. For this example, I've chosen to use a Shared script so that we can reference and therefore reuse this script within other scripts.
Copy and paste the following code in the Content Hub script:
using Stylelabs.M.Framework.Essentials.LoadConfigurations; using Stylelabs.M.Sdk; using Stylelabs.M.Sdk.Contracts.Base; using System; using System.Threading.Tasks; using Newtonsoft.Json; using Newtonsoft.Json.Linq; public const string SettingPropertyValueKey = "M.Setting.Value"; public async Task<T> ReadSettingAsync<T>(string settingCategory, string settingName) where T : class { if(string.IsNullOrEmpty(settingCategory)) { throw new ArgumentException(nameof(settingCategory)); } if(string.IsNullOrEmpty(settingName)) { throw new ArgumentException(nameof(settingName)); } return await ReadSettingFromContentHubAsync<T>(settingCategory, settingName); } public async Task<T> ReadSettingFromContentHubAsync<T>(string settingCategory, string settingName) where T : class { var settings = await MClient.Settings.GetSettingAsync(settingCategory, settingName, EntityLoadConfiguration.DefaultCultureFull).ConfigureAwait(false); if(settings == null) { throw new InvalidOperationException($"Couldn't read setting {settingCategory} - {settingName}"); } var settingValue = settings.GetProperty<ICultureInsensitiveProperty>(SettingPropertyValueKey); if (settingValue == null) { throw new InvalidOperationException($"Can't read property {SettingPropertyValueKey} value"); } var settingValueJObject = settingValue.GetValue<JObject>(); if (settingValueJObject == null) { throw new InvalidOperationException($"Property value of Setting {settingName} in category {settingName} not found."); } return settingValueJObject.ToObject<T>(); }
Now that we have the basic functionality in place to read and parse settings from the Content Hub, we can create another script that uses this script. Make sure to Build and Publish your script. If it's hasn't been published, you will not be able to reference it properly.
In the new script file, we need to create a reference to the shared script. You can do this by adding the following line of code:
#load "Settings - C.Settings - Read JSON from Setting.csx"
TIP: don't forget to add the .csx file extension at the end. Without it, it will not load. You will get the errors like this.
var model = await ReadSettingAsync<GroupMappings>("Scripting","C.GroupMappings");
Last but not least you need to add the class for GroupMappings to the script. You could of course also put this in a separate shared script if you would like. If you combine it all, we get the following script file:
#load "Settings - C.Settings - Read JSON from Setting.csx" using Stylelabs.M.Sdk; using System; using System.Threading.Tasks; using Newtonsoft.Json; using System.Collections.Generic; try { var model = await ReadSettingAsync<GroupMappings>("Scripting","C.GroupMappings"); } catch(Exception ex) { MClient.Logger.Error(ex.Message); } public class GroupMapping { public string External { get; set; } public string Internal { get; set; } } public class GroupMappings { public List<GroupMapping> GroupMapping { get; set; } }
So this is how you can reuse the shared scripts for reading custom Settings from the Content Hub. I'm sure this is not only going to save time and money but will give you also more flexibility during development. With this flexibility, you can also accommodate settings for multi-sandbox environments.
Happy coding'!