How To Add A Custom Property To The Purchase Order in Episerver Commerce

I've talked previously about Adding a custom field into the commerce customer object via code, Adding Custom MetaData to the LineItem Class in Episerver Commerce via Code and Adding Custom MetaData to the Cart Class in Episerver Commerce via Code. In today's post, I'm going to talk about doing the same for the Purchase Order class.  After a customer has placed an order, you will need to use the Purchase Order class to save information about the order for invoicing, reports etc..

            var name = "ProductName";
            var metaNamespace = string.Empty;
            var friendlyName = "ProductName";
            var description = string.Empty;
            var metaFieldType = MetaDataType.LongString;
            var isNullable = true;
            var length = 0;
            var isMultiLanguage = false;
            var isSearchable = false;
            var isEncrypted = false;

            var metaClass = OrderContext.Current.PurchaseOrderMetaClass;
            if (metaClass.MetaFields.Any(x => x.Name == name))
                return false;

            var metaContext = OrderContext.MetaDataContext;

            var metaField = MetaField.Create(metaContext,

In order to to add a new meta field, we need to use the MetaDataPlus API.  First, we need to check the field doesn't already exist.  If it does, the AddField method will throw an exception.  This is done using OrderContext.Current.PurchaseOrderMetaClass.  As the name suggests, this gives us access to the in-built PurchaseOrderMetaClass metadata class. After we know the field doesn't already exist we can then create it.  This is done by creating a meta field, then using the OrderContext.MetaDataContext in order to save it.  First we use MetaField.Create passing in the parameters we care about, then the result of that is passed into OrderContext.MetaDataContext.AddField() and the rest is done.  You should know using this approach, each field you add needs to be unique.  Even if you tried to add ProductName on the Line Item object, for example, Episerver will throw.


In today's post we've talked about how to create custom fields in the Purchase Order class. This is done using the Metadata Plus API.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

Back to top