4
votes

I am storing some email templates(means samples, do not confuse with umbraco templates) in Umbraco back office. At some point I need to send email using this template(subject, body, cc, bcc, etc). For that I use auto generated models, which require IPublishedContent for initalization.

So I cannot initialize this model as I cannot get content of type IPUblishedContent.

When I get it by Id everything works fine. But I do not want to rely on id as it changes and decided to use path and cannot do that. I get NullReferenceException when I execute following:

 contentCache.GetByRoute(true, "relative path to email template")//I think I am mistaken here as I am using cache

relative path looks like "/settings/emailtemplate/registrationtemplate" Can anyone point me how to achieve this as I am new to Umbraco and struggling to understand how should I do certain things.


Another approach which I tried initially(which is also not successful) is to get content by doc type id

 var contentService = umbracoContext.Application.Services.ContentService;
 var contentTypeService = umbracoContext.Application.Services.ContentTypeService;
 var contentType = contentTypeService.GetContentType("emailSettings");
 var templates = contentService.GetContentOfContentType(contentType.Id);

then I get content from ContentService:

 var content = templates.FirstOrDefault(c => c.Name.Contains("Registration"));
 var publishedContent = contentService.GetById(content.Id)

But here I get publishedContent as IContent and there is no way to convert it to IPublishedContent.


Update 1

I found that the reason is that I am trying to get published content from Umbraco API controller. So the question is Is there a way to get published content from api?

1
What is your version of umbraco?Ashkan Sirous
it is Umbraco 7.5.7a-man
On the GetByRoute part, that's for getting a page by it's URL. If there isn't an umbraco page with the URL "/settings/emailtemplate/registrationtemplate" it won't return anything. Does your email template have a corresponding Umbraco page, or iOS it just a standalone template?Tim

1 Answers

9
votes

You didn't mention Umbraco but you can use UmbracoHelper like this.

IPublishedContent content = UmbracoHelper.TypedContent(content.Id);

If you cant have access to UmbracoHelper take it like this:

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);