Saving Custom Data

When customizing the CMS there will come a point where you want to save some data that is not content or media and re-use it.

For this you can use GlobalData, which stores a string with an alias.

public class GlobalData
{
    public Guid Id { get; set; } = Guid.NewGuid().NewSequentialGuid();
    public string? Alias { get; set; }
    public string? Data { get; set; }
    public DateTime DateCreated { get; set; } = DateTime.UtcNow;
    public DateTime DateUpdated { get; set; } = DateTime.UtcNow;
}

You can save data into Global Data using the following method, you need to serialise your data into a string

var saveResult = await Mediator.Send(new SaveGlobalDataCommand
{
    GlobalData = new GlobalData
    {
        Alias = "YourAlias",
        Data = "SerialisedData"
    }
});

Once it's saved it's very easy to get the data out by using this extension method

var mydata= await Mediator.GetGlobalData<MyDataType>("YourAlias");

Global data is cached by default and auto flushed whenever SaveGlobalDataCommand is used. However, if you want to bypass the cache and manually call for the data you can use

var globalData = await mediator.Send(new GetGlobalDataCommand
{
    Alias = "YourAlias", 
    Cached = false
});

Did you know, we use Global Data to power the Global Settings in the CMS

Last updated