Data FAQ

How to create, update, delete static Data Types via C#?

How to create, update, delete static Data Types via C#?

Answer:

Working with structured and reusable data in C1 is handled through Data Types. There are three different types of data types:

  1. Global data: data that is reusable across all pages in all sites that are connected in the site-structure
  2. Page data: “local” data associated with one specific page. Will result in a data folder on the page, in which the editor can store page-specific data
  3. Metadata: a flexible metadata structure that will be assigned to a page or a section of the website

Create a new Data Type:

using System;
using Composite.Data;
using Composite.Data.Hierarchy;
using Composite.Data.Hierarchy.DataAncestorProviders;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;


namespace MySample
{
	[AutoUpdateble]
	[KeyPropertyName("Id")]
	[LabelPropertyName("Name")]
	[ImmutableTypeId("{1EF1F9CC-D14B-4c63-AF4C-662089BFEDEA}")]
	[DataScope(DataScopeIdentifier.PublicName)]
	[DataAncestorProvider(typeof(NoAncestorDataAncestorProvider))]
	[RelevantToUserType(UserType.Developer)]
	public interface ITestCategory : IData
	{
		[ImmutableFieldId("{72A03F88-4A29-4182-9BCF-84080F1A2111}")]
		[StoreFieldType(PhysicalStoreFieldType.Guid)]
		Guid Id { get; set; }


		[ImmutableFieldId("{CAEEA24A-77AB-486c-AE6F-69EB224735A4}")]
		[StoreFieldType(PhysicalStoreFieldType.String, 64)]
		[NotNullValidator()]
		string Name { get; set; }


		[ImmutableFieldId("{BA085CAD-E74E-4e4b-8352-B31D9932EDCC}")]
		[StoreFieldType(PhysicalStoreFieldType.String, 512)]
		string Description { get; set; }
	}
}

Download the source

Check this example of data manipulation from C#:

using  (DataConnection connection = new DataConnection())
{
    // Add data to datatype
    ITestCategory c = DataConnection.New<ITestCategory>();
    c.Id = Guid.NewGuid();
    c.Name = "Test";
    c.Description = "Test description";
    connection.Add<ITestCategory>(c);
    
    // Get all data
    var result = connection.Get<ITestCategory>();
    foreach  (var data in result)
    {
        // Update data
        data.Name = "New Name";
        connection.Update<ITestCategory>(data);

        // Delete data
        // connection.Delete<ITestCategory>(data);
    }
}