-
Notifications
You must be signed in to change notification settings - Fork 59
/
EmbeddedSending.cs
267 lines (240 loc) · 12.2 KB
/
EmbeddedSending.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
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
// <copyright file="EmbeddedSending.cs" company="DocuSign">
// Copyright (c) DocuSign. All rights reserved.
// </copyright>
namespace ESignature.Examples
{
using System;
using System.Collections.Generic;
using System.Text;
using DocuSign.eSign.Api;
using DocuSign.eSign.Client;
using DocuSign.eSign.Model;
public static class EmbeddedSending
{
/// <summary>
/// Create an envelope and then create an embedded sending view for that envelope
/// </summary>
/// <param name="signerEmail">Email address for the signer</param>
/// <param name="signerName">Full name of the signer</param>
/// <param name="ccEmail">Email address for the cc recipient</param>
/// <param name="ccName">Name of the cc recipient</param>
/// <param name="accessToken">Access Token for API call (OAuth)</param>
/// <param name="basePath">BasePath for API calls (URI)</param>
/// <param name="accountId">The DocuSign Account ID (GUID or short version) for which the APIs call would be made</param>
/// <param name="docPdf">String of bytes representing the document (pdf)</param>
/// <param name="docDocx">String of bytes representing the Word document (docx)</param>
/// <param name="startingView">The sending view to show initially (either "tagging" or "recipient")</param>
/// <param name="returnUrl">URL user will be redirected to after they sign</param>
/// <returns>URL to embed in your application</returns>
public static string SendEnvelopeUsingEmbeddedSending(string signerEmail, string signerName, string ccEmail, string ccName, string docDocx, string docPdf, string accessToken, string basePath, string accountId, string startingView, string returnUrl)
{
//ds-snippet-start:eSign11Step2
var docuSignClient = new DocuSignClient(basePath);
docuSignClient.Configuration.DefaultHeader.Add("Authorization", "Bearer " + accessToken);
EnvelopesApi envelopesApi = new EnvelopesApi(docuSignClient);
// Step 2. Make the envelope with "created" (draft) status
EnvelopeDefinition env = MakeEnvelope(signerEmail, signerName, ccEmail, ccName, docDocx, docPdf, "created");
EnvelopeSummary results = envelopesApi.CreateEnvelope(accountId, env);
string envelopeId = results.EnvelopeId;
//ds-snippet-end:eSign11Step2
// Step 3. create the sender view
// Call the CreateSenderView API
// Exceptions will be caught by the calling function
//ds-snippet-start:eSign11Step3
EnvelopeViewRequest viewRequest = PrepareViewRequest(startingView, returnUrl);
ViewUrl result1 = envelopesApi.CreateSenderView(accountId, envelopeId, viewRequest);
// Switch to Recipient and Documents view if requested by the user
string redirectUrl = result1.Url;
Console.WriteLine("startingView: " + startingView);
if ("recipient".Equals(startingView))
{
redirectUrl = redirectUrl.Replace("send=1", "send=0");
}
//ds-snippet-end:eSign11Step3
return redirectUrl;
}
//ds-snippet-start:eSign11Step3
private static EnvelopeViewRequest PrepareViewRequest(string startingView, string returnUrl)
{
EnvelopeViewSettings viewSettings = new EnvelopeViewSettings
{
StartingScreen = startingView,
SendButtonAction = "send",
ShowBackButton = "false",
BackButtonAction = "previousPage",
ShowHeaderActions = "false",
ShowDiscardAction = "false",
LockToken = string.Empty,
RecipientSettings = new EnvelopeViewRecipientSettings
{
ShowEditRecipients = "false",
ShowContactsList = "false",
},
DocumentSettings = new EnvelopeViewDocumentSettings
{
ShowEditDocuments = "false",
ShowEditDocumentVisibility = "false",
ShowEditPages = "false",
},
TaggerSettings = new EnvelopeViewTaggerSettings
{
PaletteSections = "default",
PaletteDefault = "custom",
},
TemplateSettings = new EnvelopeViewTemplateSettings
{
ShowMatchingTemplatesPrompt = "true",
},
};
EnvelopeViewRequest viewRequest = new EnvelopeViewRequest
{
ReturnUrl = returnUrl,
ViewAccess = "envelope",
Settings = viewSettings,
};
return viewRequest;
}
//ds-snippet-end:eSign11Step3
//ds-snippet-start:eSign11Step2
private static EnvelopeDefinition MakeEnvelope(string signerEmail, string signerName, string ccEmail, string ccName, string docDocx, string docPdf, string envStatus)
{
// Data for this method
// signerEmail
// signerName
// ccEmail
// ccName
// Config.docDocx
// Config.docPdf
// RequestItemsService.Status -- the envelope status ('created' or 'sent')
// document 1 (html) has tag **signature_1**
// document 2 (docx) has tag /sn1/
// document 3 (pdf) has tag /sn1/
//
// The envelope has two recipients.
// recipient 1 - signer
// recipient 2 - cc
// 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!
string doc2DocxBytes = Convert.ToBase64String(System.IO.File.ReadAllBytes(docDocx));
string doc3PdfBytes = Convert.ToBase64String(System.IO.File.ReadAllBytes(docPdf));
// create the envelope definition
EnvelopeDefinition env = new EnvelopeDefinition();
env.EmailSubject = "Please sign this document set";
// Create document objects, one per document
Document doc1 = new Document();
string b64 = Convert.ToBase64String(Document1(signerEmail, signerName, ccEmail, ccName));
doc1.DocumentBase64 = b64;
doc1.Name = "Order acknowledgement"; // can be different from actual file name
doc1.FileExtension = "html"; // Source data format. Signed docs are always pdf.
doc1.DocumentId = "1"; // a label used to reference the doc
Document doc2 = new Document
{
DocumentBase64 = doc2DocxBytes,
Name = "Battle Plan", // can be different from actual file name
FileExtension = "docx",
DocumentId = "2",
};
Document doc3 = new Document
{
DocumentBase64 = doc3PdfBytes,
Name = "Lorem Ipsum", // can be different from actual file name
FileExtension = "pdf",
DocumentId = "3",
};
// The order in the docs array determines the order in the envelope
env.Documents = new List<Document> { doc1, doc2, doc3 };
// 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
{
Email = signerEmail,
Name = signerName,
RecipientId = "1",
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
{
Email = ccEmail,
Name = ccName,
RecipientId = "2",
RoutingOrder = "2",
};
// Create signHere fields (also known as tabs) on the documents,
// We're using anchor (autoPlace) positioning
//
// The DocuSign platform searches throughout your envelope's
// documents for matching anchor strings. So the
// signHere2 tab will be used in both document 2 and 3 since they
// use the same anchor string for their "signer 1" tabs.
SignHere signHere1 = new SignHere
{
AnchorString = "**signature_1**",
AnchorUnits = "pixels",
AnchorYOffset = "10",
AnchorXOffset = "20",
};
SignHere signHere2 = new SignHere
{
AnchorString = "/sn1/",
AnchorUnits = "pixels",
AnchorYOffset = "10",
AnchorXOffset = "20",
};
// Tabs are set per recipient / signer
Tabs signer1Tabs = new Tabs
{
SignHereTabs = new List<SignHere> { signHere1, signHere2 },
};
signer1.Tabs = signer1Tabs;
// Add the recipients to the envelope object
Recipients recipients = new Recipients
{
Signers = new List<Signer> { signer1 },
CarbonCopies = new List<CarbonCopy> { cc1 },
};
env.Recipients = recipients;
// Request that the envelope be sent by setting |status| to "sent".
// To request that the envelope be created as a draft, set to "created"
env.Status = envStatus;
return env;
}
private static byte[] Document1(string signerEmail, string signerName, string ccEmail, string ccName)
{
// Data for this method
// signerEmail
// signerName
// ccEmail
// ccName
return Encoding.UTF8.GetBytes(
" <!DOCTYPE html>\n" +
" <html>\n" +
" <head>\n" +
" <meta charset=\"UTF-8\">\n" +
" </head>\n" +
" <body style=\"font-family:sans-serif;margin-left:2em;\">\n" +
" <h1 style=\"font-family: 'Trebuchet MS', Helvetica, sans-serif;\n" +
" color: darkblue;margin-bottom: 0;\">World Wide Corp</h1>\n" +
" <h2 style=\"font-family: 'Trebuchet MS', Helvetica, sans-serif;\n" +
" margin-top: 0px;margin-bottom: 3.5em;font-size: 1em;\n" +
" color: darkblue;\">Order Processing Division</h2>\n" +
" <h4>Ordered by " + signerName + "</h4>\n" +
" <p style=\"margin-top:0em; margin-bottom:0em;\">Email: " + signerEmail + "</p>\n" +
" <p style=\"margin-top:0em; margin-bottom:0em;\">Copy to: " + ccName + ", " + ccEmail + "</p>\n" +
" <p style=\"margin-top:3em;\">\n" +
" Candy bonbon pastry jujubes lollipop wafer biscuit biscuit. Topping brownie sesame snaps sweet roll pie. Croissant danish biscuit soufflé caramels jujubes jelly. Dragée danish caramels lemon drops dragée. Gummi bears cupcake biscuit tiramisu sugar plum pastry. Dragée gummies applicake pudding liquorice. Donut jujubes oat cake jelly-o. Dessert bear claw chocolate cake gummies lollipop sugar plum ice cream gummies cheesecake.\n" +
" </p>\n" +
" <!-- Note the anchor tag for the signature field is in white. -->\n" +
" <h3 style=\"margin-top:3em;\">Agreed: <span style=\"color:white;\">**signature_1**/</span></h3>\n" +
" </body>\n" +
" </html>");
}
//ds-snippet-end:eSign11Step2
}
}