+1 800 256 3608 (toll-free in North America) or +49 7531 90 60 10| service@combit.com

List & Label and Google Drive

Valid from List & Label 20
With List & Label you have the possibility to upload your reports directly to your Google Drive account. We used a PDF file for our test that was created with List & Label. You can change the file format of course. The Google Drive SDK will be used for that. Further information about the Google Drive SDK and the SDK itself (NuGet package) can be found under the link below. Some of the used files are under the Apache 2.0 license - please consider the corresponding licensing terms.

For the following code snippets a simple Microsoft .NET C# Console Application is used. With the command "PM > Install-Package Google.Apis.Drive.v2" in the NuGet Package Manager console the required Assembly references can be added automatically to the C# project:
static void Main(string[] args)
{
    // first: create report (export file) with List & Label
    Console.WriteLine("Creating List & Label Report...");
    string fileToUpload = CreateLLExportFile();
    if (!string.IsNullOrEmpty(fileToUpload) && System.IO.File.Exists(fileToUpload))
    {
        // second: upload the exported file to Google Drive
        Console.WriteLine("List & Label Report was created successfully; now it will be uploaded...");
        if (GoogleDriveUpload(fileToUpload))
        {
            Console.WriteLine("info: file upload successful.");
        }
        else
        {
            Console.WriteLine("error: file upload failed!");
        }
    }
    Console.ReadLine();
}


In the first step a simple export with List & Label will be created with the ExportConfiguration class:

private static string CreateLLExportFile()
{
    string exportedFile = string.Empty;
    using(ListLabel LL = new ListLabel())
    {
        try
        {
            // define licensing info for List & Label - see also PersonalLicense.txt
            LL.LicensingInfo = "<your personal key>";
      
            // first: set the List & Label datasource
            string xmlDataSourceFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "data.xml");
            XmlDataProvider provider = new XmlDataProvider(xmlDataSourceFile);
            LL.DataSource = provider;
      
            // second: configure and execute the export
            exportedFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "LL_GoogleDrive.pdf");
            ExportConfiguration expConfig = new combit.ListLabel20.ExportConfiguration(LlExportTarget.Pdf, exportedFile, "test.lst");
            LL.Export(expConfig);
        }
        catch (ListLabelException ex)
        {
            Console.WriteLine("Information: " + ex.Message + "\n\nThis information was generated by a List & Label custom exception.");
        }
    }
    return exportedFile;
}


(Attached you find a ZIP file that contains the project file (report) and the XML data source used in the sample. These must be placed in the same directory as the created application.)

The created List & Label report can now be uploaded with the Google Drive API. The API however requires several credentials. To obtain your CLIENT_ID and CLIENT_SECRET, activate the Google Drive API first and create an API Access (Create an OAuth 2.0 client ID). Further information and an instruction can be found under the following Google link. Please note the hints in the Google link that the used authentication process is very simplified. Enter this data into your code as follows:

private static readonly string CLIENT_ID = "YOUR CLIENT_ID";
private static readonly string CLIENT_SECRET = "YOUR CLIENT_SECRET";


As soon as the credentials are available the actual upload can be realized with the Google Drive SDK:

private static bool GoogleDriveUpload(string fileToUpload)
{
    bool succeeded = false;
    try
    {
        // first: create/generate credentials
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
          new ClientSecrets
          {
              ClientId = CLIENT_ID,
              ClientSecret = CLIENT_SECRET,
          },
          new[] { DriveService.Scope.Drive }, 
          "user",
          CancellationToken.None).Result;

        // second: create the service with the specified credentials
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Google Drive API Sample",
        });

        // third: upload the file with the specified service
        File body = new File();
        body.Title = "LL Test document";
        body.Description = "A List & Label document";
        body.MimeType = "application/pdf";

        byte[] byteArray = System.IO.File.ReadAllBytes(fileToUpload);
        System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
    
        FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, body.MimeType);
        request.Upload();

        File file = request.ResponseBody;
        if (
            file != null
            && !string.IsNullOrEmpty(file.Id)
            )
        {
          succeeded = true;
        }
    }
    catch (Google.GoogleApiException ex)
    {
        Console.WriteLine("Information: " + ex.Message);
    }
  return succeeded;
}


Hint:
For the one-time authentication, so that an upload can be made to the Google Drive account without any user interaction, the Google Drive API uses a key file, that is stored on your system under “%APPDATA%\Google.Apis.Auth”.

Links:

http://www.combit.net/de/support/files/cmbtkb/KBTD000860.zip
https://developers.google.com/drive/quickstart-cs

Verwandte Artikel:

KBTE000859
IDKBTE000863 KBTE000863