Products Technologies Demo Docs Blog Support Company

This blog post contains outdated information.

The cited code snippets may be workarounds, and be part of the official API in the meantime.

Edit Templates in an ASP.NET Core MVC Application (.NET Core)

The ReportingCloud Editor Widget integrates with ASP.NET Core MVC to enable browser-based template editing. The cross-platform application lists templates from cloud storage, loads them into the editor widget via the Web API, and saves modifications back using Base64-encoded data.

Edit Templates in an ASP.NET Core MVC Application (.NET Core)

We just published an ASP.NET Core MVC Web Application that shows how to use the ReportingCloud Web API and the ReportingCloud Editor Widget to edit templates in any browser. This project is targeting .NET Core, is cross-platform and therefore, can be deployed to Windows, macOS, and Linux

The following screenshot shows this demo project:

ASP.NET Core Web Application

This very basic sample lists all templates located in your ReportingCloud template storage. When clicking on a template name, the template is loaded into the editor and saved back to your template storage when pressing the Save Template button.

How Does It Work?

The project uses TXTextControl.ReportingCloud.Core, the Microsoft .NET Core wrapper classes for Text Control ReportingCloud.

The Index action of the HomeController uses ReportingCloud to retrieve all templates using the Templates/List endpoint and returns this list to the view.

public IActionResult Index()
{
    // create a ReportingCloud object with stored API-Key
    ReportingCloud rc = new ReportingCloud(RCSettings.APIKey);

    // return a list of templates
    return View(rc.ListTemplates());
}

In the view, a table is created based on the templates:

@model List<TXTextControl.ReportingCloud.Template>

...

@foreach (var template in Model)
{
    <tr>
        <td><a onclick="LoadDocument('@template.TemplateName')">@template.TemplateName</a></td>
        <td class="text-right"><small>@(template.Size / 1024)&nbsp;KB</small></td>
        <td class="text-right"><small>@String.Format("{0:d}", template.Modified)</small></td>
    </tr>
}

When clicking on a template file name, the following JavaScript calls a controller method to load the selected template from the ReportingCloud template storage. The returned document is then loaded into the editor widget.

function LoadDocument(template) {
  var serviceURL = "/ReportingCloud/Template?TemplateName=" + template;

  // call the "GET Template" controller method with a template name
  $.ajax({
      type: "GET",
      url: serviceURL,
      contentType: 'application/json',
      success: successFunc,
      error: errorFunc
  });

  function successFunc(data, status) {

      streamType = TXTextControl.StreamType.InternalUnicodeFormat;

      // create the proper StreamType based on the extension
      if (template.endsWith("docx")) {
          streamType = TXTextControl.StreamType.WordprocessingML;
      }
      else if (template.endsWith("doc")) {
          streamType = TXTextControl.StreamType.MSWord;
      }
      else if (template.endsWith("rtf")) {
          streamType = TXTextControl.StreamType.RichTextFormat;
      }

      // load the document into the widget
      textControl1.loadDocument(streamType, data);

      // enable the save button and set heading
      $("#saveBtn").removeAttr("disabled");
      loadedDocument = template;
      $("#documentName").text(loadedDocument);
  }

  function errorFunc(data, success) {
      console.log(data);
  }
}

The controller method Template uses the ReportingCloud Web API to retrieve the template and returns it as a Base64 encoded string that can be loaded into the editor.

[HttpGet]
public IActionResult Template([FromQuery] TemplateRequest templateRequest)
{
    // create a ReportingCloud object with stored API-Key
    ReportingCloud rc = new ReportingCloud(RCSettings.APIKey);

    // download document from ReportingCloud template storage
    byte[] document = rc.DownloadTemplate(templateRequest.TemplateName);

    // return Base64 string version
    return new OkObjectResult(Convert.ToBase64String(document));
}

This project doesn't use any references to TX Text Control and uses only ReportingCloud functionality - completely cross-platform.

How to Use the Sample?

In order to use the sample project, you will need to perform some easy steps including the creation of a ReportingCloud account:

  1. Create a ReportingCloud account here:

    https://portal.reporting.cloud/account/register/

  2. In your Manage Account area, create an API Key:

    ASP.NET Core Web Application

  3. In the Widgets area, create a new widget location. If you are testing the sample project on your local machine from within Visual Studio, use localhost as the hostname:

    ASP.NET Core Web Application

  4. Click on Create Tag to create a customized JavaScript tag.

  5. Download the Visual Studio project from GitHub and open it in Visual Studio.

  6. Find the file appsettings.json, open it and add your API Key to the APIKey value. In our sample, this JSON looks like this:

    {
      "ReportingCloudSettings": {
        "APIKey": "HUOJVK04b7e7ZhD15tUS5Xq3iOgEg5hHRRqGMriZ8"
      },
      "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
          "Default": "Warning"
        }
      }
    }
  7. Find the file Index.cshtml in the Views -> Home folder, open it and add replace the JavaScript tag in line 7 with your created JavaScript tag:

    <script src="https://portal.reporting.cloud/Widget/Script/FQ2WTQFTx9HXcouF8xNoBfGhbqbD9TFrKpceM4DWs"></script>
  8. Compile and start the application.

We would love to hear your feedback about ReportingCloud and this cross-platform widget. Contact us to join the conversation.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

Requirements for this sample

  • Visual Studio 2017 or better
  • ReportingCloud account (trial sufficient)

Related Posts

JavaScriptReportingReportingCloud

ReportingCloud Editor Widget Released to Beta

The ReportingCloud editor widget beta embeds a fully featured document editor into any HTML page using a single JavaScript tag. Developers register their web hostname in the ReportingCloud Portal…


AngularReportingBASTA

Integrate Documents in any Platform: Visit us at BASTA! 2018

Text Control marks its 10th BASTA! Fall appearance in Mainz, Germany, presenting the cross-platform document integration campaign. The booth features ReportingCloud with new pricing plans and the…


AngularJavaScriptReact

Technology Preview: Embeddable HTML Widget to integrate Document Editing to…

TX Text Control offers a technology preview of an embeddable JavaScript widget that adds document editing to Angular, React, and other web frameworks. Developers add a script tag and create a…


Reporting.NET CoreReportingCloud

Tutorial: Merging your First Template using the ReportingCloud .NET Core…

This tutorial creates a .NET Core console application in Visual Studio Code that merges a ReportingCloud template. After installing the TXTextControl.ReportingCloud.Core NuGet package and…

Share on this blog post on: