Accessing Data with C#


C1 CMS is based on .NET Framework and its data system is based on the CLR types. This implies that you can use LINQ to query, and C# to work with, data in C1 CMS. All C1 CMS data types implement a common interface - Composite.Data.IData and their concrete classes are generated by C1 CMS automatically.

Who Should Read This Guide?

This document is a quick guide intended for C# developers who want to learn how to perform CRUD operations on C1 CMS’s data system using C# and LINQ.

Working with data is the primary focus of this guide. For information about creating your own data types by using C#, please read Data Types Using C#.

Getting Started

The following data operations are covered here:

In the following few chapters, you will learn more about these operations.

We recommend that you first read the following two sections to get familiar with the DataConnection class - the central class in accessing data in C1 CMS - and the way the data types are represented in the C# code.

Data Connection

Before you access data in C1 CMS, you need to establish a connection to the C1 CMS data system by creating an instance of the DataConnection class.

DataConnection connection = new DataConnection();

Listing 1: Connecting to the data store

The DataConnection class is available in the Composite.Data namespace, which you should add to your code with the using directive.

using Composite.Data;

Listing 2: Using Composite.Data namespace

This constructor creates an instance that inherits the current PublicationScope and locale (set on the call stack). When outside an existing scope, they are set to the default Published scope and the default language on the website.

By using the DataConnection instance and static member methods, you can query, add, update and delete data in C1 CMS.

To retrieve data from a specific scope or locale, you should use one of the constructor overloads.

DataConnection connection = new DataConnection(new CultureInfo("da-DK"));

Listing 3: Data connection with a specific locale

This constructor creates a DataConnection instance with a specific locale (and the current or default scope). It uses an instance of CultureInfo, initialized to a specific locale, e.g. “da-DK”.

DataConnection connection = new DataConnection(PublicationScope.Published);

Listing 4: Data connection with a specific scope

This constructor creates a DataConnection instance with a specific scope (and the current or default locale).

The scope of data is defined in relation to its publication status:

  • Data which support publication should always be maintained in the “Unpublihed” scope
  • Reading data on the public website should always be done in the “Published” scope

Normally, C1 CMS itself handles the PublicationScope. You may however choose to explicitly set the PublicationScope (for example, on new service end-points or if you need specific features related to updating or publishing data).

DataConnection connection = new DataConnection(PublicationScope.Published, new CultureInfo("da-DK"));

Listing 5: Data connection with a specific scope and a specific locale

This constructor creates a DataConnection instance with a specific scope and a specific locale and is the combination of the above two.

Classes behind Data Types

Each data type available in the C1 CMS data system is represented by its own class that implements the IData interface and is generated by C1 CMS automatically.

You can access a data item as an instance of such a data type class by referring to it by the full name of the data type, which includes its namespaces.

Demo.Users myUser = DataConnection.New<Demo.Users>();

Listing 6: Creating an instance of the Demo.Users data type

In the above example, you create a data type instance by calling the DataConnection’s static member method New.

You can then access fields of the data type items as properties to set and get values.

myUser.Name = "John Doe";

Listing 7: Setting the Name property of the Demo.Users instance

Data operations are available for both a single instance of a data type and multiple instances represented by an enumerable list of instances that implement the IData interface.