Create an export and download a file
1. Overview
This article will guide you through exporting a dashboard, report, or other view type to PDF and then saving it to a file using the REST API. For example, you could embed a view into another web application and trigger the file export from the parent application toolbar.
To create an export and a download a file you will need to do the following:
- Log on and get a session
- Create an export
- Download the export result
2. Log on and get a session
To log on and get a session using the REST API, you will need to make a POST /API/LogOn request. The following is an example of how to accomplish this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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(); // ... } } |
3. Create an export
After getting a session ID, you can 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 view with a particular ID. The providerId is 5752eb39-40b5-4d79-b96b-9f9297c67193 which is the standard PDF export provider ID. The isLegacyExport property is set to true to avoid passing the viewData property.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | string url = string .Format( CultureInfo.InvariantCulture, "{0}/API/Export/" , baseUrl ); 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; // Add an Authorization header (v10 and higher) httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer" , sessionId); // 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( '\"' ); } } |
4. Download the export result
Now that you have the export ID, you can make the following request to GET /Export/Result/{id}/ to get the file from the REST API. The following is an example of how to download the export file, and then save it to C:\Temp\file.pdf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | if (! string .IsNullOrEmpty(exportResultId)) { url = string .Format( CultureInfo.InvariantCulture, "{0}/API/Export/Result/{1}/?isDownloadLink={2}" , baseUrl, exportResultId, false ); using (var response = httpClient.GetAsync(url).Result) { if (response.StatusCode == HttpStatusCode.OK) { byte [] fileBytes = response.Content.ReadAsByteArrayAsync().Result; File.WriteAllBytes( @"C:\Temp\file.pdf" , fileBytes); } } } |
5. Entire code
The following is the entire example together: the sample logs in, creates a PDF export for the view with ID e16138cd-b78f-4c43-9b2d-d0e68f427998, then the file is downloaded and saved:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | 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(); string url = string .Format( CultureInfo.InvariantCulture, "{0}/API/Export/" , baseUrl ); 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; // Add an Authorization header (v10 and higher) httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer" , sessionId); // 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)) { url = string .Format( CultureInfo.InvariantCulture, "{0}/API/Export/Result/{1}/?isDownloadLink={2}" , baseUrl, exportResultId, false ); using (var response = httpClient.GetAsync(url).Result) { if (response.StatusCode == HttpStatusCode.OK) { byte [] fileBytes = response.Content.ReadAsByteArrayAsync().Result; File.WriteAllBytes( @"C:\Temp\file.pdf" , fileBytes); } } } } } |
6. See also
- REST API: /API/LogOn
- REST API: /API/Export
- REST API: /API/Export/Result
- JavaScript API: ExportRequest Class
- Script Library: Export
- File and folder properties
- Share or export your work