Leverage Message Templates

Posted by Community Admin on 04-Aug-2018 02:45

Leverage Message Templates

All Replies

Posted by Community Admin on 04-Oct-2012 00:00

1) I want to allow my users to create templates in the campaigns backend, then leverage them in my code when I need to send notifications.

So this shows creating them in code...but how can I READ them from the backend campaign instead?

...or can I?

2) In a sender profile, how can I define the senders NAME?  I see email, but if I define it like person <person@telerik.com> or something it just throws exceptions.

Posted by Community Admin on 08-Oct-2012 00:00

Hello Steve,

Yes, it is possible to read the campaigns, issues and templates by code.
If I understand you correctly, your users create message templates, and you wish to create sendable campaigns based on those templates.

Please refer to the following code smaple:

private void CreateAndSendCampaignBasedOnTemplate()
    NewslettersManager nlMgr = NewslettersManager.GetManager();
    var template = nlMgr.GetMessageBodies()
            .FirstOrDefault(b => b.Name == "My template");
    var list = nlMgr.GetMailingLists()
            .FirstOrDefault(l => l.Title == "My mailing list");
 
    var newCampaign = nlMgr.CreateCampaign(true);
    newCampaign.Name = "Based on a template!";
    newCampaign.FromName = "Sitefinity campaigns demo";
    newCampaign.List = list;
    newCampaign.MessageBody.BodyText = template.BodyText;
    newCampaign.MessageBody.CopiedTemplateId = template.Id;
    newCampaign.MessageBody.MessageBodyType = template.MessageBodyType;
    newCampaign.MessageBody.PlainTextVersion = template.PlainTextVersion;
    newCampaign.IsReadyForSending = true;
    newCampaign.MessageSubject = "test message";
    newCampaign.ReplyToEmail = "demo@sitefinity-campaigns.com";
    nlMgr.SaveChanges();
 
    var issue = nlMgr.CreateIssue(newCampaign, true);
    nlMgr.SaveChanges();
 
    bool toomanysubs = false;
    nlMgr.SendIssue(issue.Id, out toomanysubs);       
  • First this code uses NewslettersManager to retrieve a message template, specifically by its name ("My template").
  • It also retrieves a specific mailing list which contains defaults for the message delivery and a collection of subscribers to which the campaign can be sent.
  • Next a campaign is created.
    Note that the campaign's MessageBody members are populated by values coped from the template.
  • Also note that the FromName property of the campaign is populated. This is to address your 2nd bullet about specifying the sender's name.
  • The campaign is saved.
  • An issue based on the campaign which was just created is being created and saved.
    At this point all the values from the campaign are copied automatically to the issue.
  • The issue is now ready to be sent. It's possible to also send it directly by code, as demonstrated here.

This code should work fine as long as the template's bodies are plain text or rich text based. If a template is "like a web page" there are additional steps to be taken in order to copy the template's content to the created campaign and/or issue.
Please contact us for more information about web page templates or any additional help about this matter.


Kind regards,
Alon Rotem
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 09-Oct-2012 00:00

Hey Alon!
  Thanks for the detailed reply.  However I'm looking for this to be WAY more simple :)

I just want the message templates to be a repository literally for "Templates" (since it already has the built-in UI for replacement items like names....also beats having an external directory for .txt files in App_Data or something.  So in this case a user does something on site site which triggers a "Notification" that something happened (like add a follower on twitter).  So instead of me hardcoding the message I want to empower the user to change\tweak on their end as needed...this seems like a good UI to do that :)


So I think using your below GetMessageBodies I'll be good!

(quick followup though)
1) Do you forsee see issues with me doing this?  Like it won't interfere with actual campaigns or anything?
2) If the above looks good, and I can leverage the page editor style template UI...I'm sure putting the code in here or a KB article\blog post (*cough*SLAVO*cough*) would help not just me, but future devs.
3) Can we add items to the replace list in the template UI in Advanced Settings by any chance (Insert dynamic data in the text)?  Like if I wanted to add in Nickname or something else (even to handle in my code, not by the email campaigns)?

Steve

Posted by Community Admin on 09-Oct-2012 00:00

Hi Steve,

I'm glad you find this solution helpful.
Indeed, the call GetMessageBodies may not be intuitive for retrieving email campaign templates, but templates are merely stored as message body objects.
There is one difference, though, between a message body of a campaign/issue (stored as a member of a Campaign object) and that of a template (not stored as a member of another object). The MessageBody class exposes the IsTemplate boolean property in order to differentiate between the two.

As for your notes:

  1. I do not foresee any issue with using this code, except the issues you can create and send via Sitefinity (pun intended). After all this is the same API Sitefinity uses in order to create and send campaigns and issues.
    Once you create a campaign/issue, you should be able see them in the Campaigns grid along with the correct status text denoting the number of issues and the state of the latest issue (sent, draft etc).
    Note that unlike the code below, you don't always have to create a new campaign. You can use a single campaign with multiple issues (however a campaign can have only 1 unsent issue in draft mode. After the issue is sent, a new issue can be created). The decision on how to implement this according to your needs is up to you.
  2. Thank you for the tip. I will recommend this to our SDK team.
  3. When you create an campaign/issue, you may edit the message body text and/or plain text version in any way you wish, prior to sending it. You can replace texts as you need.
    There are built-in auto-replaced text keys which can be inserted into the body of the message and those will be replaced automatically upon delivery:
    |Subscriber.FirstName|
    |Subscriber.LastName|
    |Subscriber.Email|
    |MailingList.SubscriptionReminder|
    |Campaign.MessageSubject|
    |Campaign.Name|
    |Campaign.FromName|
    |Campaign.ReplyToEmail|
    |MailingList.Title|
    |MailingList.DefaultSubject|
    |MailingList.DefaultFromName|
    |MailingList.DefaultReplyToEmail|
I hope this info helps achieve what you need.

Kind regards,
Alon Rotem
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed