forked from docusign/code-examples-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEg017SetTemplateTabValues.cs
218 lines (194 loc) · 9.1 KB
/
Eg017SetTemplateTabValues.cs
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
using System;
using System.Collections.Generic;
using DocuSign.eSign.Api;
using DocuSign.eSign.Model;
using DocuSign.CodeExamples.Models;
using Microsoft.AspNetCore.Mvc;
using System.Text;
using DocuSign.eSign.Client;
namespace DocuSign.CodeExamples.Controllers
{
[Area("eSignature")]
[Route("eg017")]
public class Eg017SetTemplateTabValuesController : EgController
{
// Set up the Ping Url, signer client ID, and the return (callback) URL for embedded signing
private string dsPingUrl;
private readonly string signerClientId = "1000";
private string dsReturnUrl;
public Eg017SetTemplateTabValuesController(DSConfiguration config, IRequestItemsService requestItemsService)
: base(config, requestItemsService)
{
ViewBag.title = "SetTabValues";
dsPingUrl = config.AppUrl + "/";
dsReturnUrl = config.AppUrl + "/dsReturn";
}
public override string EgName => "eg017";
[HttpPost]
public IActionResult Create(string signerEmail, string signerName, string ccEmail, string ccName)
{
// Check the token with minimal buffer time
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");
}
// The envelope will be sent first to the signer; after it is signed,
// a copy is sent to the cc person
//
// Read files from a local directory
// The reads could raise an exception if the file is not available!
var basePath = RequestItemsService.Session.BasePath + "/restapi";
// Step 1: Obtain your OAuth token
var accessToken = RequestItemsService.User.AccessToken; // Represents your {ACCESS_TOKEN}
var accountId = RequestItemsService.Session.AccountId; // Represents your {ACCOUNT_ID}
// Step 2: Construct your API headers
var apiClient = new ApiClient(basePath);
apiClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
// Step 3: Create Tabs and CustomFields
// Set the values for the fields in the template
// List item
List colorPicker = new List
{
Value = "green",
DocumentId = "1",
PageNumber = "1",
TabLabel = "list"
};
// Checkboxes
Checkbox ckAuthorization = new Checkbox
{
TabLabel = "ckAuthorization",
Selected = "true"
};
Checkbox ckAgreement = new Checkbox
{
TabLabel = "ckAgreement",
Selected = "true"
};
RadioGroup radioGroup = new RadioGroup
{
GroupName = "radio1",
// You only need to provide the readio entry for the entry you're selecting
Radios = new List<Radio> { new Radio { Value = "white", Selected = "true" } }
};
Text includedOnTemplate = new Text
{
TabLabel = "text",
Value = "Jabberywocky!"
};
// We can also add a new tab (field) to the ones already in the template
Text addedField = new Text
{
DocumentId = "1",
PageNumber = "1",
XPosition = "280",
YPosition = "172",
Font = "helvetica",
FontSize = "size14",
TabLabel = "added text field",
Height = "23",
Width = "84",
Required = "false",
Bold = "true",
Value = signerName,
Locked = "false",
TabId = "name"
};
// Add the tabs model (including the SignHere tab) to the signer.
// The Tabs object wants arrays of the different field/tab types
// Tabs are set per recipient/signer
Tabs tabs = new Tabs
{
CheckboxTabs = new List<Checkbox> { ckAuthorization, ckAgreement },
RadioGroupTabs = new List<RadioGroup> { radioGroup },
TextTabs = new List<Text> { includedOnTemplate, addedField },
ListTabs = new List<List> { colorPicker }
};
// Create a signer recipient to sign the document, identified by name and email
// We're setting the parameters via the object creation
TemplateRole signer = new TemplateRole
{
Email = signerEmail,
Name = signerName,
RoleName = "signer",
ClientUserId = signerClientId, // Change the signer to be embedded
Tabs = tabs //Set tab values
};
TemplateRole cc = new TemplateRole
{
Email = ccEmail,
Name = ccName,
RoleName = "cc"
};
// Create an envelope custom field to save our application's
// data about the envelope
TextCustomField customField = new TextCustomField
{
Name = "app metadata item",
Required = "false",
Show = "true", // Yes, include in the CoC
Value = "1234567"
};
CustomFields cf = new CustomFields
{
TextCustomFields = new List<TextCustomField> { customField }
};
// Step 4: Create the envelope definition
EnvelopeDefinition envelopeAttributes = new EnvelopeDefinition
{
// Uses the template ID received from example 08
TemplateId = RequestItemsService.TemplateId,
Status = "Sent",
// Add the TemplateRole objects to utilize a pre-defined
// document and signing/routing order on an envelope.
// Template role names need to match what is available on
// the correlated templateID or else an error will occur
TemplateRoles = new List<TemplateRole> { signer, cc },
CustomFields = cf
};
// Step 5: Call the eSignature REST API
var envelopesApi = new EnvelopesApi(apiClient);
EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, envelopeAttributes);
// Step 6: Create the View Request
RequestItemsService.EnvelopeId = results.EnvelopeId;
RecipientViewRequest viewRequest = new RecipientViewRequest();
// Set the URL where you want the recipient to go once they are done signing;
// this should typically be a callback route somewhere in your app.
// The query parameter is included as an example of how
// to save/recover state information during the redirect to
// the DocuSign signing ceremony. It's usually better to use
// the session mechanism of your web framework. Query parameters
// can be changed/spoofed very easily
viewRequest.ReturnUrl = dsReturnUrl + "?state=123";
// How has your app authenticated the user? In addition to your app's authentication,
// you can include authentication steps from DocuSign; e.g., SMS authentication
viewRequest.AuthenticationMethod = "none";
// Recipient information must match the embedded recipient info
// that we used to create the envelope
viewRequest.Email = signerEmail;
viewRequest.UserName = signerName;
viewRequest.ClientUserId = signerClientId;
// DocuSign recommends that you redirect to DocuSign for the
// signing ceremony. There are multiple ways to save state.
// To maintain your application's session, use the PingUrl
// parameter. It causes the DocuSign Signing Ceremony web page
// (not the DocuSign server) to send pings via AJAX to your app
viewRequest.PingFrequency = "600"; // seconds
// NOTE: The pings will only be sent if the pingUrl is an HTTPS address
viewRequest.PingUrl = dsPingUrl; // Optional setting
ViewUrl results1 = envelopesApi.CreateRecipientView(accountId, results.EnvelopeId, viewRequest);
//***********
// Don't use an iframe with embedded signing requests!
//***********
// State can be stored/recovered using the framework's session or a
// query parameter on the return URL (see the makeRecipientViewRequest method)
string redirectUrl = results1.Url;
return Redirect(redirectUrl);
}
}
}