-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide custom FieldType support in Field Definitions #1082
Comments
It seems to be hard-coded....
|
Here is what can be done to work around it. This is fast-get-it-done workaround without any elegance. Right now SPMeta2 does not support custom fields unless plain FieldDefinition is used (which limits ability to specify properties such as choices and so on). You still can FieldDefinition with your custom type but it won't be much help in your case. So.. for you to get all benefits of existing choice field (or any other field), a few tricks need to be done. These are legit steps, totally within SPMeta2 supportability. We'd need to inherit our own definition and then match it with either existing or inherited model handler. Some code below, hop that explained well. // create a new class for your definition
// the goal is to override FieldType returning your own thing
public class MyTypeFieldDefinition : MultiChoiceFieldDefinition {
[ExpectValidation]
[ExpectRequired]
[DataMember]
public override string FieldType
{
get { return "MyType"; }
set
{
}
}
}
// register a model handler to handle your definition
// SPMeta2 has one-to-one mapping between definition and model handler
// we pretty much pointing existing model handler for choice field to MyTypeFieldDefinition
// that works same way for CSOM/SSOM model handlers and provision services
csomProvisionService.ModelHandlers.Add(typeof(MyTypeFieldDefinition), new MultiChoiceFieldModelHandler()); MultiChoiceFieldModelHandler exist in both CSOM/SSOM implementations. Check the following namespaces as you need:
Same idea with "Standard" definitions. Internally, these handler would come to a base class Hence we did two tricks - crafted our own definition and married it up with existing model handler. // get CSOM/SSOM model handler for out the box field provision, just override TargetType
public class MyFieldModelHandler : MultiChoiceFieldModelHandler
{
public override Type TargetType
{
get { return typeof(MyTypeFieldDefinition); }
}
}
// Register this instance same way:
csomProvisionService.ModelHandlers.Add(typeof(MyTypeFieldDefinition), new MyFieldModelHandler()); Few ideas on this trick are here: Also, let us know if you guys use model serialisation/deserialisation. In this case, additional attributes and registrations are required to get custom definition serialized. Finally, we'll think on how we can improve custom fields provision and surely let us know your thoughts on that. |
Awesome thanks for very quick reaction! |
Brief description
I would like to use the custom class for FieldType in FieldDefinition.
Currently SPMeta2 works well only when I use one of the BuiltInFieldTypes, however when I populate the FieldDefinition.FieldType with my custom type (class which I have created), the field is not using my type bu just 'Choice' type...
SharePoint API
SharePoint 2016 On Premise
SPMeta2 API
SPMeta2FileVersion:[1.2.17191.0958]
SPMeta2 model
Here is my model for FieldDefinition
here is my custom type code:
The same field works well when I create it from XML. Here is the snippet.
The text was updated successfully, but these errors were encountered: