Update: Code updated to use tracker contact when delivering emails.
When generating an EXM newsletter you might need to get contacts’ details so you can personalise the newsletter further than just OOTB token replacement. For example, render different datasource depending on a contact’s preference.
This post outlines how to get a contact’s ID and the contact’s object from xDB so you can read its data.
The following function retrieves the contact guid in all 3 EXM tabs where the contact is read: message, review and delivery. There are other ways of retrieving this (e.g. querystring) but the following works in every scenario and was recommended by an EXM developer at sitecore (sorry can’t remember who it was but thank you!). Do not forget to see the extension class required at the bottom