Open jarzimichal opened 7 years ago
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 FieldModelHandler
which crafts basic properties for fields including field type. Field type gets extracted from the definition itself.
Hence we did two tricks - crafted our own definition and married it up with existing model handler. If SPMeta2 would complain on not found model handler or mismatch, then get a new model handler inherited:
// 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: http://docs.subpointsolutions.com/spmeta2/extensibility/custom-definition.html
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.