forked from docusign/code-examples-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEg031BulkSendEnvelopes.cs
219 lines (187 loc) · 9.09 KB
/
Eg031BulkSendEnvelopes.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
using DocuSign.eSign.Api;
using DocuSign.eSign.Client;
using DocuSign.eSign.Model;
using eg_03_csharp_auth_code_grant_core.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using System;
using System.Collections.Generic;
using System.Linq;
namespace eg_03_csharp_auth_code_grant_core.Controllers
{
[Route("Eg031")]
public class Eg031BulkSendEnvelopesController : EgController
{
public Eg031BulkSendEnvelopesController(DSConfiguration config, IRequestItemsService requestItemsService) :
base(config, requestItemsService)
{
}
public override string EgName => "Eg031";
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult SetProfile(RecipientModel signer1, RecipientModel carbonCopy1, RecipientModel signer2, RecipientModel carbonCopy2)
{
// Check the 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");
}
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 config = new Configuration(new ApiClient(basePath));
config.AddDefaultHeader("Authorization", "Bearer " + accessToken);
var bulkEnvelopesApi = new BulkEnvelopesApi(config);
// Construct request body
var sendingList = this.MakeBulkSendList(signer1, carbonCopy1, signer2, carbonCopy2);
try
{
// Step 3. Submit a bulk list
var createBulkListResult = bulkEnvelopesApi.CreateBulkSendList(accountId, sendingList);
// Step 4. Create an envelope
var envelopeDefinition = new EnvelopeDefinition
{
Documents = new List<Document>
{
new Document
{
DocumentBase64 = Convert.ToBase64String(System.IO.File.ReadAllBytes(Config.docDocx)),
Name = "Battle Plan",
FileExtension = "docx",
DocumentId = "1"
}
},
EnvelopeIdStamping = "true",
EmailSubject = "Please sign this document sent from the C# SDK",
Status = "created"
};
EnvelopesApi envelopesApi = new EnvelopesApi(config);
var envelopeResults = envelopesApi.CreateEnvelope(accountId, envelopeDefinition);
// Step 5. Attach your bulk list ID to the envelope
// Add an envelope custom field set to the value of your listId (EnvelopeCustomFields::create)
// This Custom Field is used for tracking your Bulk Send via the Envelopes::Get method
var fields = new CustomFields
{
ListCustomFields = new List<ListCustomField> { },
TextCustomFields = new List<TextCustomField>
{
new TextCustomField
{
Name = "mailingListId",
Required = "false",
Show = "false",
Value = createBulkListResult.ListId //Adding the BULK_LIST_ID as an Envelope Custom Field
}
}
};
envelopesApi.CreateCustomFields(accountId, envelopeResults.EnvelopeId, fields);
// Step 6. Add placeholder recipients.
// These will be replaced by the details provided in the Bulk List uploaded during Step 3
// Note: The name / email format used is:
// Name: Multi Bulk Recipients::{rolename}
// Email: MultiBulkRecipients-{rolename}@docusign.com
var recipients = new Recipients
{
Signers = new List<Signer>
{
new Signer
{
Name = "Multi Bulk Recipient::signer",
Email = "[email protected]",
RoleName = "signer",
RoutingOrder = "1",
Status = "sent",
DeliveryMethod = "Email",
RecipientId = "1",
RecipientType = "signer"
},
new Signer
{
Name = "Multi Bulk Recipient::cc",
Email = "[email protected]",
RoleName = "cc",
RoutingOrder = "1",
Status = "sent",
DeliveryMethod = "Email",
RecipientId = "2",
RecipientType = "cc"
}
}
};
envelopesApi.CreateRecipient(accountId, envelopeResults.EnvelopeId, recipients);
//Step 7. Initiate bulk send
var bulkRequestResult = bulkEnvelopesApi.CreateBulkSendRequest(accountId, createBulkListResult.ListId,
new BulkSendRequest
{
EnvelopeOrTemplateId =
envelopeResults.EnvelopeId
});
//Wait until all requests sent. For 2000 recipients, it can take about 1h.
System.Threading.Thread.Sleep(5000);
Console.WriteLine("Bulk Batch ID: "+ bulkRequestResult.BatchId);
// Step 8. Confirm successful batch send
var status = bulkEnvelopesApi.Get(accountId, bulkRequestResult.BatchId);
ViewBag.h1 = "Bulk send envelope was successfully performed!";
ViewBag.message = $@"Bulk request was sent to {status.Sent} user lists.";
}
catch (Exception ex)
{
ViewBag.h1 = "Bulk send envelope failed.";
ViewBag.message = $@"Bulk request failed to send. Reason: {ex}.";
}
return View("example_done");
}
private BulkSendingList MakeBulkSendList(RecipientModel signer1, RecipientModel carbonCopy1, RecipientModel signer2, RecipientModel carbonCopy2)
{
return new BulkSendingList
{
BulkCopies = new List<BulkSendingCopy> {
new BulkSendingCopy
{
Recipients = new List<BulkSendingCopyRecipient>
{
new BulkSendingCopyRecipient
{
Name = signer1.Name,
Email = signer1.Email,
RoleName = "signer"
},
new BulkSendingCopyRecipient
{
Name = carbonCopy1.Name,
Email = carbonCopy1.Email,
RoleName = "cc"
}
}
},
new BulkSendingCopy
{
Recipients = new List<BulkSendingCopyRecipient>
{
new BulkSendingCopyRecipient
{
Name = signer2.Name,
Email = signer2.Email,
RoleName = "signer"
},
new BulkSendingCopyRecipient
{
Name = carbonCopy2.Name,
Email = carbonCopy2.Email,
RoleName = "cc"
}
}
}
},
Name = "sample.csv"
};
}
}
}