Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get published content from Umbraco API controller ASP.NET

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?

like image 616
a-man Avatar asked Jan 05 '23 17:01

a-man


1 Answers

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);
like image 114
Ashkan S Avatar answered Jan 13 '23 14:01

Ashkan S