Field

The field managed element is used when declaring fields (list or web scoped). The base class that is used when declaring a field element is the SPGENField class inside the SPGenensis.Core assembly and namespace.

The syntax is:

[SPGENField(
        ID = "{EC23CA6E-BA90-443F-9F3A-6D54889298F4}", 
        DisplayName = "My field 1", 
        Type = SPFieldType.Text, 
        Group = "SPGenesis demo")]
    public class MyField1 : SPGENField<MyField1, SPFieldText, string>
    {
    }


The first part is the class attribute SPGENFieldAttribute that acts like a declaration of well known and defined properties for the field element. The ID attribute is required because this is used to bind the field with one that is used in lists or web scoped fields in SharePoint, or the ID to be used when the field is provisioned (created).

The class declaration holds the reference in code so the field can be used like a type when it's being referenced in code. All functionality is inherited from the base class SPGENField which exposes the Definition properties etc. All methods are exposed through the singleton instance property named Instance.

Head declaration

...
public class MyField1 : SPGENField<MyField1, SPFieldText, string>
...
  • Generic parameter 1: MyField1 is a reference to itself and must be the same as the type being declared.
  • Generic parameter 2: SPFieldText tells the framework which SPField-class it should be casted when using certain framework methods.
  • Generic parameter 3: string tells the framework which .NET-type it should cast list item values to when using certain framework methods.

Definition property

The Definition property exposed by the SPGENField base class holds the properties for this field definition. All properties that are declared by the SPGENFieldAttribute and in the overrided virtual method InitializeDefinition. There is on more source where properties could be read from and that is if the field is boudn to a XML-definition with the SPGENFeatureAssociationAttribute (more of that later on in this document).

The Definition property is accessed like this:
string fieldDisplayName = MyField1.Definition.DisplayName;

Most common properties

ID
This is the field ID.

InternalName
This is the internal name of the field. If the property is not specified and the field properties does not come from a XML-definition, the class name will be used when provisioning the field etc.

StaticName
Same as InternalName

DisplayName
This is the field display name. If the property is not specified and the field properties does not come from a XML-definition, the class name will be used when provisioning the field etc.

Type
This is the field type specified with the SPFieldType enumeration in the Microsoft.Sharepoint namespace.

Group
This is the field group used in web scoped fields.

Field types

Built-in types
Built-in types are fields that ships with SharePoint foundation 2010. The SPFieldType enumeration in the Microsoft.Sharepoint namespace is the source for specifying one of these field types in the Type property.

To specify a field as a Note field, use this syntax:

[SPGENField(
        ID = "{EC23CA6E-BA90-443F-9F3A-6D54889298F5}", 
        DisplayName = "My note field", 
        Type = SPFieldType.Note, 
        Group = "SPGenesis demo")]
    public class MyNoteField1 : SPGENField<MyNoteField1, SPFieldText, string>
    {
    }


Custom field types
If you want to use a custom field type, use the CustomType property:

[SPGENField(
        ID = "{EC23CA6E-BA90-443F-9F3A-6D54889298F6}", 
        DisplayName = "My custom field type", 
        CustomType = "MyCustomFieldType", 
        Group = "SPGenesis demo")]
    public class MyCustomField1 : SPGENField<MyCustomField1, SPFieldText, string>
    {
    }


Lookup fields
Lookup fields needs to have their source list specified as well ass the source field to show. To do this we use the LookupList property and the LookupField property.

The LookupList property can be specified like this:
  • string - the value identifies the source list as a list at the speficied site relative URL. If the string is a GUID represented as ID, it will be used as the list ID instead.
  • type - this is used when a you want to use a list instance declared with the List Instance managed element (see List instance managed element documentation). Use the typeof statement to specify the type.

The LookupField property accepts these values:
  • string - the value identifies the source field to use as the lookup field value. It must be the internal name of the field.
  • type - this is used when you want to use a field declared with the Field managed element. It could for example be a field in a list instance definition or a site columen. Use the typeof statement to specify the type.

To specify a lookup column using a lookup list with the site relative URL Lists/MyLookupList and the Title column as the field shown for this lookup use:

[SPGENField(
        ID = "{EC23CA6E-BA90-443F-9F3A-6D54889298F7}",
        DisplayName = "My lookup field",
        Type = SPFieldType.Lookup,
        LookupList = "Lists/MyLookupList",
        LookupField = "Title",
        Group = "SPGenesis demo")]
    public class MyLookupField1 : SPGENField<MyLookupField1, SPFieldLookup, SPFieldLookupValue>
    {
    }


If the source list is managed list instance element use:
LookupList = typeof(MyListInstance)

If the source field is managed field element use:
LookupField = typeof(MyField)

Last edited May 31, 2011 at 8:22 AM by tore7506, version 9

Comments

No comments yet.