Your custom field type can be added to a list in a browser easily. But how do you add a custom field type via code?
Here is my way:
- add a new field with the field type from which your custom field type derived
- change the field type of the new field to your own custom field type
In my case my custom field type derived from a SPFieldLookup.
1: // create new lookup field
2: string newFieldName = fields.AddLookup("fieldname", list.ID, web.ID, false); 3: var newField = fields.GetFieldByInternalName(newFieldName);
4: // change field type to our own
5: newField.SchemaXml = newField.SchemaXml.Replace("Lookup", "yourFieldType");
Update 4. Feb 2008
The above solution will bring you a field with the type. But a much smoother way is to create a new field, which has the selected type. This way you don’t need to modify the schema of the field.
1: CustomFieldClass field = list.Fields.CreateNewField("CustomFieldClass", "The name of the field");2: list.Fields.Add(field);