Create an Export and Download a File

Contents[Hide]

This article will guide you through exporting a dashboard to PDF and then saving it to a file. To create an export and a download a file you will need to do the following:

  • Logon and get a session
  • Create an export
  • Download the export result

1. Logon and get a session

To logon and get a session you will need to make a POST /API/LogOn request to the REST API. The following is an example of how to accomplish this:

using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.Globalization;

// ... 

string baseUrl = "http://localhost:8010";

var cookieContainer = new CookieContainer();

string sessionId = string.Empty;

using(HttpClientHandler httpClientHandler = 
	new HttpClientHandler())
{

       httpClientHandler.CookieContainer = cookieContainer;

	using (HttpClient httpClient = 
		new HttpClient(httpClientHandler))
	{
		// Get Session Id
		string logonUri = 
			string.Concat(baseUrl, "/Api/LogOn/");
			
		var logonOptions = new
		{
			accountName = "admin",
			password = "1234",
			cultureName = string.Empty,
			deleteOtherSessions = true,
			isWindowsLogOn = false
		};
	
		JavaScriptSerializer serializer = new JavaScriptSerializer();
		var requestBodyAsString = serializer.Serialize(logonOptions);
		StringContent content =
			new StringContent(
				requestBodyAsString,
				Encoding.UTF8,
				"application/json"
			);
			
		string jsonString = string.Empty;
	
		using (var response = httpClient.PostAsync(logonUri, content).Result)
		{
			jsonString =
				response.Content.ReadAsStringAsync().Result;
		}
	
		var obj = 
			(Dictionary<string,object>) 
				serializer.DeserializeObject(jsonString);
		
		sessionId = 		
		        obj["sessionId"].ToString();

// ...
          }
}

Tip
For multiple examples of REST logon visit the POST /LogOn/ page in the REST API documentation.

2. Create an Export

After getting a session ID you can now create an export. This will be accomplished by making a POST /API/Export call to the REST API. The following example creates an export for the dashboard with ID = 'e16138cd-b78f-4c43-9b2d-d0e68f427998'. The providerId is '5752eb39-40b5-4d79-b96b-9f9297c67193' which is the PDF provider ID. The isLegacyExport property is set to true to avoid passing the viewData property.

string url = 
	string.Format(
		CultureInfo.InvariantCulture,
		"{0}/API/Export/?sessionId={1}",
		baseUrl,
		sessionId
	);

var exportRequestOptions =
	new {
		__classType = "dundas.export.ExportRequest",
		isLegacyExport = true,

		// PDF Exporter
		providerId = "5752eb39-40b5-4d79-b96b-9f9297c67193",
		viewId = "e16138cd-b78f-4c43-9b2d-d0e68f427998"
	};

requestBodyAsString = serializer.Serialize(exportRequestOptions);

string exportResultId = string.Empty; 

// Define the request body
HttpContent requestBody = null;
requestBody = new StringContent(requestBodyAsString,Encoding.UTF8,"application/json");
using (var response = httpClient.PostAsync(url, requestBody).Result)
{
	
	if(response.StatusCode == HttpStatusCode.OK)
	{
		Console.WriteLine("Success");

		string jsonObject = response.Content.ReadAsStringAsync().Result;
		
		exportResultId = 
			jsonObject.Trim('\"');
	}

}

3. Download the Export Result

Now that you have the export ID you can request the file from the Dundas BI web application. In order to do this you will need to set the session ID in a cookie under the name dundas_webapp_sessionid with the value being the valid session ID and set for the base url. Then a GET request is made to the following:

{BaseUrl}/Export/?exportId=00000000-0000-0000-0000-000000000000

The following is an example of how to set the cookie, download the export file, and then save it to C:\Temp\file.pdf:

