GET the contents of an Envelope, download a signed document

An 'Envelope' is the Docusign container that contains everything about a communication. 

After I send an Envelope, I want to be able to programmatically check to see if it has been signed. If it has, I want to save that signed document to a SQL table and then kick off some business logic. 

This code shows how to download a signed document (pdf)

We're going to be 'deserializing' JSON into a class, so that we can get the status of the document and extract a pdf. 

So, you'll need these classes in your code first

Imports System.Runtime.Serialization
<DataContract>
Public Class DocumentPages
    'This is the result of this call
    '{
    '    "resultSetSize": "1",
    '    "startPosition": "1",
    '    "endPosition": "1",
    '    "totalSetSize": "1",
    '    "pages": [{
    '        "pageId": "ce03e4a8-6bec-4a1f-aa19-1b019537c6ea",
    '        "sequence": "1",
    '        "height": "792",
    '        "width": "612",
    '        "imageBytes": "iVBORw0KGgoAAAANSUhEUgAAAx8AAAQKCAIAAACuelBUAAAAAXNSR0IArs4c6QA...snSqDC1GS4YAAAAASUVORK5CYII=",
    '        "mimeType": "png"
    '    }]
    '}
 
    <DataMember>
    Public Property resultSetSize As Int16
    <DataMember>
    Public Property startPosition As Int16
    <DataMember>
    Public Property endPosition As Int16
    <DataMember>
    Public Property pages As pages()
 
 
End Class
 
<DataContract>
Public Class pages
 
    <DataMember>
    Public Property pageId As String
    <DataMember>
    Public Property sequence As Int16
    <DataMember>
    Public Property mimeType As String
    <DataMember>
    Public Property imageBytes As String
 
 
End Class

This is just a skeleton, you can add all the fields... but I didn't want to get too wordy for a demo script. 

See this article for a discussion about the AccountID, UserName, Pass, and Key

Public Sub GetPageImages(strAccountID As String, strUserName As String, strPassword As String, strIngegratorKey As String, strEnvelopeID As String)
    Try
 
        'needed to upgrade my project to .NET 4.5.1 to have this available
        ServicePointManager.Expect100Continue = True
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Dim oRequest As System.Net.HttpWebRequest
        Dim oResponse As System.Net.WebResponse = Nothing
        Dim oPostStream As System.IO.Stream = Nothing
 
        'form the url
        Dim strUrl As String = String.Format("https://demo.docusign.net/restapi/v2.1/accounts/{0}/envelopes/{1}/documents/1/pages", strAccountID, strEnvelopeID)
 
 
        ' Create the web request
        oRequest = DirectCast(System.Net.WebRequest.Create(strUrl), System.Net.HttpWebRequest)
        oRequest.Method = "GET"
        oRequest.Accept = "application/json"
        oRequest.ContentType = "application/json"
 
        ' Add authentication to request
        oRequest.Headers.Add("X-DocuSign-Authentication", String.Format("<DocuSignCredentials><Username>{0}</Username><Password>{1}</Password><IntegratorKey>{2}</IntegratorKey></DocuSignCredentials>", strUserName, strPassword, strIngegratorKey))
 
        ' Get the response
        oResponse = DirectCast(oRequest.GetResponse(), System.Net.WebResponse)
 
        'convert the response to a Stream
        Dim oStream As System.IO.Stream = oResponse.GetResponseStream()
        'convert the Stream to a StreamReader
        Dim oStreamReader As New System.IO.StreamReader(oStream)
        'read the response into a String
        Dim strResponse As String = oStreamReader.ReadToEnd
 
        ' Deserializing Json object from string
        Dim oSerializer As New DataContractJsonSerializer(GetType(DocumentPages))
 
        ' Read JSON data into memory stream and cast to top-level object
        Dim ms = New MemoryStream(Encoding.Unicode.GetBytes(strResponse))
 
        'deserialize into class objects
        'after this line executes, examine the oDeserializedJSON object and you'll see the effect
        Dim oDeserializedJSON As DocumentPages = DirectCast(oSerializer.ReadObject(ms), DocumentPages)
 
 
        Dim oBytes As Byte()
        'in production, save this to a sql table
        'note that it is possible for a pdf to have more than one page, and "pages" is an array of pages, but our data only ever has one page,
        'so we only save the first page to SQL
        oBytes = FPCommon.ImageHandler.StringToByte(oDeserializedJSON.pages(0).imageBytes)
 
        Stop
 
        'This is the result of this call
        '{
        '    "resultSetSize": "1",
        '    "startPosition": "1",
        '    "endPosition": "1",
        '    "totalSetSize": "1",
        '    "pages": [{
        '        "pageId": "ce03e4a8-6bec-4a1f-aa19-1b019537c6ea",
        '        "sequence": "1",
        '        "height": "792",
        '        "width": "612",
        '        "imageBytes": "iVBORw0KGgoAAAANSUhEUgAAAx8AAAQKCAIAAACuelBUAAAAAXNSR0IArs4c6QA...snSqDC1GS4YAAAAASUVORK5CYII=",
        '        "mimeType": "png"
        '    }]
        '}
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
 
End Sub

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences