Getting Started with JavaScript Application

This section explains briefly about how to create a ReportDesigner in your web application with JavaScript.

Project Creation

The following steps displays the Project Creation Wizard in Visual Studio 2013.

Create a new ASP.NET Empty Web application project by selecting the WEB category from the listed project template in Microsoft Visual Studio IDE.

Add References

  1. In the Solution Explorer, right-click the References folder and then click Add Reference.

  2. Add the following Syncfusion assemblies to the project that are necessary for using the report designer control and click OK.

    • Syncfusion.Chart.Wpf
    • Syncfusion.Compression.Base
    • Syncfusion.DocIO.Base
    • Syncfusion.EJ.ReportDesigner
    • Syncfusion.EJ.ReportViewer
    • Syncfusion.Gauge.Wpf
    • Syncfusion.Pdf.Base
    • Syncfusion.Presentation.Base
    • Syncfusion.Shared.Wpf
    • Syncfusion.SfMaps.Wpf
    • Syncfusion.XlsIO.Base

    Refer the above assemblies from the installed location, [Installed Location]:\Program Files (x86)\Syncfusion\Report Platform SDK\Assemblies

  3. Add the following WebAPI assemblies from NuGet package.

    • System.Web.Http
    • System.Web.Http.WebHost
    • System.Net.Http.WebRequest
    • System.Net.Http.Formatting

    The System.Web.Routing and System.Net.Http assemblies are also required, which are referred by default when creating the project.

Create HTML Page

  1. Right-Click on the project and select Add then click New Item.

  2. Select HTML Page from the listed templates and name the page as Default.html.

  3. Click Add.

Add Scripts and Styles

Add the script files and theme files in the <head> tag of the Default.html page.

