We saw how to convert a PDF file into a JSON file from DocuSign for signature and fill out the other form’s contents in the previous article. Click here to learn more about this.
Introduction:
In this tutorial, we’ll go over how to transmit documents to DocuSign for signature step by step. So let’s begin.
Step 1:
Set the necessary key into your appsetting.json file. If you want to know how to get keys from DocuSign then you can read my previous blog by clicking here.
"DocuSign": { "IntegrationKey": "fc81131e-XXXX-XXXX-XXXX-0dbfc94e3af7", "UserId": "29c410f7-XXXX-XXXX-XXXX-1a4fc813defd", "AuthServer": "account-d.docusign.com", "RSAPrivateKeyFile": "private.key", "JWTLifeTime": "1", "BaseURI": "https://demo.docusign.net", "AccountId": "e2f2e355-XXXX-XXXX-XXXX-764d6994f8c2", }
Step 2:
Put the downloaded JSON file in the Properties folder. Click here if you want to know how to create a JSON file for signature in DocuSign.
Step 3:
Add this code to your controller.
[HttpGet("SendDocumentForSignature/{accessToken}")] public async Task<IActionResult> SendDocumentForSignature(string accessToken) { var apiResponse = await _docuSignService.SendDocumentForSignature(accessToken); return new JsonResult(apiResponse); }
Step 4:
Add Document response class and declare your method in your interface class.
public class SendDocumentResponse { public SendDocumentResponse(string redirectUrl, string envelopeId) { RedirectUrl = redirectUrl; EnvelopeId = envelopeId; } public string RedirectUrl { get; } public string EnvelopeId { get; } }
Task<SendDocumentResponse> SendDocumentForSignature(string accessToken);
Step 5:
Add BuildTemplate(), BuildEnvelope(), GetApiClient(), and BuildRecipientViewRequest() in your method defination class.
public async Task<EnvelopeTemplate> BuildTemplate(string rootDir) { using var reader = new StreamReader(rootDir + "Properties/Sample_Document.json"); return await Task.FromResult(JsonConvert.DeserializeObject<EnvelopeTemplate>(reader.ReadToEnd())); }
public async Task<EnvelopeDefinition> BuildEnvelope(string userFullName, string userEmail) { var role = new TemplateRole { Email = userEmail, Name = userFullName, RoleName = "User", ClientUserId = "1000" }; var env = new EnvelopeDefinition { TemplateRoles = new List<TemplateRole> { role }, Status = "Sent" }; return await Task.FromResult(env); }
private async Task<ApiClient> GetApiClient(string token) { var docuSignConfig = new Configuration(_configuration["DocuSign:BaseURI"] + "/restapi"); docuSignConfig.AddDefaultHeader("Authorization", "Bearer " + token); docuSignConfig.AccessToken = token; var apiClient = new ApiClient(docuSignConfig); apiClient.SetBasePath(docuSignConfig.BasePath); return await Task.FromResult(apiClient); }
private async Task<RecipientViewRequest> BuildRecipientViewRequest(string signerEmail, string signerName, string returnUrl, string pingUrl) { RecipientViewRequest viewRequest = new RecipientViewRequest { ReturnUrl = returnUrl, AuthenticationMethod = "none", Email = signerEmail, UserName = signerName, ClientUserId = "1000" }; if (pingUrl != null) { viewRequest.PingFrequency = "600"; viewRequest.PingUrl = pingUrl; } return await Task.FromResult(viewRequest); }
Step 6:
Finally, add SendDocumentForSignature() method.
public async Task<SendDocumentResponse> SendDocumentForSignature(string accessToken) { SendDocumentResponse sendDocumentResponse = null; try { if (!string.IsNullOrEmpty(accessToken)) { var userFullName = "Shaikh Shahjaha"; string rootDir = _configuration.GetValue<string>(WebHostDefaults.ContentRootKey); EnvelopeTemplate envelopeTemplate = await BuildTemplate(rootDir); EnvelopeDefinition envelope = await BuildEnvelope(userFullName, "example@gmail.com"); var client = await GetApiClient(accessToken); if (client != null) { var templatesApi = new TemplatesApi(client); var listTemplates = templatesApi.ListTemplates(_configuration["DocuSign:AccountId"]); EnvelopeTemplate template = listTemplates?.EnvelopeTemplates?.FirstOrDefault(x => x.Name == "Sample Document"); if (template != null) { envelope.TemplateId = template.TemplateId; } else { TemplateSummary templateSummary = templatesApi.CreateTemplate(_configuration["DocuSign:AccountId"], envelopeTemplate); envelope.TemplateId = templateSummary.TemplateId; } var envelopesApi = new EnvelopesApi(client); EnvelopeSummary envelopeSummary = envelopesApi.CreateEnvelope(_configuration["DocuSign:AccountId"], envelope); ViewUrl recipientView = envelopesApi.CreateRecipientView( _configuration["DocuSign:AccountId"], envelopeSummary.EnvelopeId, await BuildRecipientViewRequest( "example@gmail.com", userFullName, "localhost:44321", "localhost:44321") ); sendDocumentResponse = new SendDocumentResponse(recipientView.Url, envelopeSummary.EnvelopeId); } } } catch (Exception ex) { sendDocumentResponse = null; } return await Task.FromResult(sendDocumentResponse); }
Conclusion:
Following the addition of this code to your project, you will receive a DocuSign redirect URL in the response. Simply copy it and paste it into a new tab. You’ll also require an access token, which we obtained in the previous blog. Click here for instructions on how to obtain an access token from DocuSign.
Related Articles:
Getting Started With The DocuSign
Get Authentication Token From The DocuSign (Part-1)
Get Authentication Token From The DocuSign (Part-2)
How To Create JSON Document File For Signature In DocuSign