Creating custom macros to access external data structure?

Dominic Porter asked on January 25, 2016 18:20

Hi, I am needing to integrate an external parent/child data structure under contacts. There is a 1:M between contacts and the external parent, and then a further 1:M between the external parent and external child.

My thought is to use custom macros to allow users to access the external data in the same way they can access activities under a contact etc. I have experimented with adding custom macro fields to the ContactInfo class; this seems to work ok when returning a simple data type but I'm not sure how I return a collection of the external parent? And then, how would I access the external children for a parent? And for it all to look hierarchically correct in the Macro selection tree?

Has anyone had to do this? Is using custom macro fields the right way or should I be looking at creating a custom info provider, or some other approach? Whichever is the best way, does anyone have some example code to show how it is done?


Recent Answers

Virgil Carroll answered on January 25, 2016 20:59


First let me ask if I am understanding you right. You need to tie external data to the Kentico ContactInfo class, correct? I am assuming this means when a user clicks on a contact they would get an additional information displayed from the external data source? And you want them to be able to click those external data items and pull that detailed information up as well? Or just be transferred to the other system?

Here is a couple of recommendations for you.

First you are probably on a bit of the right track, but I would probably recommend you don't use custom macros. Depending on the number of users you have accessing this system, Macros tend to have quite the resource cost for them in Kentico vs. your own custom code. Using custom code, you could either build your own controls (take a look at the UniGridView control as one that gets used in most of the admin screens) and bind the data to it using how ever your foreign key relationship is and then bind to a detail screen (or transfer to the other system). Using this method will also give you more flexibility in growing into additional requirements as well.

Second, where modifying a class in the Web UI can be easy to do, I would also stray away from doing this alot. We do it on occasion, but you always run into issues with upgradeability. You could, as an alternative, extend the class via the API code (using ovverride) to complete your additional coded needs.

Where I have not done this stuff with the Contact information, I have with extending the ecommerce orders class and if you take a good look at the pattern Kentico has built in their webparts and API code, you can really get a long ways in mimicking it and adding your own functionality.


0 votesVote for this answer Mark as a Correct answer

Dominic Porter answered on January 26, 2016 10:25

Hi Virgil, thanks for your reply. Yes, I am wanting to tie external data to the ContactInfo class. However, I'm not looking to use it in the way you mention. The data represents actions that a contact has done on another system and the reason for recording it in Kentico is so we can reference/test against it when defining scoring, contact groups, marketing automation workflows and website personalisation etc. Macros seem to be available across all these areas and I liked the sentence based user text that you can define over a macro since we'll want to give certain choice users the ability to define their own tests....

Would you think macros are the best way to go for this? If so, I am still stuck on how to return a collection of external objects to Kentico in a macro? Should I use a particular collection type?

I am also a little concerned at your comment that macros can be resource hungry as this is likely to be a relatively high volume system - do you have any statistics on macro performance and tips on how to maximise their performance?

Thanks again

0 votesVote for this answer Mark as a Correct answer

Virgil Carroll answered on January 27, 2016 18:17

Dominic, I apologize I did not see this yesterday. The best way to see the performance of macros vs other elements is to turn on Debugging in Kentico and view the debug output. You will see that almost all macro calls show up in RED and are the slowest of page calls. I am pretty sure this is due to the extra K# language stuff that has to happen. Macros to me are very useful for simple transformation needs and such. I hesitate to use them on a highly transactional site where you are going to get many concurrent users running these macros.

When I mentioned about not wanting to get large amounts of data through macros, again I was referring to not wanting to pull lots of data lots of times over a short period. I would always go in the direction of creating my own custom c# functions for that instead. If you need to use macros, I would just make sure to follow Kentico caching best practices to try and get as much efficiencies as you can in the project.

The one thing I absolutely agree with you is how the sentence-based structure of macros is very easy to use, but you have to weigh it against performance. If these pages are going to have lots of users accessing them at the same time frequently, then using a macro will probably not affect performance all that much.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.