Being new to Umbraco, it's really confusing to understand when to use Umbraco code binaries and when to use Umbraco Rest API.
Our requirement is as follows. We are building an Umbraco website. Along with the pages for the website, the email templates, that will be sent to customers on various occasions, will also reside in Umbraco. These templates will be used to send an email to the customer when they complete certain actions on the website. So far so good.
We also have another bespoke .NET MVC application which will be used by administrators. If a customer contacts them and says they didn't receive a confirmation or an email for something they did on the website described above, the administrators need to have the ability to send that email to the customer. To do that, they need to retrieve the HTML output from the Umbraco Razor template, substitute placeholders with customer data and send the email. To achieve this functionality, we want to develop ASP.NET Web API that this bespoke MVC application can talk to to retrieve the email HTML body. The Web API should query the Umbraco website and return the email template HTML output.
How do I achieve this:? Should I use Umbraco core binaries in the Web API and try to get the output or should I use Umbraco Rest API to retrieve? Has anyone done something like this before? If so please could you provide code samples?
There aren't many code samples for the Umbraco Rest API so it's hard to comprehend under what circumstances they should be used.
If I understand your requirements correctly then I wouldn't go far as using the REST API, which has it's uses for exposes all data from Umbraco on an API but what you want to do is quite small and specific.
In Umbraco you can create a special custom Web API controller, which is routed by convention through Umbraco, and allows you to make use of Umbraco helpers for retrieving Umbraco content.
So if you create something like this in your Umbraco Web application
public class EmailContentController : UmbracoApiController
{
[HttpGet]
public HtmlString GetEmailBody(string emailTypeId)
{
var emailTemplateNode = Umbraco.TypedContent(emailTypeId);
var emailBody = emailTemplateNode.GetPropertyValue<HtmlString>("emailBodyPropertyAlias");
return emailBody;
}
}
Then you would be able to retrieve this content via the url
If your MVC app and Umbraco site live on the same server, then you could also add the Core Umbraco binaries to the MVC app and query the Umbraco site from there.
Thanks for the response. Would just like to clarify this point:
"If your MVC app and Umbraco site live on the same server, then you could also add the Core Umbraco binaries to the MVC app and query the Umbraco site from there."
Does that mean, if the MVC app and the Umbraco site are on different servers, we cna't use this Umbraco core libraries on the MVC app approach to query the content from the umbraco site? What are the limitations? Is it down to authentication or some other issue?
I've been able to use the Code Umbraco binaries and query the site like so:
var template = UmbContentService.GetById(1234);
return template.Properties["emailBody"].Value.ToString();
But what is really needed is the HTML output from the underlying template (razor view). Getting the properties and reconstructing the view in the MVC app would be an overkill.
Please could you guide how the actual rendered HTML output can be retrieved? We also have added complexity that it is a multilingual site and therefore the template (razor view) will reference items from the dictionary. Hence the need to directly retrieve the rendered HTML rather than the bits and pieces and reconstructing it all over again.
and think, if I understand correctly, you can use something like RazorEngine https://antaris.github.io/RazorEngine/ to enable you to send a model to a RazorView and receive the output as html for your email template, but...
since you kind of want all the Umbraco goodness around dictionary, and as your MVC app is kind of separate to Umbraco, I'm thinking you create a new document type called EmailTemplate - use Route Hijacking to create a controller called EmailTemplateController, and create a template that accepts variables from your MVC app, eg current culture / user details.
public class EmailTemplateController : RenderMvcController {
public ActionResult GetEmailTemplate(RenderModel model, string cultureId, UserDetails userInfo)
{
// do stuff here,
var vm = new EmailTemplateViewModel(model);
vm.UserInfo = userInfo;
// Set Culture for current thread...
//create a custom view model for the email
return CurrentTemplate(vm);
}
and have your view inherit the email template model like so:
@inherits UmbracoViewPage<EmailTemplateViewModel>
@{
//build the html for your email with dictionary items etc
}
<h1>Email Template @vm.UserInfo.Name</h1>
Then your MVC app retrieves the html for the body of the email by making a request to your hijacked route, that then will return the html with the dictionary items, and details populated based on the template in Umbraco.
How would we do this if our website is ASP .NET Core? I tried adding UmbracoCms.Core but I guess it isn't .NET Core compatible. I'm interested in a headless approach but not sure how to create the Web API in my project.
Umbraco Core or Umbraco Rest API?
Hello,
Being new to Umbraco, it's really confusing to understand when to use Umbraco code binaries and when to use Umbraco Rest API.
Our requirement is as follows. We are building an Umbraco website. Along with the pages for the website, the email templates, that will be sent to customers on various occasions, will also reside in Umbraco. These templates will be used to send an email to the customer when they complete certain actions on the website. So far so good.
We also have another bespoke .NET MVC application which will be used by administrators. If a customer contacts them and says they didn't receive a confirmation or an email for something they did on the website described above, the administrators need to have the ability to send that email to the customer. To do that, they need to retrieve the HTML output from the Umbraco Razor template, substitute placeholders with customer data and send the email. To achieve this functionality, we want to develop ASP.NET Web API that this bespoke MVC application can talk to to retrieve the email HTML body. The Web API should query the Umbraco website and return the email template HTML output.
How do I achieve this:? Should I use Umbraco core binaries in the Web API and try to get the output or should I use Umbraco Rest API to retrieve? Has anyone done something like this before? If so please could you provide code samples?
There aren't many code samples for the Umbraco Rest API so it's hard to comprehend under what circumstances they should be used.
Please help. Thanks.
Hi Meenakshi
If I understand your requirements correctly then I wouldn't go far as using the REST API, which has it's uses for exposes all data from Umbraco on an API but what you want to do is quite small and specific.
In Umbraco you can create a special custom Web API controller, which is routed by convention through Umbraco, and allows you to make use of Umbraco helpers for retrieving Umbraco content.
So if you create something like this in your Umbraco Web application
Then you would be able to retrieve this content via the url
/umbraco/api/EmailContent/GetEmailBody/?emailTypeId=234
in your external MVC app...
if that helps?
If your MVC app and Umbraco site live on the same server, then you could also add the Core Umbraco binaries to the MVC app and query the Umbraco site from there.
regards
Marc
Hi Marc,
Thanks for the response. Would just like to clarify this point: "If your MVC app and Umbraco site live on the same server, then you could also add the Core Umbraco binaries to the MVC app and query the Umbraco site from there."
Does that mean, if the MVC app and the Umbraco site are on different servers, we cna't use this Umbraco core libraries on the MVC app approach to query the content from the umbraco site? What are the limitations? Is it down to authentication or some other issue?
Thanks.
You can add Umbraco Core binaries to another project, eg class library:
https://www.nuget.org/packages/UmbracoCms.Core/
and bridge the gap between your mvc app and Umbraco content, your app would need to be able to access your Umbraco db.
but I think creating an UmbracoApiController as above is the neatest way to go...
Hi Marc,
Thanks for the response.
I've been able to use the Code Umbraco binaries and query the site like so: var template = UmbContentService.GetById(1234); return template.Properties["emailBody"].Value.ToString();
But what is really needed is the HTML output from the underlying template (razor view). Getting the properties and reconstructing the view in the MVC app would be an overkill.
Please could you guide how the actual rendered HTML output can be retrieved? We also have added complexity that it is a multilingual site and therefore the template (razor view) will reference items from the dictionary. Hence the need to directly retrieve the rendered HTML rather than the bits and pieces and reconstructing it all over again.
Thanks.
ahh I think I understand a little more...
and think, if I understand correctly, you can use something like RazorEngine https://antaris.github.io/RazorEngine/ to enable you to send a model to a RazorView and receive the output as html for your email template, but...
since you kind of want all the Umbraco goodness around dictionary, and as your MVC app is kind of separate to Umbraco, I'm thinking you create a new document type called EmailTemplate - use Route Hijacking to create a controller called EmailTemplateController, and create a template that accepts variables from your MVC app, eg current culture / user details.
and have your view inherit the email template model like so:
Then your MVC app retrieves the html for the body of the email by making a request to your hijacked route, that then will return the html with the dictionary items, and details populated based on the template in Umbraco.
eg
/EmailTemplatesFolder/WelcomeEmail?culture=en-gb&userDetails=
I think that would be the least pain route anyway...
regards
Marc
How would we do this if our website is ASP .NET Core? I tried adding UmbracoCms.Core but I guess it isn't .NET Core compatible. I'm interested in a headless approach but not sure how to create the Web API in my project.
Me too, but I think if you want to use Umbraco API you need to build API into a normal Umbraco Project and call Rest API from .NET Core
is working on a reply...