All docs
V20.2
21.1 (EAP/Beta)
20.2
20.1
19.2
The page you are viewing does not exist in version 19.2. This link will take you to the root page.
19.1
The page you are viewing does not exist in version 19.1. This link will take you to the root page.
18.2
The page you are viewing does not exist in version 18.2. This link will take you to the root page.
18.1
The page you are viewing does not exist in version 18.1. This link will take you to the root page.
17.2
The page you are viewing does not exist in version 17.2. This link will take you to the root page.

Register JSON Data Connections

  • 7 minutes to read

This document describes how to provide a set of JSON data connections to the Web Report Designer. The Data Source Wizard displays these connections when users create new JSON data sources.

The Data Source Wizard allows you to create a new JSON data source if a JSON data connection provider is registered in the application. You can register a built-in connection provider or implement and register a custom provider. The built-in connection provider obtains connections from the application's configuration file (Web.config). A custom provider allows you to create connection strings at runtime and use a custom storage to store/load connection strings and credentials.

Use Application Configuration File

Follow the steps below to use connection strings from the application configuration file:

  1. Specify data connections in the application's configuration file (Web.config).

    <!-- ... -->
    <connectionStrings>
        <add name="JsonConnection" connectionString="Uri=https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json" providerName="JsonSourceProvider" />
    </connectionStrings>
    
  2. Register the built-in connection string provider. For this, call the static DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileJsonConnectionStringsProvider method at the application's startup.

    using DevExpress.XtraReports.Web.ReportDesigner;
    // ...
    public class Global : System.Web.HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            // ...
            DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileJsonConnectionStringsProvider();
            // ...
        }
    // ...
    }    
    

Implement a Custom Connection String Provider

A custom connection provider enables you to take full control over JSON connections accessible to end-users:

  • modify available connections at runtime (for example, on a per-user basis)
  • allow users to create new connections
  • validate and store connections in a separate storage
  • store credentials securely
NOTE

To review a sample JSON connection provider and storage implementation, use DevExpress template to create a Web Forms Reporting application and view the following files:

  • Services\CustomDataSourceWizardJsonDataConnectionStorage.cs
  • Services\CustomJsonDataConnectionProviderFactory.cs
  • Global.asax.cs

For more information on DevExpress template review the Create an ASP.NET Web Forms Application with a Report Designer help topic.

To use a custom JSON connection provider in your application, follow the steps below:

1) Create a class that implements the DevExpress.DataAccess.Web.IDataSourceWizardJsonConnectionStorage interface. The code snippet below demonstrates an implementation that stores connections in a session.

TIP

Review the Access HttpContext.Session in Services topic for information on how to use session in methods implemented in a custom connection provider class.

Show code
using DevExpress.DataAccess.Json;
using DevExpress.DataAccess.Web;
using DevExpress.DataAccess.Wizard.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

public class CustomDataSourceWizardJsonDataConnectionStorage : IDataSourceWizardJsonConnectionStorage
{
    public const string JsonDataConnectionsKey = "myJsonDataConnections";

    public Dictionary<string, JsonDataConnection> Connections
    {
        get
        {
            if (HttpContext.Current == null || HttpContext.Current.Session == null)
            {
                return null;
            }
            else if (HttpContext.Current.Session[JsonDataConnectionsKey] == null)
            {
                HttpContext.Current.Session[JsonDataConnectionsKey] = GetDefaults();
            }
            return HttpContext.Current.Session[JsonDataConnectionsKey] as Dictionary<string, JsonDataConnection>;
        }
    }

    bool IJsonConnectionStorageService.CanSaveConnection { get { return HttpContext.Current != null && HttpContext.Current.Session != null; } }
    bool IJsonConnectionStorageService.ContainsConnection(string connectionName)
    {
        return Connections == null ? false : Connections.ContainsKey(connectionName);
    }

    IEnumerable<JsonDataConnection> IJsonConnectionStorageService.GetConnections()
    {
        if (Connections == null)
        {
            return new List<JsonDataConnection>();
        }
        return Connections.Select(x => x.Value);

    }

    JsonDataConnection IJsonDataConnectionProviderService.GetJsonDataConnection(string name)
    {
        if (Connections == null || !Connections.ContainsKey(name))
            throw new InvalidOperationException();
        return Connections[name];
    }

