Skip to content

Runtime definition customization

misonou edited this page Sep 14, 2016 · 1 revision

The library allows dynamic customizations when provisioning model definitions to SharePoint entities.

This allows the definitions of content types, fields, lists and views to depend on the SharePoint sites they are creating in; where those customizations cannot be hard-coded.

This is done by deriving the class SPModelProvisionEventReceiver:

// tells the library to pass definitions to the event receiver for customizations
[SPContentType("AFFE42A4D54242DB93DA7D6EAFF0E9DD", "My Content Type",
    ProvisionEventReceiverType = typeof(MySPModelProvisionEventReceiver))]
public abstract class MyItem : SPModel {
    [SPLookupField("CustomLookup")]
    public abstract string CustomLookup { get; set; }
}

// event receiver that handles customizations
// override the suitable method for customizations
internal class MyItemProvisionEventReceiver : SPModelProvisionEventReceiver {
}

For example to provision a lookup field in a content type where the lookup source depends on the site context:

internal class MyItemProvisionEventReceiver : SPModelProvisionEventReceiver {
    public override void OnFieldProvisioning(SPFieldProvisionEventArgs e) {
        // only customize list column
        if (e.ParentList != null) {
            if (e.Definition.InternalName == "CustomLookup") {
                SPLookupFieldAttribute definition = e.Definition as SPLookupFieldAttribute;
                definition.LookupField = "Title";
                definition.LookupListUrl = GetLookupListUrlFromSite(e.Site);
            }
        }
    }
}