Themes

  • html
  • <link href="themes/default-theme/ej.web.all.min.css" rel="stylesheet" />
    <link href="themes/default-theme/ej.reportdesigner.min.css" rel="stylesheet" />

    Scripts

    External dependencies
  • html
  • <script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jsrender/0.9.90/jsrender.min.js" type="text/javascript"></script>
    Internal dependencies

    Refer the below scripts to render report designer control.

  • html
  • <script src="scripts/web/ej.web.all.min.js"></script>
    <script src="scripts/web/ej.reportdesigner.min.js"></script>
    Code Mirror

    To edit the SQL queries with syntax highlighter need to refer the below code mirror scripts and themes.

  • html
  • <link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.37.0/codemirror.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.37.0/addon/hint/show-hint.min.css" rel="stylesheet" />
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.37.0/codemirror.min.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.37.0/addon/hint/show-hint.min.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.37.0/addon/hint/sql-hint.min.js" type="text/javascript"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.37.0/mode/sql/sql.min.js" type="text/javascript"></script>

    Use the above code examples while adding scripts and styles.

    Note: Refer the themes and scripts from the installed location [Installed Drive]:\Program Files (x86)\Syncfusion\ReportsSDK\Samples\Common\Javascript\assets

    Control Initialization

    Initialize Report Designer by using the following code example in the <body> tag of the Default.html page.

  • html
  • <div id="container" style="position: absolute; height: 100%; width: 100%;"></div>
    <script type="text/javascript">
        $(function() {
            $("#container").ejReportDesigner({
                serviceUrl: '../../api/ReportDesigner'
            });
        });
    </script>

    Add WebAPI controller for Report Designer

    The JavaScript Report Designer uses WebApi services to process the report file and get the request from control.

    Add Controller

    1. Right-Click on the project and select Add then click New Item.

    2. Select Web API Controller Class from the listed templates and name the controller as ReportDesignerController.cs.

    3. Click Add.

    Inherit IReportDesignerController

    The ApiController should inherit the IReportDesignerController and to process the report file. The interface IReportDesignerController contains the required actions and helper methods declaration to process the report. The ReportDesignerHelper and ReportHelper class contains helper methods that helps to process Post/Get request from control and return the response to control.

    Please add the following code example in ReportDesignerController.cs.

  • c#
  • using System;
    using Syncfusion.EJ.ReportViewer;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using Syncfusion.Reports.EJ;
    using System.Collections;
    using System.Web;
    using Syncfusion.EJ.ReportDesigner;
    using System.IO;
    
    namespace ReportDesignerSample
    {
        public class ReportDesignerController : ApiController, Syncfusion.EJ.ReportDesigner.IReportDesignerController
        {
    
        public string GetFilePath(string fileName)
        {
            string targetFolder = HttpContext.Current.Server.MapPath("~/");
            targetFolder += "Cache";
    
            if (!Directory.Exists(targetFolder))
            {
                Directory.CreateDirectory(targetFolder);
            }
    
            if (!Directory.Exists(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken))
            {
                Directory.CreateDirectory(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken);
            }
    
            var folderPath = HttpContext.Current.Server.MapPath("~/") + "Cache\\" + ReportDesignerHelper.EJReportDesignerToken + "\\";
            return folderPath + fileName;
        }
    
        public object GetImage(string key, string image)
        {
            return ReportDesignerHelper.GetImage(key, image, this);
        }
    
        public object PostDesignerAction(Dictionary<string, object> jsonResult)
        {
            return ReportDesignerHelper.ProcessDesigner(jsonResult, this, null);
        }
    
        public bool UploadFile(System.Web.HttpPostedFile httpPostedFile)
        {
            string targetFolder = HttpContext.Current.Server.MapPath("~/");
            string fileName = !string.IsNullOrEmpty(ReportDesignerHelper.SaveFileName) ? ReportDesignerHelper.SaveFileName : Path.GetFileName(httpPostedFile.FileName);
            targetFolder += "Cache";
    
            if (!Directory.Exists(targetFolder))
            {
                Directory.CreateDirectory(targetFolder);
            }
    
            if (!Directory.Exists(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken))
            {
                Directory.CreateDirectory(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken);
            }
    
            httpPostedFile.SaveAs(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken + "\\" + fileName);
            return true;
        }
    
        public void UploadReportAction()
        {
            ReportDesignerHelper.ProcessDesigner(null, this, HttpContext.Current.Request.Files[0]);
        }
    
        public object GetResource(string key, string resourcetype, bool isPrint)
        {
            return ReportHelper.GetResource(key, resourcetype, isPrint);
        }
    
        public void OnInitReportOptions(ReportViewerOptions reportOption)
        {
            //You can update report options here
        }
    
        public void OnReportLoaded(ReportViewerOptions reportOption)
        {
            //You can update report options here
        }
    
        public object PostReportAction(Dictionary<string, object> jsonResult)
        {
            return ReportHelper.ProcessReport(jsonResult, this as IReportController);
        }
    
        public FileModel GetFile(string filename, bool isOverride)
        {
            throw new NotImplementedException();
        }
    
        public List<FileModel> GetFiles(FileType fileType)
        {
            throw new NotImplementedException();
        }
        }
    }

    WebAPI Routing

    If Global Application Class file already exists in your application skip the below Add Global Application Class section.

    Add Global Application Class

    1. Right-Click on the project and select Add then click New Item.

    2. Select Global Application Class from the listed templates and name it as Global.asax.

    3. Click Add.

    Route WebAPI

    Modify the WebAPI routing in Application_Start event of Global.asax file as follows.

  • c#
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.SessionState;
    using System.Web.Http;
    
    namespace ReportDesignerSample
    {
        public class Global : System.Web.HttpApplication
        {
        protected void Application_Start(object sender, EventArgs e)
        {
            System.Web.Http.GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });
            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);
        }
        }
    }

    On running the application, Report Designer will be rendered like below.

    Integrate the component with Report Server

    Report Designer can be integrated with the Report Server by using below code snippet. After integrating you can open, browse and edit the reports in the Report Server using Report designer.

    Set the Report Server serviceUrl and serviceAuthorizationToken in the ReportDesigner properties.

  • html
  • <div id="container" style="position: absolute; height: 100%; width: 100%;"></div>
    <script type="text/javascript">
       $(function () {
           var dataValue = "";
                var apiRequest = new Object();
                apiRequest.password = "demo";
                apiRequest.userid = "guest";
                $.ajax({
                    type: "POST",
                    url: "http://reportserver.syncfusion.com/api/get-user-key",
                    data: apiRequest,
                    success: function (data) {
                        dataValue = data.Token;
                        var token = JSON.parse(dataValue);
                        $("#container").ejReportDesigner(
                        {
                           serviceUrl: 'http://reportserver.syncfusion.com/ReportService/api/Designer',
                           serviceAuthorizationToken: token['token_type'] + ' ' + token['access_token']
                        });
                    }
                });
       });
    </script>