    void IJsonConnectionStorageService.SaveConnection(string connectionName, JsonDataConnection dataConnection, bool saveCredentials)
    {
        // The saveCredentials parameter is always true in Web Reporting.
        if (Connections == null)
        {
            return;
        }
        dataConnection.Name = connectionName;
        dataConnection.StoreConnectionNameOnly = true;
        Connections[connectionName] = dataConnection;
    }

    Dictionary<string, JsonDataConnection> GetDefaults()
    {
        var connections = new Dictionary<string, JsonDataConnection>();
        var dataConnection = new JsonDataConnection(@"Uri=https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json")
        {
            Name = "Customers (JSON)",
            StoreConnectionNameOnly = true
        };
        connections.Add("Customers (JSON)", dataConnection);
        return connections;
    }
}

2) Create a new class (CustomJsonDataConnectionProviderFactory in this example) that implements the DevExpress.DataAccess.Web.IJsonDataConnectionProviderFactory interface. The Designer's preview uses this class to load data. The class collects all JSON connections and provides them to the preview when the preview starts in a different thread. To collect connections, the CustomJsonDataConnectionProviderFactory class uses another class (CustomDocumentViewerJsonDataConnectionProvider) that implements the DevExpress.DataAccess.Json.IJsonDataConnectionProviderService interface.

Show code
using DevExpress.DataAccess.Json;
using DevExpress.DataAccess.Web;
using System.Collections.Generic;
using System.Web;
namespace WebApplication1
{
    public class CustomJsonDataConnectionProviderFactory : IJsonDataConnectionProviderFactory
    {

        Dictionary<string, JsonDataConnection> connections;
        public CustomJsonDataConnectionProviderFactory()
        {
            connections = HttpContext.Current == null || HttpContext.Current.Session == null ? null : HttpContext.Current.Session[CustomDataSourceWizardJsonDataConnectionStorage.JsonDataConnectionsKey] as Dictionary<string, JsonDataConnection>;
        }

        IJsonDataConnectionProviderService IJsonDataConnectionProviderFactory.Create()
        {
            return new CustomDocumentViewerJsonDataConnectionProvider(connections);
        }
    }

    public class CustomDocumentViewerJsonDataConnectionProvider : IJsonDataConnectionProviderService
    {
        readonly Dictionary<string, JsonDataConnection> jsonDataConnections;
        public CustomDocumentViewerJsonDataConnectionProvider(Dictionary<string, JsonDataConnection> jsonDataConnections)
        {
            this.jsonDataConnections = jsonDataConnections;
        }

        JsonDataConnection IJsonDataConnectionProviderService.GetJsonDataConnection(string name)
        {
            return jsonDataConnections == null ? null : jsonDataConnections[name];
        }
    }
}

3) Register services at the application's startup:

Show code
using DevExpress.DataAccess.Web;
using DevExpress.XtraReports.Web.Extensions;
using DevExpress.XtraReports.Web.QueryBuilder;
using DevExpress.XtraReports.Web.QueryBuilder.Native;
using DevExpress.XtraReports.Web.ReportDesigner;
using DevExpress.XtraReports.Web.ReportDesigner.Native;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using DevExpress.XtraReports.Web.WebDocumentViewer.Native;
using System;
using System.Web.SessionState;

namespace WebApplication1
{
    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            ReportDesignerBootstrapper.SessionState = SessionStateBehavior.Required;
            WebDocumentViewerBootstrapper.SessionState = SessionStateBehavior.Required;
            QueryBuilderBootstrapper.SessionState = SessionStateBehavior.Required;
            // ...
            DefaultReportDesignerContainer.RegisterDataSourceWizardConfigFileJsonConnectionStringsProvider();
            DefaultReportDesignerContainer.RegisterDataSourceWizardJsonConnectionStorage<CustomDataSourceWizardJsonDataConnectionStorage>(true);
            DefaultWebDocumentViewerContainer.Register<IJsonDataConnectionProviderFactory, CustomJsonDataConnectionProviderFactory>();
            DefaultQueryBuilderContainer.Register<IJsonDataConnectionProviderFactory, CustomJsonDataConnectionProviderFactory>();
            // ...
        }
    // ...
    }
}

After you register a custom JSON connection provider, you can:

  • use an existing connection to create a new JSON data source:

  • create a new JSON connection:

See Also