SQL Server
SQL Server
DocuSign Download Envelope Document (PDF)
See more DocuSign Examples
Retrieves the specified document from the envelope. The response body of this method is the PDF file as a byte stream. You can get the file name and document ID from the response's Content-Disposition header.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Implements the following HTTP request:
-- GET /restapi/v2.1/accounts/{accountId}/envelopes/{envelopeId}/documents/1
-- Adds the "Authorization: Bearer eyJ0eXAi.....UE8Kl_V8KroQ" header.
DECLARE @jsonToken int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jsonToken OUT
-- Load a previously obtained OAuth2 access token.
EXEC sp_OAMethod @jsonToken, 'LoadFile', @success OUT, 'qa_data/tokens/docusign.json'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @jsonToken, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
RETURN
END
EXEC sp_OAMethod @jsonToken, 'StringOf', @sTmp0 OUT, 'access_token'
EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
-- Use your account ID and a valid envelopeId here:
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'accountId', '7f3f65ed-5e87-418d-94c1-92499ddc8252'
EXEC sp_OAMethod @http, 'SetUrlVar', @success OUT, 'envelopeId', '90d7e40a-b4bd-4ccd-bf38-c80e37954a13'
DECLARE @url nvarchar(4000)
SELECT @url = 'https://demo.docusign.net/restapi/v2.1/accounts/{$accountId}/envelopes/{$envelopeId}/documents/1'
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
EXEC sp_OAMethod @http, 'DownloadBd', @success OUT, @url, @bd
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @bd
RETURN
END
DECLARE @respStatusCode int
EXEC sp_OAGetProperty @http, 'LastStatus', @respStatusCode OUT
PRINT 'Response Status Code = ' + @respStatusCode
IF @respStatusCode <> 200
BEGIN
PRINT 'Response Header:'
EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT
PRINT @sTmp0
-- The response body contains an error message.
EXEC sp_OAMethod @bd, 'GetString', @sTmp0 OUT, 'utf-8'
PRINT @sTmp0
PRINT 'Failed.'
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @bd
RETURN
END
-- The response indicated success.
-- Get the filename from the Content-Disposition header and save to a file.
DECLARE @mime int
EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT
EXEC sp_OAGetProperty @http, 'LastResponseHeader', @sTmp0 OUT
EXEC sp_OAMethod @mime, 'LoadMime', @success OUT, @sTmp0
DECLARE @filename nvarchar(4000)
EXEC sp_OAMethod @mime, 'GetHeaderFieldAttribute', @filename OUT, 'Content-Disposition', 'filename'
PRINT 'filename = ' + @filename
DECLARE @sbPath int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbPath OUT
EXEC sp_OAMethod @sbPath, 'Append', @success OUT, 'C:/aaworkarea/'
EXEC sp_OAMethod @sbPath, 'Append', @success OUT, @filename
EXEC sp_OAMethod @sbPath, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @bd, 'WriteFile', @success OUT, @sTmp0
IF @success = 0
BEGIN
PRINT 'Failed to save to output file.'
END
ELSE
BEGIN
EXEC sp_OAMethod @sbPath, 'GetAsString', @sTmp0 OUT
PRINT 'Wrote ' + @sTmp0
END
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @jsonToken
EXEC @hr = sp_OADestroy @bd
EXEC @hr = sp_OADestroy @mime
EXEC @hr = sp_OADestroy @sbPath
END
GO