Skip to content

Instantly share code, notes, and snippets.

@davidknipe
Created March 5, 2015 10:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidknipe/3ff4fc7a22fb0b2c30c6 to your computer and use it in GitHub Desktop.
Save davidknipe/3ff4fc7a22fb0b2c30c6 to your computer and use it in GitHub Desktop.
Create an EPiServer Commerce Metafield programmatically and add it to a Metaclass in Business Foundation
/// <summary>
/// Programmatically create a new MetaField on the Contact MetaClass <seealso href="http://world.episerver.com/documentation/Items/Developers-Guide/EPiServer-Commerce/8/Business-Foundation/Business-Meta-Model/MetaField-class/"/>
/// </summary>
private void setupMetaField()
{
MetaClassManager metaModel = DataContext.Current.MetaModel;
foreach (MetaClass mc in metaModel.MetaClasses)
{
if (mc.Name == "Contact")
{
//Delete if needed
//mc.DeleteMetaField(mc.Fields["DOB"]);
string fieldName = "DOB";
if (mc.Fields[fieldName] == null)
{
AttributeCollection attr = new AttributeCollection();
MetaField newMetafield = mc.CreateMetaField(
fieldName,
"Date of Birth",
MetaFieldType.Date,
true,
string.Empty,
attr);
}
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment