/
Program.cs
507 lines (403 loc) · 20.1 KB
/
Program.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Extensions.Configuration;
using Microsoft.PowerPlatform.Dataverse.Client;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
using System.Text;
namespace PowerPlatform.Dataverse.CodeSamples
{
class Program
{
/// <summary>
/// Contains the application's configuration settings.
/// </summary>
IConfiguration Configuration { get; }
/// <summary>
/// Constructor. Loads the application configuration settings from a JSON file.
/// </summary>
Program()
{
// Get the path to the appsettings file. If the environment variable is set,
// use that file path. Otherwise, use the runtime folder's settings file.
string? path = Environment.GetEnvironmentVariable("DATAVERSE_APPSETTINGS");
path ??= "appsettings.json";
// Load the app's configuration settings from the JSON file.
Configuration = new ConfigurationBuilder()
.AddJsonFile(path, optional: false, reloadOnChange: true)
.Build();
}
static void Main()
{
// Files used in this sample
FileInfo wordDoc = new("Files/WordDoc.docx");
FileInfo excelDoc = new("Files/ExcelDoc.xlsx");
FileInfo pdfDoc = new("Files/25mb.pdf"); // A large file over default size
List<FileInfo> smallFiles = new() { wordDoc, excelDoc };
List<FileInfo> allFiles = new() { wordDoc, excelDoc, pdfDoc };
List<(string FileName, Guid ActivityMimeAttachmentId)> reusableAttachments = new();
Program app = new();
// Create a Dataverse service client using the default connection string.
ServiceClient serviceClient =
new(app.Configuration.GetConnectionString("default"))
{
UseWebApi = false
};
// Get current MaxUploadFileSize
int originalMaxUploadFileSize = Utility.GetMaxUploadFileSize(serviceClient);
Console.WriteLine($"Current MaxUploadFileSize: {originalMaxUploadFileSize}");
#region Create single-use attachments
Console.WriteLine("Start: Create single-use attachments");
// Create email activity
Entity email = new("email")
{
Attributes =
{
{"subject", "This is an example email." }
}
};
Guid emailid = serviceClient.Create(email);
email.Id= emailid; //Added so that ToEntityReference will work later.
Console.WriteLine("Created an email activity.");
// Attach the small files to the email directly
smallFiles.ForEach(smallFile =>
{
Entity attachment = new("activitymimeattachment")
{
Attributes =
{
{ "objectid", new EntityReference("email", emailid)},
{ "objecttypecode", "email" },
{ "subject", $"Sample attached {smallFile.Name}" },
{ "body", Convert.ToBase64String(File.ReadAllBytes(smallFile.FullName)) },
{ "filename", smallFile.Name},
{ "mimetype", Utility.GetMimeType(smallFile)}
}
};
serviceClient.Create(attachment);
});
Console.WriteLine("Created two e-mail attachments with small files for the e-mail activity.");
// Set MaxUploadFileSize to the maximum value
Utility.SetMaxUploadFileSize(serviceClient, 131072000);
Console.WriteLine($"Updated MaxUploadFileSize to: {Utility.GetMaxUploadFileSize(serviceClient)}");
// Prepare data for a large attachment
Entity largeAttachment = new("activitymimeattachment")
{
Attributes =
{
{ "objectid", new EntityReference("email", emailid)},
{ "objecttypecode", "email" },
{ "subject", $"Sample attached {pdfDoc.Name}" },
// Do not set the body
{ "filename", pdfDoc.Name},
{ "mimetype", Utility.GetMimeType(pdfDoc)}
}
};
Console.WriteLine($"Adding {pdfDoc.Name}...");
// Creates the activitymimeattachment record with a file.
CommitAttachmentBlocksUploadResponse uploadAttachmentResponse = UploadAttachment(
service: serviceClient,
attachment: largeAttachment,
fileInfo: pdfDoc);
Console.WriteLine($"\tUploaded {pdfDoc.Name} as attachment. " +
$"\n\t\tActivityMimeAttachmentId:{uploadAttachmentResponse.ActivityMimeAttachmentId} \n\t\tFileSizeInBytes: {uploadAttachmentResponse.FileSizeInBytes}");
// Retrieve information about the attachments related to the email.
// See https://learn.microsoft.com/power-apps/developer/data-platform/org-service/entity-operations-retrieve#retrieve-with-related-rows
RelationshipQueryCollection relationshipQueryCollection = new();
// The named relationship between email and activitymimeattachment
Relationship email_attachments = new("email_activity_mime_attachment");
// Details about what to retrieve
QueryExpression relatedAttachments = new("activitymimeattachment")
{
ColumnSet = new ColumnSet("filename")
};
relationshipQueryCollection.Add(
key: email_attachments,
value: relatedAttachments);
RetrieveRequest retrieveRequest = new()
{
ColumnSet = new ColumnSet("activityid"),
RelatedEntitiesQuery = relationshipQueryCollection,
Target = email.ToEntityReference()
};
var retrieveResponse = (RetrieveResponse)serviceClient.Execute(retrieveRequest);
Entity retrievedEmail = retrieveResponse.Entity;
EntityCollection attachments = retrievedEmail.RelatedEntities[email_attachments];
Console.WriteLine("Download attached files:");
foreach (Entity attachment in attachments.Entities)
{
string filename = (string)attachment["filename"];
Console.WriteLine($"\tDownloading filename: {filename}...");
var (bytes, name) = DownloadAttachment(
service: serviceClient,
target: attachment.ToEntityReference());
File.WriteAllBytes($"Downloaded{name}", bytes);
Console.WriteLine($"\tSaved the attachment to \\bin\\Debug\\net6.0\\Downloaded{name}.");
}
// Delete the email activity and the attachments will be deleted as well
serviceClient.Delete("email", emailid);
#endregion Create single-use attachments
#region Create re-usable attachments
Console.WriteLine("\nStart: Create re-usable attachments");
// Create an email template to add the re-usable attachments to.
// ActivityMimeAttachment ObjectId and ObjectTypeCode are SystemRequired.
Entity template = new("template")
{
Attributes = {
{ "body", "<?xml version=\"1.0\" ?>" +
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" +
"<xsl:output method=\"text\" indent=\"no\"/>" +
"<xsl:template match=\"/data\">" +
"<![CDATA[<div>Text for the example account template.</div>]]>" +
"</xsl:template>" +
"</xsl:stylesheet>" },
{ "description", "The description of the Example Account Template" },
{ "ispersonal", false }, //Organization
{ "languagecode", 1033 }, //English
{ "presentationxml", "<template>" +
"<text>" +
"<![CDATA[<div>Text for the example account template.</div>]]>" +
"</text>" +
"</template>" },
{ "safehtml", "<div>Text for the example account template.</div>\n" },
{ "subject", "<?xml version=\"1.0\" ?>" +
"<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" version=\"1.0\">" +
"<xsl:output method=\"text\" indent=\"no\"/>" +
"<xsl:template match=\"/data\">" +
" <![CDATA[Example Account Template Subject]]>" +
"</xsl:template>" +
"</xsl:stylesheet>" },
{ "subjectpresentationxml", "<template>" +
"<text>" +
"<![CDATA[Example Account Template Subject]]>" +
"</text>" +
"</template>" },
{ "subjectsafehtml", "Example Account Template Subject" },
{ "templatetypecode", "account" },
{ "title", "Example Account Template" }
}
};
// Create the template
Guid templateId = serviceClient.Create(template);
Console.WriteLine("Created an email template.");
// Add all files (large and small) as attachments to the template in the same way.
allFiles.ForEach(file =>
{
Entity attachment = new("activitymimeattachment")
{
Attributes =
{
{ "objectid", new EntityReference("template", templateId)},
{ "objecttypecode", "template" },
{ "subject", $"Reusable attachment {file.Name}" },
// Does not include the body.
{ "filename", file.Name},
{ "mimetype", Utility.GetMimeType(file)}
}
};
// Create the attachment with upload
CommitAttachmentBlocksUploadResponse uploadAttachmentResponse = UploadAttachment(
service: serviceClient,
attachment: attachment,
fileInfo: file);
reusableAttachments.Add((file.Name, uploadAttachmentResponse.ActivityMimeAttachmentId));
Console.WriteLine($"\tAdded {file.Name} to the email template.");
});
Console.WriteLine("Added all files as attachment to email template.");
// Create new email to re-use attachments from Template
Entity email2 = new("email")
{
Attributes =
{
{"subject", "This is an example email with re-used attachments." }
}
};
Guid email2Id = serviceClient.Create(email2);
Console.WriteLine("Created a second email activity.");
foreach ((string FileName, Guid ActivityMimeAttachmentId) in reusableAttachments)
{
Entity attachment = new("activitymimeattachment")
{
Attributes =
{
{ "objectid", new EntityReference("email", email2Id)},
{ "objecttypecode", "email" },
{ "subject", $"Sample attached {FileName}" },
{ "attachmentid", new EntityReference("activitymimeattachment", ActivityMimeAttachmentId) } // Only set attachmentid
// Do not set body, filename, or mimetype
}
};
serviceClient.Create(attachment);
Console.WriteLine($"\tAttached {FileName} to the second email");
}
// Delete the second email
serviceClient.Delete("email", email2Id);
Console.WriteLine($"Deleted the second email.");
// Verify the re-used attachments still exist
foreach ((string FileName, Guid ActivityMimeAttachmentId) in reusableAttachments)
{
Entity attachment = serviceClient.Retrieve(
entityName: "activitymimeattachment",
id: ActivityMimeAttachmentId,
columnSet: new ColumnSet("filename"));
if ((string)attachment["filename"] == FileName)
{
Console.WriteLine($"\tAttachment for {FileName} still exists.");
}
}
// Clean up
// Delete the template
serviceClient.Delete("template", templateId); //Will delete re-usable attachments
#endregion Create re-usable attachments
// Return MaxUploadFileSize to the original value
Utility.SetMaxUploadFileSize(serviceClient, originalMaxUploadFileSize);
Console.WriteLine($"Current MaxUploadFileSize: {Utility.GetMaxUploadFileSize(serviceClient)}");
}
/// <summary>
/// Creates an activitymimeattachment with file.
/// </summary>
/// <param name="service">The IOrganizationService instance to use.</param>
/// <param name="attachment">The activitymimeattachment data to create.</param>
/// <param name="fileInfo">A reference to the file to upload.</param>
/// <param name="fileMimeType">The mimetype of the file.</param>
/// <returns>CommitAttachmentBlocksUploadResponse containing ActivityMimeAttachmentId and FileSizeInBytes.</returns>
/// <exception cref="ArgumentException">The attachment parameter must be an activitymimeattachment entity.</exception>
static CommitAttachmentBlocksUploadResponse UploadAttachment(
IOrganizationService service,
Entity attachment,
FileInfo fileInfo,
string fileMimeType = null)
{
if (attachment.LogicalName != "activitymimeattachment")
{
throw new ArgumentException(
"The attachment parameter must be an activitymimeattachment entity.",
nameof(attachment));
}
// body value in activitymimeattachment not needed. Remove if found.
if (attachment.Contains("body"))
{
attachment.Attributes.Remove("body");
}
// Try to get the mimetype if not provided.
if (string.IsNullOrEmpty(fileMimeType))
{
var provider = new FileExtensionContentTypeProvider();
if (!provider.TryGetContentType(fileInfo.Name, out fileMimeType))
{
fileMimeType = "application/octet-stream";
}
}
// Don't overwrite mimetype value if it exists
if (!attachment.Contains("mimetype"))
{
attachment["mimetype"] = fileMimeType;
}
// Initialize the upload
InitializeAttachmentBlocksUploadRequest initializeRequest = new()
{
Target = attachment
};
var initializeResponse =
(InitializeAttachmentBlocksUploadResponse)service.Execute(initializeRequest);
string fileContinuationToken = initializeResponse.FileContinuationToken;
// Capture blockids while uploading
List<string> blockIds = new();
using Stream uploadFileStream = fileInfo.OpenRead();
int blockSize = 4 * 1024 * 1024; // 4 MB
byte[] buffer = new byte[blockSize];
int bytesRead = 0;
long fileSize = fileInfo.Length;
// The number of iterations that will be required:
// int blocksCount = (int)Math.Ceiling(fileSize / (float)blockSize);
int blockNumber = 0;
// While there is unread data from the file
while ((bytesRead = uploadFileStream.Read(buffer, 0, buffer.Length)) > 0)
{
// The file or final block may be smaller than 4MB
if (bytesRead < buffer.Length)
{
Array.Resize(ref buffer, bytesRead);
}
blockNumber++;
string blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()));
blockIds.Add(blockId);
// Prepare the request
UploadBlockRequest uploadBlockRequest = new()
{
BlockData = buffer,
BlockId = blockId,
FileContinuationToken = fileContinuationToken,
};
// Send the request
service.Execute(uploadBlockRequest);
}
// Commit the upload
CommitAttachmentBlocksUploadRequest commitRequest = new()
{
BlockList = blockIds.ToArray(),
FileContinuationToken = fileContinuationToken,
Target = attachment
};
return (CommitAttachmentBlocksUploadResponse)service.Execute(commitRequest);
}
/// <summary>
/// Downloads the file for an activitymimeattachment.
/// </summary>
/// <param name="service">The IOrganizationService instance to use.</param>
/// <param name="target">A reference to the activitymimeattachment containing the file.</param>
/// <returns>Tuple of bytes and fileName</returns>
/// <exception cref="ArgumentException">"The target parameter must refer to an activitymimeattachment record."</exception>
static (byte[] bytes, string fileName) DownloadAttachment(
IOrganizationService service,
EntityReference target)
{
if (target.LogicalName != "activitymimeattachment")
{
throw new ArgumentException(
"The target parameter must refer to an activitymimeattachment record.",
nameof(target));
}
InitializeAttachmentBlocksDownloadRequest initializeRequest = new()
{
Target = target
};
var response =
(InitializeAttachmentBlocksDownloadResponse)service.Execute(initializeRequest);
string fileContinuationToken = response.FileContinuationToken;
int fileSizeInBytes = response.FileSizeInBytes;
string fileName = response.FileName;
List<byte> fileBytes = new(fileSizeInBytes);
long offset = 0;
long blockSizeDownload = 4 * 1024 * 1024; // 4 MB
// File size may be smaller than defined block size
if (fileSizeInBytes < blockSizeDownload)
{
blockSizeDownload = fileSizeInBytes;
}
while (fileSizeInBytes > 0)
{
// Prepare the request
DownloadBlockRequest downLoadBlockRequest = new()
{
BlockLength = blockSizeDownload,
FileContinuationToken = fileContinuationToken,
Offset = offset
};
// Send the request
var downloadBlockResponse =
(DownloadBlockResponse)service.Execute(downLoadBlockRequest);
// Add the block returned to the list
fileBytes.AddRange(downloadBlockResponse.Data);
// Subtract the amount downloaded,
// which may make fileSizeInBytes < 0 and indicate
// no further blocks to download
fileSizeInBytes -= (int)blockSizeDownload;
// Increment the offset to start at the beginning of the next block.
offset += blockSizeDownload;
}
return (fileBytes.ToArray(), fileName);
}
}
}