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.)
- Download, unzip and build the project: Sample.DataProvider.CultureData.
- Copy
Sample.DataProvider.CultureData.dll
to your C1 CMS’s/bin
folder. - Edit
/App_Data/Composite/Composite.config
and locate the following element:Composite.Data.Plugins.DataProviderConfiguration/DataProviderPlugins
- Add the following configuration element below (just before the end tag
</DataProviderPlugins>
):<add type="Sample.DataProvider.CultureData.CultureDataProvider, Sample.DataProvider.CultureData" name="CultureDataProvider"/>
- 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:
- make LINQ queries against it using CMS Data API (Composite.Data.DataConnection, "How to Query Data Using LINQ")
- create dynamic data fields that reference this data
- use data-centric functions auto-created for XSLT development to query the data and get it as XML (
Sample.DataProvider.CultureData.ICultureData.GetICultureDataXml
) - create Visual Functions based on it
- create "Tree Definitions" based on the data
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 interfaceIData
and is implemented in theCultureDataItem
class (see ICultureDataItem.cs). For information onIData
, 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’sIDataProvider
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 implementIWritableDataProvider
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.