How to Automatically Export Reports to Cloud Storage Services

With List & Label you can export reports in different formats like PDF, Word, Excel and many more. The resulting reports can either be stored directly in the file system or – in .NET – in a stream, in order to transfer them manually into a database, a document management system or similar. Reports can also be automatically stored directly in so-called cloud storage services. The cloud storage providers for GoogleDrive, Microsoft OneDrive or Dropbox are available in List & Label for .NET for this purpose.

In order for the List & Label .NET component to transfer the exported reports directly and automatically to one of the supported cloud storage services, at least one additional assembly reference is required, depending on the target system. The source code for the required assemblies is available on GitHub and can be adapted or extended at will. You can also use the cloud storage providers for scheduled reports in the combit Report Server. Below you will find some examples showing how the providers can be used in your own code.

First, a simple file-based PDF export with List & Label is shown. The report is then uploaded to GoogleDrive as a stream, including the login data, so that no separate authentication by the user is required. For this purpose, a special RefreshToken is required:

...
using (ListLabel LL = new ListLabel())
{
// D:  Datenquelle und Optionen für List & Label definieren
// US: Define data source and options for List & Label
// ...

// D:  Bericht mit List & Label als PDF nach _filePath exportieren
// US: Export report with List & Label as PDF document into _filePath
ExportConfiguration exportConfiguration = new ExportConfiguration(LlExportTarget.Pdf, _filePath, "simple.lst")
{
ShowResult = false
};

LL.Export(exportConfiguration);

// D:  Bericht nach GoogleDrive hochladen
// US: Upload report into GoogleDrive
var stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read);
GoogleDriveUploadParameter googleDriveUploadParameter = new GoogleDriveUploadParameter()
{
MimeType = GetMimeType(_filePathh),
UploadStream = stream,
CloudFileName = Path.GetFileName(_filePath),
CloudPath = "" // empty string means root folder
};

GoogleDriveCredentials googleDriveUploadCredentials = new GoogleDriveCredentials()
{
ApplicationName = "<Application name of your Google App>",
ClientId = "<Client Id of your Google App>",
ClientSecret = "client_secret.json",
RefreshToken = "<Refresh token of your Google App>"
};
LL.UploadSilently(googleDriveUploadParameter, googleDriveUploadCredentials);

stream.Close();
}
...

The following code shows how to upload a stream-based report to Microsoft OneDrive that has been generated manually before. However, this will force the user to enter the logon credentials to Microsoft OneDrive:

...
using (ListLabel LL = new ListLabel())
{
// D:  Datenquelle und Optionen für List & Label definieren
// US: Define data source and options for List & Label
// ...

// D:  Bericht mit List & Label als PDF direkt in einen Stream exportieren
// US: Export report with List & Label as PDF document into stream
var stream = new FileStream(_filePath, FileMode.CreateNew);
ExportConfiguration exportConfiguration = new ExportConfiguration(LlExportTarget.Pdf, stream, "simple.lst")
{
ShowResult = false
};
LL.Export(exportConfiguration);

// D:  Bericht nach Microsoft OneDrive hochladen
// US: Upload report into Microsoft OneDrive
MicrosoftOneDriveUploadParameter microsoftOneDriveUploadParameter = new MicrosoftOneDriveUploadParameter()
{
ApplicationId = "<Application Id of your Microsoft App>",
UploadStream = stream,
CloudFileName = Path.GetFileName(_filePath),
CloudPath = "Documents"
};
LL.Upload(microsoftOneDriveUploadParameter);
}
...

The GoogleDrive and Microsoft OneDrive examples above all show a separate upload of the previously exported report using the upload feature of the respective cloud storage provider. Of course, any files can be uploaded at any time, completely independent of List & Label. With the help of an extension method for the Export function, which each Cloud Storage Provider contains, the export of the report including the upload can be carried out completely in one go. Which Cloud Storage Provider is used depends on the specific parameter. This would look like a direct file-based export including upload to Dropbox:

...
using (ListLabel LL = new ListLabel())
{
// D:  Datenquelle und Optionen für List & Label definieren
// US: Define data source and options for List & Label
// ...

// D:  Optionen für den Export des Berichts definieren
// US: Define export options for the report
ExportConfiguration exportConfiguration = new ExportConfiguration(LlExportTarget.Pdf, _filePath, "simple.lst")
{
ShowResult = false
};

// D:  Bericht direkt nach dem Exportieren nach Dropbox hochladen
// US: Upload the exported report into Dropbox
DropboxExportParameter dropBoxExportParameter = new DropboxExportParameter()
{
CloudFileName = Path.GetFileNameWithoutExtension(_filePath),
CloudPath = "Documents"
};
LL.Export(exportConfiguration, dropBoxExportParameter, "<Your DropBox App Key>");
}
...

In the individual implementations of the Cloud Storage Providers you will find further helpful functions and detailed code documentation. Browse around a bit or even implement new features specifically for your needs. Of course, we are also happy to receive pull requests on GitHub to make updates available to the entire List & Label community.

Related Posts

Leave a Comment