OneSpan Sign How To: Download Documents Within A Date Range

Haris Haidary,

When completing a document package (transaction in the new UI), you have the capability of downloading your signed documents. Currently, OneSpan Sign will keep all completed packages indefinitely. Thus, you can download your signed documents anytime you wish. However, you may also want to keep a copy of the signed documents on your end. For example, company policy may oblige you to print hard copies of the signed documents, or you may simply want to keep them on your own databases. The simplest way is to go through the OneSpan Sign web portal and download your signed documents there. Though, this process can be long and tedious if you have a large numbers of packages in your account. In this blog, I will show you how to leverage the download functionality in order to download your signed documents within a date range.

 

 

OneSpan Developer Community

OneSpan Developer Community

Join the OneSpan Developer Community! Forums, blogs, documentation, SDK downloads, and more.

Join Today

The Code

You can go ahead and skip to the section which applies to your method. I will cover the same information in each segment. You can get the complete example code from the Developer Community Code Share: Java, .NET, and REST.

Java SDK

I'll start with the Java SDK. The first step is to define your date range you want to retrieve your documents.

Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_MONTH, -7);
Date from = cal.getTime();
Date to = new Date();

Then, you retrieve your packages within your date range with OneSpan Sign's PackageService(), which comes as a page from OneSpan Sign. In the sample code below, the number of packages returned in the PageRequest is set to 50, which is the maximum number of packages you can retrieve per page. The first value in the PageRequest (variable ‘index’) is the starting point in the overall list of packages that should be returned. This variable is incremented as you step through your packages to keep track of where we are in the overall list of packages.

Page<DocumentPackage> completed_packages = client.getPackageService().getUpdatedPackagesWithinDateRange(PackageStatus.COMPLETED, new PageRequest(index, 50), from, to);

Next, you download your zip file from each of the completed packages in the page returned by OneSpan Sign. Once all the zip files have been downloaded, you update your page to retrieve the next 50 packages until all the packages have been retrieved within your date range.

while (index <= completed_packages.getNumberOfElements())
        {
            for (DocumentPackage pack : completed_packages)
            {
            	System.out.println("Downloading package: " + pack.getId() + "...");
                byte[] zipContent = client.downloadZippedDocuments(pack.getId());
                Files.saveTo(zipContent, "package-documents-"+ pack.getId().toString() +".zip");
                index++;
                Thread.sleep(1000);
            }
            completed_packages = client.getPackageService().getUpdatedPackagesWithinDateRange(PackageStatus.COMPLETED, new PageRequest(index, 50), from, to);
        }

.NET SDK

Next, let's go over the .NET SDK. The first step is to retrieve your packages within your date range with OneSpan Sign's PackageService(), which comes as a page from OneSpan Sign. In the sample code below, the number of packages returned in the PageRequest is set to 50, which is the maximum number of packages you can retrieve per page. The first value in the PageRequest (variable ‘index’) is the starting point in the overall list of packages that should be returned. This variable is incremented as you step through your packages to keep track of where we are in the overall list of packages.

Page<DocumentPackage> completed_packages = eslClient.PackageService.GetUpdatedPackagesWithinDateRange(DocumentPackageStatus.COMPLETED, new PageRequest(index, 50), DateTime.Today.AddDays(-7), DateTime.Now);

Next, you download your zip file from each of the completed packages in the page returned by OneSpan Sign. Once all the zip files have been downloaded, you update your page to retrieve the next 50 packages until all the packages have been retrieved within your date range.

while (index <= completed_packages.TotalElements)
            {
                foreach (DocumentPackage package in completed_packages)
                {
                    Debug.WriteLine("Downloading package: " + package.Id + "...");
                    byte[] zipContent = eslClient.DownloadZippedDocuments(package.Id);
                    File.WriteAllBytes(Directory.GetCurrentDirectory() + "/package-documents-"+package.Id.ToString()+".zip", zipContent);
                    index++;
                    Thread.Sleep(1000);
                }

                completed_packages = eslClient.PackageService.GetUpdatedPackagesWithinDateRange(DocumentPackageStatus.COMPLETED, new PageRequest(index, 50), DateTime.Today.AddDays(-7), DateTime.Now);
            }

REST API

Finally, I'll go over the REST API. The first step is to retrieve your packages within your date range by making a GET request to

https://sandbox.esignlive.com/api/packages?query=inbox&lastUpdatedStartDate=2016-06-16&lastUpdatedEndDate=2016-06-23&predefined=completed

You will need to define your date range parameter in the URL above. Then, you download your zip file from each of the completed packages in the JSON object returned by OneSpan Sign.

foreach (var package in packages)
{
      Debug.WriteLine("Downloading package: " + package["id"] + "...");
      var downloadZipResponse = myClient.GetAsync(new Uri(apiUrl + "/packages/" + package["id"] + "/documents/zip")).Result;
      ByteArrayContent content = new ByteArrayContent(downloadZipResponse.Content.ReadAsByteArrayAsync().Result);
      File.WriteAllBytes("C:/Users/hhaidary/Desktop/package-documents-"+ package["id"] + ".zip", content.ReadAsByteArrayAsync().Result);
      Thread.Sleep(1000);
}

Running Your Code

You can now go ahead and run your code. If you followed my naming convention for the zip files, below is a screenshot of the console output you can expect once you have run your code.

Capture2

If you have questions regarding this blog or anything else concerning integrating OneSpan Sign into your application, visit the developer community forums: https://developer.OneSpan.com. That's it from me. Thank you for reading! If you found this post helpful, please share it on Facebook, Twitter, or LinkedIn.

Haris Haidary
Junior Technical Evangelist
LinkedIn | Twitter