I was a little frustrated today for not finding out soon enough about the SchemaFields class. Basically that's the class you need when creating a Schema programmatically using TOM.Net API in SDL Tridion 2011.
Creating the Schema is easy -- just use the new Schema(session, parentTcmUri) constructor, but creating the fields themselves is a bit tricky. You have to use the SchemaFields class, just like you would use ItemFields on a normal Component fields or on any Metadata fields collections.
The code below creates a Schema and adds one RTF field to it and one Keyword based field to its metadata fields:
Having problems creating a Session object? Have a look at my other post describing how to fix Session creation.
Creating the Schema is easy -- just use the new Schema(session, parentTcmUri) constructor, but creating the fields themselves is a bit tricky. You have to use the SchemaFields class, just like you would use ItemFields on a normal Component fields or on any Metadata fields collections.
The code below creates a Schema and adds one RTF field to it and one Keyword based field to its metadata fields:
Session session = new
Session();
Schema schema = new
Schema(session, new
TcmUri("tcm:20-1-2"))
{
Title = "New
Schema",
Description = "Schema
Description"
};
SchemaFields fields = new
SchemaFields(schema);
fields.Fields.Add(new XhtmlFieldDefinition("NewField") {
Description = "New
Field Description",
MinOccurs = 1, //
mandatory
Height = 5
});
KeywordFieldDefinition metaField = new KeywordFieldDefinition("MetaField") {
Description = "New
Meta Field Description",
Category = new Category(new TcmUri("tcm:20-859-512"), session)
};
metaField.List.Type
= ListType.Tree;
fields.MetadataFields.Add(metaField);
schema.Xsd
= fields.ToXsd();
schema.Save(true);
Comments