Briefly about custom macro fields

#Custom software development #Kentico development #.NET #Kentico Kontent

SHARE

In this post I want to provide a bit more details regarding how to add custom macro fields to existing object types.

In Kentico documentation we have good explanation how to create and use custom macro field for the existing object type. But documentation examples often use hardcoded username value to retrieve, for example, user information. In real life things can be more complicated.

Let's consider the scenario. We have Kentico Order object type. And we created custom module and classes to manage customer's credit cards information (for example it can be last four digits for credit card number, credit card expiration date, etc.). Custom object type to store credit card information is called CreditCard. Also additional column was added to the object type Order to store credit card ID just to know which credit card customer used to pay for order. Now we need to display credit card information in order invoice or sent emails to customer with order details and credit card information used for payment. So what is the best way to insert this information into invoice? I think the answer is to use custom macros.

As credit card object is tied to order by ID so credit card object information can be field of order object. The main issue was to identify context of specific order object and get credit card ID stored within the currently used order object (for example, when we edit order information and view order invoice). The code sample below demonstrate how to register custom field for credit card.

[assembly: RegisterExtension(typeof(CustomMacroFields), typeof(OrderInfo))]

public class CustomMacroFields : MacroFieldContainer
{
    protected override void RegisterFields()
    {
        base.RegisterFields();
        
        RegisterField(new MacroField("CreditCard", (context) =>
        {
            //context.Resolver.SourceObject is used to retrieve data of currently used object
            ShoppingCartInfo si = context.Resolver.SourceObject as ShoppingCartInfo;
            if (si != null && si.Order != null)
            {
                return CreditCardDataInfoProvider.GetCreditCardDataInfo(si.Order.GetIntegerValue("OrderCreditCardID", 0));
            }

            return context.DefaultValue;
        }));
    }
}
 

So as you can see the example is quite simple, but the main focus is how to get appropriate object data from context parameter of type EvaluationContext. Now all properties of Credit card object can be easily retrieved as macro fields.

Author

Check other articles

5 5

What our clients say

Bits Orchestra team are outstanding developers‚Äč. They listen carefully to our business needs and easily turns our business objectives into a well thought out and executed development effort. Roman is very bright and definitely the most capable developer that has worked on our site. He is not only a Kentico expert but has successfully tackled other complicated development assignments demonstrating expertise in both front and backend development. Roman takes initiative to suggest enhancements that make site maintenance easier while improving the customer experience. The team is very responsive to our work requests and has great follow up. They have also worked very business partners and this has reflected positively on our company. Roman is a true partner for us and a tremendous asset to our organization. We will continue to work with them and would highly recommend Roman and his team for your development needs. He and his team will exceed your expectations!
 Alan Lehmann
Alan Lehmann
President at In energy sector

What our clients say

The Bits Orchestra team does excellent work. They are always available and I appreciate our frequent calls and screen-shares together. Their dedication to the projects and knowledge of Kentico is outstanding. They truly care about the quality of their work, and became a part of our team easily!
Shena Lowe
Shena Lowe
Managing Partner at Consensus Interactive

What our clients say

We hired Roman for a Kentico analysis project and have been very satisfied. He is very skilled and professional. We are looking to hire him and his team again on future projects.
Sylvain Audet
Sylvain Audet
CEO at MyDevPartner.com

What our clients say

Roman and team have taken over an existing Kentico EMS site for a large US Oil Company. So far, they have handled every single request that we have thrown at them and these were diverse, challenging, often bespoke, usually urgent and almost daily, over the last 11 months. Their work is of an extremely high quality, they are capable, quick and we have great confidence in the support that we are getting.
Jon Hollis
Jon Hollis
Head of Web Development at confidential

What our clients say

Bits Orchestra team was very helpful, they had a good understanding of the brief and deep knowledge of the system. They were always keen to provide advice and recommendations that benefit the project substantially.
Ramon Lapenta
Ramon Lapenta
Senior Front End Developer at Cyber-Duck Ltd