Skip to content

Instantly share code, notes, and snippets.

@avisra
Created April 13, 2013 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avisra/5376981 to your computer and use it in GitHub Desktop.
Save avisra/5376981 to your computer and use it in GitHub Desktop.
This snippet demonstrates how to extend one of Sitefinity's classes (like Discount) to support dynamic fields.
// If this class is not already setup as a dynamic meta type - set it up. Without this, you cannot add custom fields to the type
if (metaManager.GetMetaType(typeof(Discount)) == null)
{
metaManager.CreateMetaType(typeof(Discount));
metaManager.SaveChanges();
}
// The snippet below allows you to create a new property/field of the specified type (Discount). The benefit to this is that Sitefinity will actually create a new column for "Uses" and this can be saved/read using Sitefinity's functions: discountObject.GetValue<decimal>("Uses") and discountObject.SetValue("Uses", 1)
App.WorkWith()
.DynamicData()
.Type(typeof(Discount))
.Field()
.TryCreateNew("Uses", typeof(decimal)) // This line is what defines the field name and the type of the field. This also determines what column type is created in the database.
.SaveChanges(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment