Datasource client

The SDK provides a Datasource client to perform CRUD operations on datasources, also called option lists.

Note

The client variable in the following code examples refers to the IMClient instance. When using the Web SDK, the variable name can be chosen freely, but it is also called client at instantiation in the documentation.

Get datasources

A datasource can be retrieved by name:

RequestResponse
IDataSource result = await MClient.DataSources.GetAsync(name);

This returns an IDataSource object, however it is recommended to downcast it to the right type of datasource in the following way:

RequestResponse
IHierarchicalDataSource hierachicalDataSource = await MClient.DataSources.GetAsync(name) as IHierarchicalDataSource;
RequestResponse
IFlatDataSource flatDataSource = await MClient.DataSources.GetAsync(name) as IFlatDataSource;

It is required to downcast the datasource to the correct type to be able to change the datasource's value. Note that the Type property on IDataSource can be used to identify the correct datasource type at runtime.

Create datasources

To create a hierarchical datasource, an instance needs be created by the factory first. Afterwards, the instance can be saved using the client. The datasource can be used for media types, where every type of media can have more sub-values.

In this example, 'mp4' is added as a subtype of a video media type.

RequestResponse
var dataSource = MClient.DataSourceFactory.CreateHierarchicalDataSource("MediaType");
dataSource.Labels.Add(enUs, "Media type");

var video = new HierarchicalDataSourceValue("Video")
{
  Labels = { { enUs, "Video" } }
};

var mp4 = new HierarchicalDataSourceValue("MP4")
{
  Labels = { { enUs, "mp4" } }
};
video.Values.Add(mp4);

var image = new HierarchicalDataSourceValue("Image")
{
  Labels = { { enUs, "Image" } }
};

dataSource.Values.Add(video);
dataSource.Values.Add(image);

await MClient.DataSources.CreateAsync(dataSource);

Update datasources

In the following example, a flat colors datasource is extended with the color red:

RequestResponse
var dataSource = await MClient.DataSources.GetAsync("Colors") as IFlatDataSource;

var red = new FlatDataSourceValue("Red")
{
  Labels = { { enUs, "Red" } }
};
dataSource.Values.Add(red);

await MClient.DataSources.UpdateAsync(dataSource);

Delete datasources

In the following example, the MediaType datasource is deleted:

RequestResponse
await MClient.DataSources.DeleteAsync("MediaType");

Do you have some feedback for us?

If you have suggestions for improving this article,