Reading custom data

Creating a Data Provider That Reads Custom Data

Let’s assume that you have a data type that lists cultures such as “English (United States)” or “Danish (Denmark)” and you want to expose it to C1 CMS.

The following is a simple sample project that shows how a read-only data provider exposes a sample data interface to C1 CMS. (You can explore its source code and use it for reference when creating your own data providers.)

  1. Download, unzip and build the project: Sample.DataProvider.CultureData.
  2. Copy Sample.DataProvider.CultureData.dll to your C1 CMS’s /bin folder.
  3. Edit /App_Data/Composite/Composite.config and locate the following element: Composite.Data.Plugins.DataProviderConfiguration/DataProviderPlugins
  4. Add the following configuration element below (just before the end tag </DataProviderPlugins> ):
    <add type="Sample.DataProvider.CultureData.CultureDataProvider, Sample.DataProvider.CultureData" name="CultureDataProvider"/>
  5. Log in to the CMS Administrative Console and then execute Tools | Restart Server.

Using Custom Data Provider

Once hooked up, a new data interface become available in C1 CMS (Culture data, Sample.DataProvider.CultureData.ICultureData). And you can use it as you do with any other data types in C1 CMS.

You can:

There are also auto-created widgets for selecting the data (Selector and Optional Selector).

(As an example, you can copy CultureData.xml (a tree definition file) from the project folder to ~/App_Data/Composite/TreeDefinitions of your website to view the exposed data in the Administrative Console | Data perspective.)

The C# project highlights

Let’s have a quick look at the C# project we have used as our data provider's sample:

  • We have created a sample data interface - ICultureData (see ICulture.cs), which we are exposing to C1 CMS via our data provider. This interface inherits from the C1 CMS’s data interface IData and is implemented in the CultureDataItem class (see ICultureDataItem.cs). For information on IData, please refer to "Data Types Using C#".
  • The main part of the project is the CultureDataProvider class (see CultureDataProvider.cs) that implements the C1 CMS’s IDataProvider interface. This data provider delivers data to C1 CMS. (This sample only works as a very simple read only data provider. To be writeable, it must implement IWritableDataProvider instead.)
  • When constructed, the provider is given a DataProviderContext by C1 CMS, which happens before any queries are made. (In this very sample, when the provider is constructed, the data is renewed. Lazy initialization will be more relevant instead.)
  • The provider implements a number of methods that allow its data to be accesses and queried in various ways (IQueryable, IEnumerable, List, a single item).
  • The attribute ConfigurationElementType points to the configuration class for this provider, which allows creating custom configuration values that might be changed in Composite.config to control its behavior. In this sample, the configuration class is kept simple and has no custom configuration (see CultureDataProviderBuildup.cs).

For more information on the sample project’s internals, please refer to its source code with comments.