Applay transformation in email template

Stefan Sturm asked on March 12, 2016 12:23

Hello,

i'm trying to create a dynamic email template and have problems to get the data.

My code (sample):

        // Create email
        var email = new CMS.EmailEngine.EmailMessage();
        email.EmailFormat = CMS.EmailEngine.EmailFormatEnum.Both;
        email.From = template.TemplateFrom;
        email.Recipients = recipients;
        email.Subject = template.TemplateSubject;

        // Add parameters to MacroResolver
        var mcr = MacroResolver.GetInstance();
        mcr.SetNamedSourceData("Name", name);
        mcr.SetNamedSourceData("Email", email);
        mcr.SetNamedSourceData("Telefon", phone);
        mcr.SetNamedSourceData("Firmenname", companyName);
        mcr.SetNamedSourceData("WCCID", wccCheck.ID.ToString());

        // Send Email
        EmailSender.SendEmailWithTemplateText(SiteContext.CurrentSiteName,email,template,mcr,false);

So how can i apply a transformation to this:

        {% "MacroResolver".ApplyTransformation("myTransformation") %} ??

And at least how should the transformation look like:

        //key??
        <div>{%# Eval("..??..") %}</div>
        //value??
        <div>{%# Eval("..??..") %}</div>

Thanks in advice Stefan

Recent Answers


Anton Grekhovodov answered on March 13, 2016 18:06

Hi Stefan,

You have 3 mistakes in your code.

1) You call the method for string "MacroMethod", but you need to call this method for object which data you want to show, e.g. CurrentUser.ApplyTransformation. And you can show all properties of the object in the transformation

2) You can apply transformations of the Text/XML and HTML type via macro expressions. It means that you can use K# syntax to show object properties in the transformation, e.g. for example above to show user first name: {% FirstName %}. But you wrote {% #Eval() %} this is wrong construction, because you mixed K# syntax and ASPX syntax.

Please, see this link https://docs.kentico.com/display/K81/Using+transformations+in+macro+expressions

3) If you add some custom fields to MacroResolver, you can show this fields without transformation in email template. For your code it will be:

<div>{% Name %}</div>
<div>{% Email %}</div>
<div>{% Telephone %}</div>

https://docs.kentico.com/display/K8/Adding+custom+macro+fields

1 votesVote for this answer Mark as a Correct answer

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