|
| 1 | +// <copyright file="CreateAndEmbedForm.cs" company="DocuSign"> |
| 2 | +// Copyright (c) DocuSign. All rights reserved. |
| 3 | +// </copyright> |
| 4 | + |
| 5 | +namespace DocuSign.CodeExamples.WebForms.Controllers |
| 6 | +{ |
| 7 | + using System; |
| 8 | + using System.Collections.Generic; |
| 9 | + using System.Linq; |
| 10 | + using DocuSign.CodeExamples.Common; |
| 11 | + using DocuSign.CodeExamples.Controllers; |
| 12 | + using DocuSign.CodeExamples.Models; |
| 13 | + using DocuSign.eSign.Client; |
| 14 | + using DocuSign.eSign.Model; |
| 15 | + using DocuSign.WebForms.Examples; |
| 16 | + using DocuSign.WebForms.Model; |
| 17 | + using Microsoft.AspNetCore.Mvc; |
| 18 | + using Microsoft.Extensions.Configuration; |
| 19 | + |
| 20 | + [Area("WebForms")] |
| 21 | + [Route("web001")] |
| 22 | + public class CreateAndEmbedForm : EgController |
| 23 | + { |
| 24 | + private IConfiguration configuration; |
| 25 | + |
| 26 | + public CreateAndEmbedForm( |
| 27 | + DsConfiguration config, |
| 28 | + IConfiguration configuration, |
| 29 | + LauncherTexts launcherTexts, |
| 30 | + IRequestItemsService requestItemsService) |
| 31 | + : base(config, launcherTexts, requestItemsService) |
| 32 | + { |
| 33 | + this.configuration = configuration; |
| 34 | + this.CodeExampleText = this.GetExampleText(this.EgName, ExamplesApiType.Connect); |
| 35 | + this.ViewBag.title = this.CodeExampleText.ExampleName; |
| 36 | + } |
| 37 | + |
| 38 | + public override string EgName => "web001"; |
| 39 | + |
| 40 | + [SetViewBag] |
| 41 | + [HttpPost] |
| 42 | + [ValidateAntiForgeryToken] |
| 43 | + public ActionResult CheckTheTemplates() |
| 44 | + { |
| 45 | + string basePath = this.RequestItemsService.Session.BasePath + "/restapi"; |
| 46 | + string accessToken = this.RequestItemsService.User.AccessToken; |
| 47 | + string accountId = this.RequestItemsService.Session.AccountId; |
| 48 | + |
| 49 | + var docuSignClient = new DocuSignClient(basePath); |
| 50 | + docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken); |
| 51 | + |
| 52 | + List<EnvelopeTemplate> templates = CreateAndEmbedFormService.CheckIfTemplateExists(docuSignClient, accountId); |
| 53 | + if (templates == null || templates.Count == 0) |
| 54 | + { |
| 55 | + var template = CreateAndEmbedFormService.CreateTemplate( |
| 56 | + docuSignClient, |
| 57 | + accountId, |
| 58 | + this.Config.DocumentTemplatePdf); |
| 59 | + |
| 60 | + this.RequestItemsService.WebFormsTemplateId = template.TemplateId; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + this.RequestItemsService.WebFormsTemplateId = templates.First().TemplateId; |
| 65 | + } |
| 66 | + |
| 67 | + CreateAndEmbedFormService.AddTemplateIdToForm( |
| 68 | + this.Config.WebFormConfig, |
| 69 | + this.RequestItemsService.WebFormsTemplateId); |
| 70 | + |
| 71 | + return this.View("embedForm"); |
| 72 | + } |
| 73 | + |
| 74 | + [MustAuthenticate] |
| 75 | + [SetViewBag] |
| 76 | + [Route("EmbedForm")] |
| 77 | + [HttpPost] |
| 78 | + [ValidateAntiForgeryToken] |
| 79 | + public ActionResult EmbedForm(string formId) |
| 80 | + { |
| 81 | + string basePath = this.RequestItemsService.Session.WebFormsBasePath; |
| 82 | + string accessToken = this.RequestItemsService.User.AccessToken; |
| 83 | + string accountId = this.RequestItemsService.Session.AccountId; |
| 84 | + |
| 85 | + var docuSignClient = new DocuSign.WebForms.Client.DocuSignClient(basePath); |
| 86 | + docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken); |
| 87 | + |
| 88 | + WebFormInstance form = CreateAndEmbedFormService.CreateInstance( |
| 89 | + docuSignClient, |
| 90 | + Guid.Parse(accountId), |
| 91 | + formId); |
| 92 | + |
| 93 | + this.ViewBag.InstanceToken = form.InstanceToken; |
| 94 | + this.ViewBag.Url = form.FormUrl; |
| 95 | + this.ViewBag.IntegrationKey = this.configuration["DocuSign:ClientId"]; |
| 96 | + |
| 97 | + return this.View("Embed"); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments