4

I created a workflow using umbraco and what I am trying to do is: create a workflow that get the html created by a razor template (umbraco.forms).

Here is the locations where is the templates of umbraco.forms

Views\Partials\Forms\Emails This template is what I want to send by email when this workflow is called.

public class SendMail : WorkflowType
{  
    //Here I get the path where the razor template is
    [Umbraco.Forms.Core.Attributes.Setting("Path to template",
     Description = "Path to template",
     View = "TextField")]
     public string Template{ get; set; }
   public SendMail()
   {

    this.Id = new Guid("ccbeb0d5-adaa-4729-8b4c-4bb439dc0204");
    this.Name = "Send Mail";
    this.Description = "Send Mail";
    this.Icon = "icon-plugin";
    this.Group = "Services";
   }
    //When workflow get called this method is executed 
    public override WorkflowExecutionStatus Execute(Record record, RecordEventArgs e)
   {

      //here I'm trying to get the template converted to string however isn't work
     //  string template = html.Raw(Template).ToString();
   }

}

I tried string template = html.Raw(Template); but I have no access to the html.Raw("");

I've tried this solutions but seems that record.ParseWithRazorView(filePath); doesn't exists

There is a workflow that do something close but seems I can't re-write or get access to this code

enter image description here

If you don't understand exactly what I pretend please comment and I will update the question with all details

mxmissile
  • 11,464
  • 3
  • 53
  • 79
Amadeu Antunes
  • 678
  • 1
  • 8
  • 28
  • what about this article? https://www.codemag.com/Article/1312081/Rendering-ASP.NET-MVC-Razor-Views-to-String – pix Sep 13 '19 at 06:38

6 Answers6

4

I recently wanted to send html emails and get the html-conten from a razor page. I had success with this: https://github.com/aspnet/Entropy/blob/master/samples/Mvc.RenderViewToString/RazorViewToStringRenderer.cs

I also allows you to inject a model into the view, so you can dynamically change the rendered html.

You can then invoke it this way:

var html = await myRazorViewToStringRenderer.RenderViewToStringAsync("path/to/view.cshtml", myViewModel);
stipps
  • 41
  • 1
  • 1
1

I tried with RazorLight and here is the code snippet.

private async Task<string> GetMailBody(string teamplteName, object mailObject)
{
    var engine = new RazorLightEngineBuilder()
      .UseFilesystemProject("root path")
      .UseMemoryCachingProvider()
      .Build();

    string result = await engine.CompileRenderAsync(teamplteName, mailObject);

    return result;
}
asathkum
  • 174
  • 1
  • 15
cdev
  • 5,043
  • 2
  • 33
  • 32
1

create this method :

public string RenderRazorToString(string viewName, object model, ViewDataDictionary viewData = null)
{
    ViewData.Model = model;

    if (viewData != null)
        foreach (KeyValuePair<string, object> item in viewData)
            ViewData.Add(item);

    using (var sw = new System.IO.StringWriter())
    {
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);
        viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
        return sw.GetStringBuilder().ToString();
    }
}

and call it every where you want string export of razor's template .

viewName is Your Template name.model is your data if the template needs it.

masoud
  • 355
  • 2
  • 10
1

In your code create helper, that will generate your template.

@helper TemplateGenerator()
{
//generate your template here
}

and after that you can make this code

public HtmlString getTemplateHtml(HelperResult Template)
{
HtmlString str = Template.ToString(); 
}

In this example i use HtmlString, but you can try string, not sure about the result.

In razor you should call your function somehow like that:

@GetTemplateHtml(TemplateGenerator()); 

This works fine for me. Not sure this is the best solution, but i hope it will help

1

Look at something like Postal, this allows you to create and send emails using views, that you can pass your own models etc into.

Tim
  • 4,217
  • 1
  • 15
  • 21
0

I created a View and a umbraco controller

namespace name.Core.Controllers
{
    public class TemplateController : Umbraco.Web.Mvc.SurfaceController
    {
        public ActionResult Template()
        {

            return View();
        }
   }
}

The I pass the URL into "pathtoTemplate"

 public static class TemplateHTML
    {
        public static string GetTemplate(string pathtoTemplate)
        {
            using (WebClient client = new WebClient())
            {
                var encodingbody = client.DownloadData(CaminhoTemplate);
                return Encoding.UTF8.GetString(encodingbody);
            }
        }
    }

var body = TemplateHTML.GetTemplate("http://example.com/umbraco/surface/Template/Template")
Amadeu Antunes
  • 678
  • 1
  • 8
  • 28