SaaS CMS has officially launched! Learn more now.

Programmatically updating MetaField attributes



I'm trying to programmatically change some attributes on a MetaField it doesn't persist. For example:

metaClass.Fields["MyField"].Attributes["MaxLength"] = 123;

I don't get any errors from this code, but it doesn't persist the change.

I haven't found any special API's to do this or info on Google about if this is possible.

Thanks, Matt

Feb 15, 2018 8:03

I suppose you are referring to BusinessFoundation MetaClass? My memory is rusty now, but it seems you would have to do like this

var field = metaClass.Fields["MyField"];

field.Attributes["MaxLength"] =  123;


MetaField implements IChangeTracking and you should have to call .AcceptChanges to persit the changes 

Feb 15, 2018 9:29

I must be going crazy, because I can't call .AcceptChanges() for some reason.. I get this compilation error:

'MetaField' does not contain a definition for 'AcceptChanges' and no extension method 'AcceptChanges' accepting a first argument of type 'MetaField' could be found (are you missing a using directive or an assembly reference?)

But this doesn't make any sense since, like you said, if I F12 onto MetaField it clearly implements IChangeTracking but somehow also lacks the AcceptChanges method.. I must be going crazy, doesn't seem to make any sense.

I've also tried casting as IChangeTracking to call AcceptChanges:

var field = metaClass.Fields["MyField"];

field.Attributes["MaxLength"] =  123;


This runs any errors but still doesn't seem to persist the change.

Confused :-/


Feb 15, 2018 11:42

Must be my bad, it might implement ITrackingChange explicitly so you need to cast it to ITrackingChange to call that method.

Not sure why it does not work, will get back to you later 

Feb 15, 2018 12:06

Hi Quan

I don't think you're wrong, Inspecting the MetaField class it definitely does implement IChangeTracking as you said. It does implement the AcceptChanges but, again like you said, you have to cast to IChangeTracking to call the method.

But, for some reason, it's still not working for me :-/ Feel like I must be missinng something - just out of ideas now..

Thanks for helping - let me know if you think of anything else.


Feb 15, 2018 12:22

I even tried this:

var field = metaClass.Fields["MyField"];
field.Attributes["MaxLength"] =  123;

But still no luck

Feb 15, 2018 12:32

I have a little more time to look into this, and this probably the correct way to go

using (MetaClassManagerEditScope editScope = DataContext.Current.MetaModel.BeginEdit())

var field = metaClass.Fields["MyField"];
field.Attributes["MaxLength"] =  123;


Untested by me :) 

Feb 19, 2018 14:50
huan duy - Dec 11, 2022 8:19
As I see, your below code only applies to case meta field is OnBusinessFoundationObject, what if I want to update the meta fields of OrderContext ( eg: OrderContext.Current.LineItemMetaClass). Is EPI support that?


Thanks Quan - have used this in the past but had completely forgotten about it! Now is working exactly as I need :)

Feb 19, 2018 23:33
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.