Leverage Message Templates
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.
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);
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.
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 :)
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:
|Subscriber.FirstName|
|Subscriber.LastName|
|Subscriber.Email|
|MailingList.SubscriptionReminder|
|Campaign.MessageSubject|
|Campaign.Name|
|Campaign.FromName|
|Campaign.ReplyToEmail|
|MailingList.Title|
|MailingList.DefaultSubject|
|MailingList.DefaultFromName|
|MailingList.DefaultReplyToEmail|