Skip to content

Instantly share code, notes, and snippets.

@cburnette
Last active August 8, 2016 18:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cburnette/ab470109738a981ad1165a0cef59d14c to your computer and use it in GitHub Desktop.
Save cburnette/ab470109738a981ad1165a0cef59d14c to your computer and use it in GitHub Desktop.
using DocuSign.eSign.Api;
using DocuSign.eSign.Model;
using DocuSign.eSign.Client;
using System.Collections.Generic;
using System.IO;
using System;
namespace BasicMvcSample.Helpers
{
public class DocuSignHelper
{
public MemoryStream GetStream(string envid)
{
string accountid = "";
EnvelopesApi envelopesApi = new EnvelopesApi();
return (MemoryStream)envelopesApi.GetDocument(accountid, envid, "1");
}
internal static EnvelopeDefinition CreateDraftEnvelopeDefinition(Stream fs)
{
using (var memoryStream = new MemoryStream())
{
fs.CopyTo(memoryStream);
var fileBytes = memoryStream.ToArray();
EnvelopeDefinition envDef = new EnvelopeDefinition();
envDef.EmailSubject = "Please Sign my C# SDK Envelope";
Document doc = new Document();
doc.DocumentBase64 = System.Convert.ToBase64String(fileBytes);
doc.Name = "TestFile.pdf";
doc.DocumentId = "1";
envDef.Documents = new List<Document>();
envDef.Documents.Add(doc);
// Add a recipient to sign the documeent
Signer signer = new Signer();
signer.Email = "cburnette+docusign-test@box.com"; // use name is same as my email
signer.Name = "Chad Burnette";
signer.RecipientId = "1";
signer.ClientUserId = "1234";
// Create a SignHere tab somewhere on the document for the signer to sign
signer.Tabs = new Tabs();
signer.Tabs.SignHereTabs = new List<SignHere>();
SignHere signHere = new SignHere();
signHere.DocumentId = "1";
signHere.PageNumber = "1";
signHere.RecipientId = "1";
signHere.XPosition = "100";
signHere.YPosition = "100";
signer.Tabs.SignHereTabs.Add(signHere);
envDef.Recipients = new Recipients();
envDef.Recipients.Signers = new List<Signer>();
envDef.Recipients.Signers.Add(signer);
envDef.Status = "sent";
return envDef;
}
}
public ViewUrl SignBox(Stream fs, string docid)
{
string username = "";
string password = "";
string integratorKey = "";
string accountid = "";
ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
Configuration.Default.ApiClient = apiClient;
string authHeader = "{\"Username\":\"" + username + "\", \"Password\":\"" + password + "\", \"IntegratorKey\":\"" + integratorKey + "\"}";
Configuration.Default.AddDefaultHeader("X-DocuSign-Authentication", authHeader);
AuthenticationApi authApi = new AuthenticationApi();
var envDef = CreateDraftEnvelopeDefinition(fs);
EnvelopesApi envelopesApi = new EnvelopesApi();
EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountid, envDef);
var rec = new RecipientViewRequest();
rec.Email = "cburnette+docusign-test@box.com"; // use name is same as my email
rec.UserName = "Chad Burnette";
rec.ClientUserId = "1234";
rec.ReturnUrl = "http://localhost:1655/App/DocusignCallBack?envid=" + envelopeSummary.EnvelopeId + "&docid=" + docid;
rec.AuthenticationMethod = "email";
var viewurl = envelopesApi.CreateRecipientView(accountid, envelopeSummary.EnvelopeId, rec);
return viewurl;
}
//these are controller action, move it to a controller
public RedirectResult DocuSign()
{
var docid = Request.Form["docid"];
var userboxid = new Users().GetUserId("user1");
var stream = BoxHelper.UserClient(userboxid).FilesManager.DownloadStreamAsync(docid);
var doc = new DocuSignHelper();
var url = doc.SignBox(stream.Result, docid);
return Redirect(url.Url);
}
public ActionResult DocusignCallBack()
{
var q = Request.QueryString["envid"];
var docid = Request.QueryString["docid"];
var userboxid = new Users().GetUserId("user1");
var fs = new DocuSignHelper().GetStream(q);
// var fileRequest = new Box.V2.Models.BoxFileRequest { Name = "signed doc.pdf", Parent = new BoxRequestEntity { Id = "0" } };
var result = BoxHelper.UserClient(userboxid).FilesManager.UploadNewVersionAsync("newfile.pdf", docid, fs);
return View();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment