forked from docusign/code-examples-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEg008CreateTemplateController.cs
More file actions
279 lines (241 loc) · 11.5 KB
/
Eg008CreateTemplateController.cs
File metadata and controls
279 lines (241 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using System.Collections.Generic;
using DocuSign.eSign.Api;
using DocuSign.eSign.Client;
using DocuSign.eSign.Model;
using DocuSign.CodeExamples.Models;
using Microsoft.AspNetCore.Mvc;
namespace DocuSign.CodeExamples.Controllers
{
[Area("eSignature")]
[Route("eg008")]
public class Eg008CreateTemplateController : EgController
{
public Eg008CreateTemplateController(DSConfiguration config, IRequestItemsService requestItemsService)
: base(config, requestItemsService)
{
ViewBag.title = "Create a template";
}
public override string EgName => "eg008";
// Returns a tuple. See https://stackoverflow.com/a/36436255/64904
// ***DS.snippet.0.start
static string templateName = "Example Signer and CC template";
private (bool createdNewTemplate, string templateId, string resultsTemplateName) DoWork(
string accessToken, string basePath, string accountId)
{
// Data for this method
// accessToken
// basePath
// accountId
// Step 1. List templates to see if ours exists already
var apiClient = new ApiClient(basePath);
apiClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
var templatesApi = new TemplatesApi(apiClient);
TemplatesApi.ListTemplatesOptions options = new TemplatesApi.ListTemplatesOptions();
options.searchText = "Example Signer and CC template";
EnvelopeTemplateResults results = templatesApi.ListTemplates(accountId, options);
string templateId;
string resultsTemplateName;
bool createdNewTemplate;
// Step 2. Process results
if (int.Parse(results.ResultSetSize) > 0)
{
// Found the template! Record its id
templateId = results.EnvelopeTemplates[0].TemplateId;
resultsTemplateName = results.EnvelopeTemplates[0].Name;
createdNewTemplate = false;
}
else
{
// No template! Create one!
EnvelopeTemplate templateReqObject = MakeTemplate(templateName);
TemplateSummary template = templatesApi.CreateTemplate(accountId, templateReqObject);
// Retrieve the new template Name / TemplateId
EnvelopeTemplateResults templateResults = templatesApi.ListTemplates(accountId, options);
templateId = templateResults.EnvelopeTemplates[0].TemplateId;
resultsTemplateName = templateResults.EnvelopeTemplates[0].Name;
createdNewTemplate = true;
}
return (createdNewTemplate: createdNewTemplate,
templateId: templateId, resultsTemplateName: resultsTemplateName);
}
private EnvelopeTemplate MakeTemplate(string resultsTemplateName)
{
// Data for this method
// resultsTemplateName
// document 1 (pdf) has tag /sn1/
//
// The template has two recipient roles.
// recipient 1 - signer
// recipient 2 - cc
// The template will be sent first to the signer.
// After it is signed, a copy is sent to the cc person.
// read file from a local directory
// The reads could raise an exception if the file is not available!
// add the documents
Document doc = new Document();
string docB64 = Convert.ToBase64String(System.IO.File.ReadAllBytes("World_Wide_Corp_fields.pdf"));
doc.DocumentBase64 = docB64;
doc.Name = "Lorem Ipsum"; // can be different from actual file name
doc.FileExtension = "pdf";
doc.DocumentId = "1";
// create a signer recipient to sign the document, identified by name and email
// We're setting the parameters via the object creation
Signer signer1 = new Signer();
signer1.RoleName = "signer";
signer1.RecipientId = "1";
signer1.RoutingOrder = "1";
// routingOrder (lower means earlier) determines the order of deliveries
// to the recipients. Parallel routing order is supported by using the
// same integer as the order for two or more recipients.
// create a cc recipient to receive a copy of the documents, identified by name and email
// We're setting the parameters via setters
CarbonCopy cc1 = new CarbonCopy();
cc1.RoleName = "cc";
cc1.RoutingOrder = "2";
cc1.RecipientId = "2";
// Create fields using absolute positioning:
SignHere signHere = new SignHere();
signHere.DocumentId = "1";
signHere.PageNumber = "1";
signHere.XPosition = "191";
signHere.YPosition = "148";
Checkbox check1 = new Checkbox();
check1.DocumentId = "1";
check1.PageNumber = "1";
check1.XPosition = "75";
check1.YPosition = "417";
check1.TabLabel = "ckAuthorization";
Checkbox check2 = new Checkbox();
check2.DocumentId = "1";
check2.PageNumber = "1";
check2.XPosition = "75";
check2.YPosition = "447";
check2.TabLabel = "ckAuthentication";
Checkbox check3 = new Checkbox();
check3.DocumentId = "1";
check3.PageNumber = "1";
check3.XPosition = "75";
check3.YPosition = "478";
check3.TabLabel = "ckAgreement";
Checkbox check4 = new Checkbox();
check4.DocumentId = "1";
check4.PageNumber = "1";
check4.XPosition = "75";
check4.YPosition = "508";
check4.TabLabel = "ckAcknowledgement";
List list1 = new List();
list1.DocumentId = "1";
list1.PageNumber = "1";
list1.XPosition = "142";
list1.YPosition = "291";
list1.Font = "helvetica";
list1.FontSize = "size14";
list1.TabLabel = "list";
list1.Required = "false";
list1.ListItems = new List<ListItem>
{
new ListItem {Text = "Red", Value = "Red"},
new ListItem {Text = "Orange", Value = "Orange"},
new ListItem {Text = "Yellow", Value = "Yellow"},
new ListItem {Text = "Green", Value = "Green"},
new ListItem {Text = "Blue", Value = "Blue"},
new ListItem {Text = "Indigo", Value = "Indigo"},
new ListItem {Text = "Violet", Value = "Violet"},
};
// The SDK can't create a number tab at this time. Bug DCM-2732
// Until it is fixed, use a text tab instead.
// , number = docusign.Number.constructFromObject({
// documentId: "1", pageNumber: "1", xPosition: "163", yPosition: "260",
// font: "helvetica", fontSize: "size14", tabLabel: "numbersOnly",
// height: "23", width: "84", required: "false"})
Text textInsteadOfNumber = new Text();
textInsteadOfNumber.DocumentId = "1";
textInsteadOfNumber.PageNumber = "1";
textInsteadOfNumber.XPosition = "153";
textInsteadOfNumber.YPosition = "260";
textInsteadOfNumber.Font = "helvetica";
textInsteadOfNumber.FontSize = "size14";
textInsteadOfNumber.TabLabel = "numbersOnly";
textInsteadOfNumber.Height = "23";
textInsteadOfNumber.Width = "84";
textInsteadOfNumber.Required = "false";
RadioGroup radioGroup = new RadioGroup();
radioGroup.DocumentId = "1";
radioGroup.GroupName = "radio1";
radioGroup.Radios = new List<Radio>
{
new Radio {PageNumber="1", Value="white", XPosition="142", YPosition="384", Required = "false"},
new Radio {PageNumber="1", Value="red", XPosition="74", YPosition="384", Required = "false"},
new Radio {PageNumber="1", Value="blue", XPosition="220", YPosition="384", Required = "false"}
};
Text text = new Text();
text.DocumentId = "1";
text.PageNumber = "1";
text.XPosition = "153";
text.YPosition = "230";
text.Font = "helvetica";
text.FontSize = "size14";
text.TabLabel = "text";
text.Height = "23";
text.Width = "84";
text.Required = "false";
// Tabs are set per recipient / signer
Tabs signer1Tabs = new Tabs();
signer1Tabs.CheckboxTabs = new List<Checkbox>
{
check1, check2, check3, check4
};
signer1Tabs.ListTabs = new List<List> { list1 };
// numberTabs: [number],
signer1Tabs.RadioGroupTabs = new List<RadioGroup> { radioGroup };
signer1Tabs.SignHereTabs = new List<SignHere> { signHere};
signer1Tabs.TextTabs = new List<Text> { text, textInsteadOfNumber };
signer1.Tabs = signer1Tabs;
// Add the recipients to the env object
Recipients recipients = new Recipients();
recipients.Signers = new List<Signer> { signer1 };
recipients.CarbonCopies = new List<CarbonCopy> { cc1 };
// create the overall template definition
EnvelopeTemplate template = new EnvelopeTemplate();
// The order in the docs array determines the order in the env
template.Description = "Example template created via the API";
template.Name = resultsTemplateName;
template.Documents = new List<Document> { doc};
template.EmailSubject = "Please sign this document";
template.Recipients = recipients;
template.Status = "created";
return template;
}
// ***DS.snippet.0.end
[HttpPost]
public IActionResult Create()
{
// Data for this method
var accessToken = RequestItemsService.User.AccessToken;
var basePath = RequestItemsService.Session.BasePath + "/restapi";
var accountId = RequestItemsService.Session.AccountId;
bool tokenOk = CheckToken(3);
if (!tokenOk)
{
// We could store the parameters of the requested operation
// so it could be restarted automatically.
// But since it should be rare to have a token issue here,
// we'll make the user re-enter the form data after
// authentication.
RequestItemsService.EgName = EgName;
return Redirect("/ds/mustAuthenticate");
}
// Call DoWork. It Returns a tuple. See https://stackoverflow.com/a/36436255/64904
(bool createdNewTemplate, string templateId, string resultsTemplateName) = DoWork(
accessToken, basePath, accountId);
// Save the templateId
RequestItemsService.TemplateId = templateId;
string msg = createdNewTemplate ?
"The template has been created!" :
"The template already exists in your account.";
ViewBag.message = msg + "<br/>Template name: " + resultsTemplateName + ", ID " + templateId + ".";
return View("example_done");
}
}
}