if(!string.IsNullOrEmpty(exportResultId))
{
	string downloadUrl =
		string.Concat(baseUrl, "/Export/?exportId=", exportResultId);

	cookieContainer.Add(new Uri(baseUrl), new Cookie("dundas_webapp_sessionid", sessionId));

	using(var response = httpClient.GetAsync(downloadUrl).Result)
	{
		if(response.StatusCode == HttpStatusCode.OK)
		{
			byte[] fileBytes = 
				response.Content.ReadAsByteArrayAsync().Result;
		
			File.WriteAllBytes(@"C:\Temp\file.pdf", fileBytes);
		}
	
	}
	
}


4. Entire Code

The following is the entire example together, the sample logs in, creates a PDF export for the dashboard with ID = 'e16138cd-b78f-4c43-9b2d-d0e68f427998', then a cookie is set, and the file is download and saved:

using System.Net;
using System.Net.Http;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.Globalization;

// ...

string baseUrl = "http://localhost:8010";

var cookieContainer = new CookieContainer();

string sessionId = string.Empty;

using(HttpClientHandler httpClientHandler = 
	new HttpClientHandler() { CookieContainer = cookieContainer })
{

	using (HttpClient httpClient = 
		new HttpClient(httpClientHandler))
	{
		// Get Session Id
		string logonUri = 
			string.Concat(baseUrl, "/Api/LogOn/");
			
		var logonOptions = new
		{
			accountName = "admin",
			password = "1234",
			cultureName = string.Empty,
			deleteOtherSessions = true,
			isWindowsLogOn = false
		};
	
		JavaScriptSerializer serializer = new JavaScriptSerializer();
		var requestBodyAsString = serializer.Serialize(logonOptions);
		StringContent content =
			new StringContent(
				requestBodyAsString,
				Encoding.UTF8,
				"application/json"
			);
			
		string jsonString = string.Empty;
	
		using (var response = httpClient.PostAsync(logonUri, content).Result)
		{
			jsonString =
				response.Content.ReadAsStringAsync().Result;
		}
	
		var obj = 
			(Dictionary<string,object>) 
				serializer.DeserializeObject(jsonString);
		
		sessionId = 		
				obj["sessionId"].ToString();
					
		string url = 
			string.Format(
				CultureInfo.InvariantCulture,
				"{0}/API/Export/?sessionId={1}",
				baseUrl,
				sessionId
			);
		
		var exportRequestOptions =
			new {
				__classType = "dundas.export.ExportRequest",
				isLegacyExport = true,
		
				// PDF Exporter
				providerId = "5752eb39-40b5-4d79-b96b-9f9297c67193",
				viewId = "e16138cd-b78f-4c43-9b2d-d0e68f427998"
			};
		
		requestBodyAsString = serializer.Serialize(exportRequestOptions);
		
		string exportResultId = string.Empty; 
		
		// Define the request body
		HttpContent requestBody = null;
		requestBody = new StringContent(requestBodyAsString,Encoding.UTF8,"application/json");
		using (var response = httpClient.PostAsync(url, requestBody).Result)
		{
			
			if(response.StatusCode == HttpStatusCode.OK)
			{
				Console.WriteLine("Success");
		
				string jsonObject = response.Content.ReadAsStringAsync().Result;
				
				exportResultId = 
					jsonObject.Trim('\"');
			}
		
		}
			
		if(!string.IsNullOrEmpty(exportResultId))
		{
			string downloadUrl =
				string.Concat(baseUrl, "/Export/?exportId=", exportResultId);
		
			cookieContainer.Add(new Uri(baseUrl), new Cookie("dundas_webapp_sessionid", sessionId));
		
			using(var response = httpClient.GetAsync(downloadUrl).Result)
			{
				if(response.StatusCode == HttpStatusCode.OK)
				{
					byte[] fileBytes = 
						response.Content.ReadAsByteArrayAsync().Result;
				
					File.WriteAllBytes(@"C:\Temp\file.pdf", fileBytes);
				}
			
			}
			
		}

	}
}

5. See Also

Dundas Data Visualization, Inc.
500-250 Ferrand Drive
Toronto, ON, Canada
M3C 3G8

North America: 1.800.463.1492
International: 1.416.467.5100

Dundas Support Hours: 7am-6pm, ET, Mon-Fri