Now, add a recipient(s) to sign the document. Here, also we can define more than one signer.
To Get an envelopeSummary we need to use a JsonConvert. So install it from the NuGet.
Now, use JsonConvert to seraializeObject.
- var result = JsonConvert.SerializeObject(envelopeSummary);
Now, run your application.
In the above image, you can see that we successfully received the envelope summary and we might get a distinct envelope id for each DocuSign sign request.
Validate in DocuSign and Email
Now we shall check the DocuSign portal and email to see whether we have received any document. First, as an admin, we will check in DocuSign portal. In the below image, you can see the sent document with status, DateTime and recipient name.
Now as a user, we will check the mailbox. In the below image, you can see that we have received the mail from DocuSign to sign.
Now, the user is able to sign/review the document by clicking the mailed link. In the next article we will see how to track whether the user signed or viewed in our web application.
Complete Controller
- using DocuSign.eSign.Api;
- using DocuSign.eSign.Client;
- using DocuSign.eSign.Model;
- using Newtonsoft.Json;
- using System.Collections.Generic;
- using System.IO;
- using System.Web;
- using System.Web.Mvc;
- using Document = DocuSign.eSign.Model.Document;
- namespace DocusignDemo.Controllers {
- public class DocusignController: Controller {
- MyCredential credential = new MyCredential();
- private string INTEGRATOR_KEY = "Enter Integrator Key";
- public ActionResult SendDocumentforSign() {
- return View();
- }
- [HttpPost]
- public ActionResult SendDocumentforSign(DocusignDemo.Models.Recipient recipient, HttpPostedFileBase UploadDocument) {
- Models.Recipient recipientModel = new Models.Recipient();
- string directorypath = Server.MapPath("~/App_Data/" + "Files/");
- if (!Directory.Exists(directorypath)) {
- Directory.CreateDirectory(directorypath);
- }
- byte[] data;
- using(Stream inputStream = UploadDocument.InputStream) {
- MemoryStream memoryStream = inputStream as MemoryStream;
- if (memoryStream == null) {
- memoryStream = new MemoryStream();
- inputStream.CopyTo(memoryStream);
- }
- data = memoryStream.ToArray();
- }
- var serverpath = directorypath + recipient.Name.Trim() + ".pdf";
- System.IO.File.WriteAllBytes(serverpath, data);
- docusign(serverpath, recipient.Name, recipient.Email);
- return View();
- }
- public string loginApi(string usr, string pwd) {
-
- ApiClient apiClient = Configuration.Default.ApiClient;
- string authHeader = "{\"Username\":\"" + usr + "\", \"Password\":\"" + pwd + "\", \"IntegratorKey\":\"" + INTEGRATOR_KEY + "\"}";
- Configuration.Default.AddDefaultHeader("X-DocuSign-Authentication", authHeader);
-
- string accountId = null;
-
- AuthenticationApi authApi = new AuthenticationApi();
- LoginInformation loginInfo = authApi.Login();
-
- foreach(DocuSign.eSign.Model.LoginAccount loginAcct in loginInfo.LoginAccounts) {
- if (loginAcct.IsDefault == "true") {
- accountId = loginAcct.AccountId;
- break;
- }
- }
- if (accountId == null) {
- accountId = loginInfo.LoginAccounts[0].AccountId;
- }
- return accountId;
- }
- public void docusign(string path, string recipientName, string recipientEmail) {
- ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
- Configuration.Default.ApiClient = apiClient;
-
- string accountId = loginApi(credential.UserName, credential.Password);
-
- byte[] fileBytes = System.IO.File.ReadAllBytes(path);
- EnvelopeDefinition envDef = new EnvelopeDefinition();
- envDef.EmailSubject = "Please sign this doc";
-
- Document doc = new Document();
- doc.DocumentBase64 = System.Convert.ToBase64String(fileBytes);
- doc.Name = Path.GetFileName(path);
- doc.DocumentId = "1";
- envDef.Documents = new List < Document > ();
- envDef.Documents.Add(doc);
-
- DocuSign.eSign.Model.Signer signer = new DocuSign.eSign.Model.Signer();
- signer.Email = recipientEmail;
- signer.Name = recipientName;
- signer.RecipientId = "1";
- envDef.Recipients = new DocuSign.eSign.Model.Recipients();
- envDef.Recipients.Signers = new List < DocuSign.eSign.Model.Signer > ();
- envDef.Recipients.Signers.Add(signer);
-
- envDef.Status = "sent";
-
- EnvelopesApi envelopesApi = new EnvelopesApi();
- EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef);
-
- var result = JsonConvert.SerializeObject(envelopeSummary);
- }
- }
- public class MyCredential {
- public string UserName {
- get;
- set;
- } = "Enter UserName";
- public string Password {
- get;
- set;
- } = "Enter Password";
- }
- }
View
- @model DocusignDemo.Models.Recipient
- @{
-
- ViewBag.Title = "SendDocumentforSign";
- }
- @using (Html.BeginForm("SendDocumentforSign", "Docusign", FormMethod.Post, new { enctype = "multipart/form-data", id="SendForsign" }))
- {
- @Html.AntiForgeryToken()
- <br />
- <div class="panel panel-primary col-md-6">
- <div class="panel-heading">Send For Sign</div>
- <div class="panel-body" >
- <div class="form-horizontal">
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <div class="form-group">
- @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- @Html.Label("Document", htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- <input id="UploadDocument" type="file" name="UploadDocument" class="form-control" />
- </div>
- </div>
- <div class="form-group">
- @Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
- <div class="col-md-10">
- @Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
- @Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
- </div>
- </div>
- <div class="form-group">
- <div class="col-md-offset-2 col-md-10">
- <input type="submit" value="Send For Sign" class="btn btn-primary" />
- </div>
- </div>
- </div>
- </div>
- </div>
- }
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/jquery.validate.min.js"></script>
- <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
Model
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace DocusignDemo.Models {
- public partial class Recipient {
- public string Name {
- get;
- set;
- }
- public string Email {
- get;
- set;
- }
- public string Description {
- get;
- set;
- }
- }
- }
Refer to the attached project for reference, and I did attach the demonstrated project without package due to the size limit.
Summary
In this article, we have discussed how to send a document for signature from ASP.NET MVC5 web application using DocuSign. I hope it will help you out. Your valuable feedback and comments about this article are always welcome.