Page 1 of 1

Programmatically creating Custom Fields

Posted: Thu Oct 22, 2020 2:52 pm
by danvanf
Hello,

I have perused the site for some tips on creating a custom field programmatically. Failing to find a solution, it was time for a new topic.

During the initial startup of a server side app, I'd like to check for the existence of a text custom field, and if it doesn't exist, create it.

I'm not so concerned about checking the existence, we do that all the time, creating it, I've little clue. How is something like that accomplished?

Thanks!

Re: Programmatically creating Custom Fields

Posted: Thu Oct 22, 2020 3:37 pm
by BobRichards
Try this...

Code: Select all

public CreateCustomField(SelectServer ss)
{
    ICustomFieldsManager cfMgr = ss.GetService<ICustomFieldsManager>();

    // Create get a new custom field object and set its attributes.
    ICustomFieldDefinition cf = cfMgr.NewCustomFieldDefinition();
    cf.Context = cfMgr.AvailableContexts.Where(t => t.Name == "Order").First();
    cf.Name = "SpecialDate#";
    cf.Label = "The special date";
    cf.DataType = CustomFieldDataType.Date;

    // Save it to Select.
    cfMgr.ApplyChanges(cf);
}
2020-10-22_15-33-44.png
2020-10-22_15-33-44.png (5.17 KiB) Viewed 2700 times
Other attributes are available by looking up ICustomFieldDefinition in the SDK Help file.

Re: Programmatically creating Custom Fields

Posted: Thu Oct 22, 2020 4:22 pm
by danvanf
Ahh, you make it look so easy, and fast! Thanks a ton. :D