Skip to content

Defining column

misonou edited this page Sep 14, 2016 · 3 revisions

SharePoint columns are specified by annotating properties with derived classes of the SPFieldAttribute attribute class.

To implement the property use data getter and setter methods provided by the protected member SPModel.Adapter:

[SPContentType("0x01005A132C01F46341319BBA6C776A2D9D32", "My Item")]
public class MyItem : SPModel {
    [SPTextField("MyRichDescription", Title = "Description")]
    public string Description {
      	get { return Adapter.GetString("MyRichDescription"); }
        set { Adapter.SetString("MyRichDescription", value); }
    }
}

For most cases, if the getter or setter is just simple data read/write to the specified field, you can mark both the class and the property to be abstract. The library will take care for you.

[SPContentType("0x01005A132C01F46341319BBA6C776A2D9D32", "My Item")]
public class MyItem : SPModel {
    [SPTextField("MyRichDescription", Title = "Description")]
    public abstract string Description { get; set; }
}

Choosing attribute and property type

When declaring properties on the model class, make sure the attribute type and property type matches with the field type.

Note: When getting or setting values on properties that does not match with the field types of corresponding fields, the adapter will to parse the value in the retrieved format. However, it is advised to keep field type, attribute type and property type in sync.

Field Type Attribute Type Property Type Adapter Method
Integer SPIntegerFieldAttribute int GetInteger
Number SPNumberFieldAttribute double GetDouble
Currency SPCurrencyFieldAttribute double GetDouble
Text SPTextFieldAttribute string GetString
Url SPUrlFieldAttribute SPFieldUrlValue
string
GetUrlFieldValue
GetString
Note SPNoteFieldAttribute string GetString
Boolean SPBooleanFieldAttribute bool GetBoolean
Guid SPGuidFieldAttribute Guid GetGuid
DateTime SPDateTimeFieldAttribute DateTime?
DateTime
GetDateTime
GetDateOnly
Choice SPChoiceFieldAttribute Enum GetEnum<T>
MultiChoice SPMultiChoiceFieldAttribute Enum
IList<string>
ReadOnlyCollection<string>
GetEnum<T>
GetMultiChoiceFieldValue
GetMultiChoiceFieldValueReadOnly
Lookup SPLookupFieldAttribute string
IList<string>
ReadOnlyCollection<string>
T
IList<T>
ReadOnlyCollection<T>
GetLookupFieldValue
GetMultiLookupFieldValue
GetMultiLookupFieldValueReadOnly
GetModel<T>
GetModelCollection<T>
GetModelCollectionReadOnly<T>
User SPUserFieldAttribute SPPrincipal
IList<SPPrincipal>
ReadOnlyCollection<SPPrincipal>
GetUserFieldValue
GetMultiUserFieldValue
GetMultiUserFieldValueReadOnly
Taxonomy TaxonomyFieldAttribute Term
IList<Term>
ReadOnlyCollection<Term>
GetTaxonomy
GetTaxonomyMulti
GetTaxonomyMultiReadOnly
HTML PublishingHtmlFieldAttribute string GetString
Image PublishingImageFieldAttribute string GetString

Built-in or third-party columns

When associating built-in or third-party columns, use SPBuiltInFieldAttribute as the attribute type regardless of the field types.

When such association is encountered, the built-in or third-party columns will not be modified in site or web level.

[SPContentType("0x01005A132C01F46341319BBA6C776A2D9D32", "My Item")]
public abstract class MyItem : SPModel {
    // "Title" column is a built-in column
    // use `SPBuiltInFieldAttribute` even though we know it is a Text column
    
    // set "Title" column in this content type as a required column
    // this only affects "Title" column under lists that 
    // are provisioned with this content type
    // the site column definition of the "Title" column is unchanged
    [SPBuiltInField(BuiltInFieldNames.Title,
         Required = SPOption.True)]
    public abstract string Title { get; set; }
}

Form Visibility

This refers to on which forms the column is visible to users.

Value Display Form Edit Form New Form Version History
Visible Yes Yes Yes Yes
ExceptEditForm Yes No Yes Yes
ExceptNewForm Yes Yes No Yes
DisplayOnly Yes No No Yes
Hidden No No No No