- Create a table in a database
- Configure Entity Framework with database and application
- Get Envelope Information
- Store Envelope Informations
- Get Envelope By Envelope Id
Create a table in a database
First, we will create a table in SQL Server to store the details of Envelope. I have created a table called "Recipient" with the following design
Execute the below query to create a table with the above design.
- CREATE TABLE [dbo].[Recipient](
- [Id] [bigint] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](250) NULL,
- [Email] [nvarchar](300) NULL,
- [EnvelopeID] [nvarchar](max) NULL,
- [Documents] [varbinary](max) NULL,
- [Description] [nvarchar](max) NULL,
- [Status] [varchar](100) NULL,
- [SentOn] [datetime] NULL,
- [UpdatedOn] [datetime] NULL,
- CONSTRAINT [PK_Recipient] PRIMARY KEY CLUSTERED
- (
- [Id] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
- GO
Configure Entity Framework with database and application
Here, I have already discussed how to configure and implement a database-first approach. In the meantime, choose your created table with Entity Framework. Once we do our configuration with SQL table "Recipient" from CSharpCorner database and with our application, we will get the below screen as the succeeding configuration
Get Envelope Information
In our previous article discussion, we discussed how to create a signature request on a respective document for the respective recipient, we successfully sent the signature request and got the envelope Id and much more information but we didn't store it anywhere for future reference. So we need to store envelope information, with the help of the envelope information only we can track or get more information from docuSign. For every signature request, we will get a unique envelope id, so we need to store it into our database. We can't get any information without envelope id. Let's create a new signing request and get envelope information and store it in our database.
As per the previous article, we will get enough information in the below envelopeSummary object. It contains Status,StatusDateTime and EnvelopeId. So, let's store that information into our database.
- EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(accountId, envDef);
-
- var result = JsonConvert.SerializeObject(envelopeSummary);
- Recipient recipient = new Recipient();
- recipient.Description = "envDef.EmailSubject";
- recipient.Email = recipientEmail;
- recipient.Name = recipientName;
- recipient.Status = envelopeSummary.Status;
- recipient.Documents = fileBytes;
- recipient.SentOn = System.Convert.ToDateTime(envelopeSummary.StatusDateTime);
- recipient.EnvelopeID = envelopeSummary.EnvelopeId;
- CSharpCornerEntities cSharpCornerEntities = new CSharpCornerEntities();
- cSharpCornerEntities.Recipients.Add(recipient);
Now, run your application.
In the above image, you can see that we got all information and the below image represents our data has been stored in a database. Now, we can proceed with many things with the help of this envelope id and status.
Get Envelope By Envelope Id
Now, let's try to pull the envelope information by passing the evelopeID.
- public ActionResult getEnvelopeInformation() {
- ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
- Configuration.Default.ApiClient = apiClient;
-
- string envelopeId = "3e66307b-e9ba-42a0-9f9e-b3a7016fee2a";
- MyCredential myCredential = new MyCredential();
-
- string accountId = loginApi(myCredential.UserName, myCredential.Password);
-
-
-
-
- EnvelopesApi envelopesApi = new EnvelopesApi();
- Envelope envInfo = envelopesApi.GetEnvelope(accountId, envelopeId);
- return View();
- }
Now, run your application. In the below image, you can see that we got much information by passing envelope id, and we will use all properties in the future one by one.
Complete controller
- using DocuSign.eSign.Api;
- using DocuSign.eSign.Client;
- using DocuSign.eSign.Model;
- using DocusignDemo.Models;
- 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);
- Recipient recipient = new Recipient();
- recipient.Description = "envDef.EmailSubject";
- recipient.Email = recipientEmail;
- recipient.Name = recipientName;
- recipient.Status = envelopeSummary.Status;
- recipient.Documents = fileBytes;
- recipient.SentOn = System.Convert.ToDateTime(envelopeSummary.StatusDateTime);
- recipient.EnvelopeID = envelopeSummary.EnvelopeId;
- CSharpCornerEntities cSharpCornerEntities = new CSharpCornerEntities();
- cSharpCornerEntities.Recipients.Add(recipient);
- cSharpCornerEntities.SaveChanges();
- }
- public ActionResult getEnvelopeInformation() {
- ApiClient apiClient = new ApiClient("https://demo.docusign.net/restapi");
- Configuration.Default.ApiClient = apiClient;
-
- string envelopeId = "Enter Stored Envelope Id";
- MyCredential myCredential = new MyCredential();
-
- string accountId = loginApi(myCredential.UserName, myCredential.Password);
-
-
-
-
- EnvelopesApi envelopesApi = new EnvelopesApi();
- Envelope envInfo = envelopesApi.GetEnvelope(accountId, envelopeId);
- return View();
- }
- }
- public class MyCredential {
- public string UserName {
- get;
- set;
- } = "Enter your username";
- public string Password {
- get;
- set;
- } = "Enter your Password";
- }
- }
Refer to the attached project. I did attach the demonstrated project without a package due to the size limit. Wait for my next docuSign article to get more knowledge on this.
Summary
In this article, we discussed how to maintain the DocuSign envelope information in our database using Entity Framework and how to get envelope information by envelope Id. I hope it will help you out. Your valuable feedback and comments about this article are always welcome.