Skip to content

Commit 5f11318

Browse files
committed
added second web forms example
1 parent 1832adc commit 5f11318

File tree

8 files changed

+707
-200
lines changed

8 files changed

+707
-200
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// <copyright file="CreateRemoteInstance.cs" company="DocuSign">
2+
// Copyright (c) DocuSign. All rights reserved.
3+
// </copyright>
4+
5+
namespace DocuSign.CodeExamples.WebForms.Controllers
6+
{
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using DocuSign.CodeExamples.Common;
10+
using DocuSign.CodeExamples.Controllers;
11+
using DocuSign.CodeExamples.Models;
12+
using DocuSign.eSign.Client;
13+
using DocuSign.eSign.Model;
14+
using DocuSign.WebForms.Examples;
15+
using DocuSign.WebForms.Model;
16+
using Microsoft.AspNetCore.Mvc;
17+
18+
[Area("WebForms")]
19+
[Route("web002")]
20+
public class CreateRemoteInstance : EgController
21+
{
22+
public const string TemplateName = "Web Form Example Template";
23+
24+
public CreateRemoteInstance(
25+
DsConfiguration config,
26+
LauncherTexts launcherTexts,
27+
IRequestItemsService requestItemsService)
28+
: base(config, launcherTexts, requestItemsService)
29+
{
30+
this.CodeExampleText = this.GetExampleText(this.EgName, ExamplesApiType.WebForms);
31+
this.ViewBag.title = this.CodeExampleText.ExampleName;
32+
}
33+
34+
public override string EgName => "web002";
35+
36+
[SetViewBag]
37+
[HttpPost]
38+
[ValidateAntiForgeryToken]
39+
public ActionResult CheckAndCreateTemplates()
40+
{
41+
string basePath = this.RequestItemsService.Session.BasePath + "/restapi";
42+
string accessToken = this.RequestItemsService.User.AccessToken;
43+
string accountId = this.RequestItemsService.Session.AccountId;
44+
45+
try
46+
{
47+
List<EnvelopeTemplate> templates = CreateRemoteInstanceService.GetTemplatesByName(
48+
basePath,
49+
accessToken,
50+
accountId,
51+
TemplateName);
52+
53+
string templateId;
54+
55+
if (templates == null || templates.Count == 0)
56+
{
57+
TemplateSummary template = CreateRemoteInstanceService.CreateTemplate(
58+
basePath,
59+
accessToken,
60+
accountId,
61+
this.Config.DocumentTemplatePdf,
62+
TemplateName);
63+
64+
templateId = template.TemplateId;
65+
}
66+
else
67+
{
68+
templateId = templates.First().TemplateId;
69+
}
70+
71+
this.RequestItemsService.WebFormsTemplateId = templateId;
72+
73+
CreateRemoteInstanceService.AddTemplateIdToForm(
74+
this.Config.WebFormConfig,
75+
templateId);
76+
77+
this.ViewBag.CodeExampleText = this.CodeExampleText;
78+
this.ViewBag.Description = this.CodeExampleText.AdditionalPages
79+
.First(x => x.Name == "create_web_form").ResultsPageText;
80+
81+
return this.View("createWebForm");
82+
}
83+
catch (ApiException apiException)
84+
{
85+
this.ViewBag.errorCode = apiException.ErrorCode;
86+
this.ViewBag.errorMessage = apiException.Message;
87+
this.ViewBag.SupportingTexts = this.LauncherTexts.ManifestStructure.SupportingTexts;
88+
89+
return this.View("Error");
90+
}
91+
}
92+
93+
[MustAuthenticate]
94+
[SetViewBag]
95+
[Route("CreateInstance")]
96+
[HttpPost]
97+
[ValidateAntiForgeryToken]
98+
public ActionResult CreateInstance()
99+
{
100+
string basePath = this.RequestItemsService.Session.WebFormsBasePath;
101+
string accessToken = this.RequestItemsService.User.AccessToken;
102+
string accountId = this.RequestItemsService.Session.AccountId;
103+
104+
try
105+
{
106+
WebFormSummaryList forms = CreateRemoteInstanceService.GetFormsByName(
107+
basePath,
108+
accessToken,
109+
accountId,
110+
TemplateName);
111+
112+
if (forms.Items == null || forms.Items.Count == 0)
113+
{
114+
this.ViewBag.message = this.CodeExampleText.CustomErrorTexts[0].ErrorMessage;
115+
}
116+
else
117+
{
118+
var formItem = forms.Items.First(x => x.FormProperties.Name == TemplateName);
119+
WebFormInstance form = CreateRemoteInstanceService.CreateInstance(
120+
basePath,
121+
accessToken,
122+
accountId,
123+
formItem.Id,
124+
this.Config.SignerEmail,
125+
this.Config.SignerName);
126+
127+
this.ViewBag.message = string.Format(this.CodeExampleText.ResultsPageText, form.Envelopes[0].Id, form.Id);
128+
}
129+
130+
this.ViewBag.h1 = this.CodeExampleText.ExampleName;
131+
132+
return this.View("example_done");
133+
}
134+
catch (ApiException apiException)
135+
{
136+
this.ViewBag.errorCode = apiException.ErrorCode;
137+
this.ViewBag.errorMessage = apiException.Message;
138+
this.ViewBag.SupportingTexts = this.LauncherTexts.ManifestStructure.SupportingTexts;
139+
140+
return this.View("Error");
141+
}
142+
}
143+
}
144+
}
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
// <copyright file="CreateRemoteInstanceService.cs" company="DocuSign">
2+
// Copyright (c) DocuSign. All rights reserved.
3+
// </copyright>
4+
5+
namespace DocuSign.WebForms.Examples
6+
{
7+
using System;
8+
using System.Collections.Generic;
9+
using System.IO;
10+
using DocuSign.eSign.Api;
11+
using DocuSign.eSign.Client;
12+
using DocuSign.eSign.Model;
13+
using DocuSign.WebForms.Api;
14+
using DocuSign.WebForms.Model;
15+
16+
public static class CreateRemoteInstanceService
17+
{
18+
public static WebFormSummaryList GetFormsByName(string basePath, string accessToken, string accountId, string templateName)
19+
{
20+
var client = PrepareWebFormsClient(accessToken, basePath);
21+
FormManagementApi formManagementApi = new FormManagementApi(client);
22+
23+
FormManagementApi.ListFormsOptions listFormsOptions = new FormManagementApi.ListFormsOptions
24+
{
25+
search = templateName,
26+
};
27+
return formManagementApi.ListForms(accountId, listFormsOptions);
28+
}
29+
30+
public static void AddTemplateIdToForm(string fileLocation, string templateId)
31+
{
32+
string targetString = "template-id";
33+
string fileContent = File.ReadAllText(fileLocation);
34+
string modifiedContent = fileContent.Replace(targetString, templateId);
35+
36+
File.WriteAllText(fileLocation, modifiedContent);
37+
}
38+
39+
public static WebFormInstance CreateInstance(
40+
string basePath,
41+
string accessToken,
42+
string accountId,
43+
string formId,
44+
string signerEmail,
45+
string signerName)
46+
{
47+
var client = PrepareWebFormsClient(accessToken, basePath);
48+
49+
var formValues = new WebFormValues
50+
{
51+
{ "PhoneNumber", "555-555-5555" },
52+
{ "Yes", new[] { "Yes" } },
53+
{ "Company", "Tally" },
54+
{ "JobTitle", "Programmer Writer" },
55+
};
56+
57+
var requestBody = new CreateInstanceRequestBody()
58+
{
59+
SendOption = SendOption.Now,
60+
FormValues = formValues,
61+
Recipients = new List<CreateInstanceRequestBodyRecipients>
62+
{
63+
new CreateInstanceRequestBodyRecipients
64+
{
65+
Email = signerEmail,
66+
Name = signerName,
67+
RoleName = "signer",
68+
},
69+
},
70+
};
71+
72+
FormInstanceManagementApi formManagementApi = new FormInstanceManagementApi(client);
73+
return formManagementApi.CreateInstance(accountId, formId, requestBody);
74+
}
75+
76+
public static List<EnvelopeTemplate> GetTemplatesByName(
77+
string basePath,
78+
string accessToken,
79+
string accountId,
80+
string templateName)
81+
{
82+
var client = PrepareESignClient(accessToken, basePath);
83+
var templatesApi = new TemplatesApi(client);
84+
85+
var options = new TemplatesApi.ListTemplatesOptions
86+
{
87+
searchText = templateName,
88+
};
89+
90+
EnvelopeTemplateResults templates = templatesApi.ListTemplates(accountId, options);
91+
92+
return templates.EnvelopeTemplates;
93+
}
94+
95+
public static TemplateSummary CreateTemplate(
96+
string basePath,
97+
string accessToken,
98+
string accountId,
99+
string documentPdf,
100+
string templateName)
101+
{
102+
var client = PrepareESignClient(accessToken, basePath);
103+
TemplatesApi templatesApi = new TemplatesApi(client);
104+
105+
EnvelopeTemplate envelopeTemplate = PrepareEnvelopeTemplate(templateName, documentPdf);
106+
107+
return templatesApi.CreateTemplate(accountId, envelopeTemplate);
108+
}
109+
110+
public static EnvelopeTemplate PrepareEnvelopeTemplate(string resultsTemplateName, string documentPdf)
111+
{
112+
Document document = new Document()
113+
{
114+
DocumentBase64 = Convert.ToBase64String(File.ReadAllBytes(documentPdf)),
115+
Name = "World_Wide_Web_Form",
116+
FileExtension = "pdf",
117+
DocumentId = "1",
118+
};
119+
120+
Signer signer = new Signer()
121+
{
122+
RoleName = "signer",
123+
RecipientId = "1",
124+
RoutingOrder = "1",
125+
};
126+
127+
Tabs signerTabs = new Tabs()
128+
{
129+
CheckboxTabs = new List<Checkbox>
130+
{
131+
new Checkbox()
132+
{
133+
DocumentId = "1",
134+
TabLabel = "Yes",
135+
AnchorString = "/SMS/",
136+
AnchorUnits = "pixels",
137+
AnchorXOffset = "0",
138+
AnchorYOffset = "0",
139+
},
140+
},
141+
SignHereTabs = new List<SignHere>
142+
{
143+
new SignHere()
144+
{
145+
DocumentId = "1",
146+
TabLabel = "Signature",
147+
AnchorString = "/SignHere/",
148+
AnchorUnits = "pixels",
149+
AnchorXOffset = "20",
150+
AnchorYOffset = "10",
151+
},
152+
},
153+
TextTabs = new List<Text>
154+
{
155+
new Text()
156+
{
157+
DocumentId = "1",
158+
TabLabel = "FullName",
159+
AnchorString = "/FullName/",
160+
AnchorUnits = "pixels",
161+
AnchorXOffset = "0",
162+
AnchorYOffset = "0",
163+
},
164+
new Text()
165+
{
166+
DocumentId = "1",
167+
TabLabel = "PhoneNumber",
168+
AnchorString = "/PhoneNumber/",
169+
AnchorUnits = "pixels",
170+
AnchorXOffset = "0",
171+
AnchorYOffset = "0",
172+
},
173+
new Text()
174+
{
175+
DocumentId = "1",
176+
TabLabel = "Company",
177+
AnchorString = "/Company/",
178+
AnchorUnits = "pixels",
179+
AnchorXOffset = "0",
180+
AnchorYOffset = "0",
181+
},
182+
new Text()
183+
{
184+
DocumentId = "1",
185+
TabLabel = "JobTitle",
186+
AnchorString = "/Title/",
187+
AnchorUnits = "pixels",
188+
AnchorXOffset = "0",
189+
AnchorYOffset = "0",
190+
},
191+
},
192+
DateSignedTabs = new List<DateSigned>
193+
{
194+
new DateSigned()
195+
{
196+
DocumentId = "1",
197+
TabLabel = "DateSigned",
198+
AnchorString = "/Date/",
199+
AnchorUnits = "pixels",
200+
AnchorXOffset = "0",
201+
AnchorYOffset = "0",
202+
},
203+
},
204+
};
205+
206+
signer.Tabs = signerTabs;
207+
208+
Recipients recipients = new Recipients();
209+
recipients.Signers = new List<Signer> { signer };
210+
211+
return new EnvelopeTemplate()
212+
{
213+
Description = "Example template created via the eSignature API",
214+
Name = resultsTemplateName,
215+
Shared = "false",
216+
Documents = new List<Document> { document },
217+
EmailSubject = "Please sign this document",
218+
Recipients = recipients,
219+
Status = "created",
220+
};
221+
}
222+
223+
private static Client.DocuSignClient PrepareWebFormsClient(string accessToken, string basePath)
224+
{
225+
var client = new Client.DocuSignClient(basePath);
226+
client.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
227+
return client;
228+
}
229+
230+
private static DocuSignClient PrepareESignClient(string accessToken, string basePath)
231+
{
232+
var client = new DocuSignClient(basePath);
233+
client.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
234+
return client;
235+
}
236+
}
237+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<h4>@Html.Raw(ViewBag.CodeExampleText.ExampleName)</h4>
2+
3+
<p>
4+
@ViewBag.Description
5+
</p>
6+
7+
<form class="eg" asp-action="CreateInstance" method="post" data-busy="form">
8+
<button type="submit" class="btn btn-primary">@Html.Raw(ViewBag.SupportingTexts.ContinueButton)</button>
9+
</form>
10+

0 commit comments

Comments
 (0)