-
-
Notifications
You must be signed in to change notification settings - Fork 353
Defined Types
You can think of DefinedTypes as a kind of dictionary of possible values for a particular thing. The particular thing is the 'defined type'. In Rock, developers can define types of reusable fields and their possible values using this generic entity (DefinedType
and DefinedValue
; stored in the respective tables of the same name). For example, there is a well-known DefinedType
called Record Type which has the following DefinedValues
: Person and Business. There is another type called Record Status which has these values: Active, Inactive, and Pending.
DefinedTypes are named, have a description, can have a category, can be ordered, and can also be further specified by a FieldType. DefinedValues are named, can have a description, and be ordered.
NOTE: By convention, any entity that has a property that ends with *
ValueId
is a reference to a DefinedValue.
Instead of having to create many miscellaneous entities and tables to hold these various dictionary-lookup values, developers can simply rely on DefinedTypes in the Rock framework to manage this data.
You'll probably have cases when you want to store a selected value (DefinedValue of a particular DefinedType) as an entity's attribute value.
Example: A person attribute that stores the ability level of the person. In this case the ability level is a DefinedType with multiple DefinedValues. One of the selected DefinedValues is stored as value of an attribute (also called "ability level" for consistency) on the Person entity.
To retrieve the stored ability level (its name) for a particular person:
// get the person's saved DefinedValue
var valueId = person.GetAttributeValue( "AbilityLevel" );
// now get the name of that particular DefinedValue
var name = Rock.Web.Cache.DefinedValueCache.Read( Int32.Parse( valueId ) ).Name;
Or, building on the above, perhaps you need to create a drop down list while pre-selecting the one that was previously selected/stored for the person:
ListControl ddlAbilityLevels = new DropDownList();
// bind the ability level defined type to a drop down list
var abilityLevelDtGuid = new Guid( "D8672146-C14F-41E8-A143-242361CECCD3" );
ddlAbilityLevels.BindToDefinedType( DefinedTypeCache.Read( abilityLevelDtGuid ) );
ddlAbilityLevels.SelectedValue = valueId; // valueId is from the previous example