Created
April 13, 2013 04:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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