This .NET 6.0 sample demonstrates how to perform operations using file data with ActivityMimeAttachment
(Attachment) table using the Dataverse Web API.
This project one of two projects included in the AttachmentAndAnnotationOperations
solution, which provides shared resources to run either sample. See Web API Attachment and Annotation Operations sample README for an overview and how to run this sample.
Note: This sample uses the common helper code in the WebAPIService class library project.
The ActivityMimeAttachmentOperations
project demonstrates:
-
Adding attachments to an email record directly for single use. These attachment records will be deleted when the email is deleted.
-
Adding attachments to an email template, then associating the attachment to an email. These attachments will not be deleted when the email is deleted.
-
Setting the value of the
activitymimeattachment.body
property directly with a Base64 encoded string value. This is appropriate for small files. -
Using the following Dataverse Web API actions when working with
activitymimeattachment
, especially for large files. -
Downloading the
activitymimeattachment.body
value and saving the file.
This sample uses classes defined for these actions within the WebAPIService project Messages folder so that they can be re-used. These classes inherit from the .NET HttpRequestMessage and HttpResponseMessage classes. These classes have constructors that accept the relevant variables to compose the requests to send, or to deserialize the responses into standard properties.
The code for this sample is in the Program.cs file.
This sample uses three files to represent attachments:
File | Size | Description |
---|---|---|
WordDoc.docx |
18 KB | A small Word document. |
ExcelDoc.xlsx |
75 KB | A small Excel file. |
25mb.pdf |
25,265 KB | A large PDF file, which exceeds the default maximum file size of 5 MB. |
To upload the large file, the maximum file upload size must be increased. The Utility.GetMaxUploadFileSize
function retrieves this value so it can be set back to the original value when the sample completes.
The sample is separated into two regions: Create single-use attachments and Create re-usable attachments.
-
Create an email activity.
-
Using only the small files, create two attachments associated to the email, setting the
activitymimeattachment.body
directly. -
Use
Utility.SetMaxUploadFileSize
to increase the maximum file size for the environment to 131,072,000 Bytes (125 MB). -
Use the static
UploadAttachment
method to create the attachment and upload the large file in chunks.The static
UploadAttachment
method encapsulates the use of these Web API actions: -
Retrieve the
activitymimeattachmentid
andfilename
of the attachment records associated with the email using theactivity_pointer_activity_mime_attachment
collection-valued navigation property. -
Use that data to download each attachment using the static
DownloadAttachment
method.The static
DownloadAttachment
method encapsulates the use of these Web API actions: -
Download the large
25mb.pdf
file individually usingGET /activitymimeattachments(<activitymimeattachmentid>)/body/$value
. This returns a Base64 encoded string value that can be converted intobyte[]
and saved.This is done using the WebAPIService/Messages/DownloadAttachmentFileRequest and WebAPIService/Messages/DownloadAttachmentFileResponse classes.
-
Delete the email activity. All the attachments are deleted with the email.
- Create an email template. This provides values to use when creating an
ActivityMimeAttachment
becauseObjectId
andObjectTypeCode
are required. - Attach all files as attachments using the
UploadAttachment
method described above. - Create a new email activity.
- For each attachment created using the
UploadAttachment
method, create a new attachment associated with the email. Only set theattachmentid
single-valued navigation property. Do not set thebody
,filename
, ormimetype
. - Delete the email activity.
- Retrieve the attachments created using the
UploadAttachment
method to confirm that they still exist. - Delete the email template to clean up. This will delete the attachments associated with it
Use static Utility.SetMaxUploadFileSize
method to set the maximum file upload size to the original setting.
The output of the sample should look something like this:
Current MaxUploadFileSize: 5242880
Start: Create single-use attachments
Created an email activity.
Created two e-mail attachments with small files for the e-mail activity.
Updated MaxUploadFileSize to: 131072000
Adding 25mb.pdf...
Uploaded 25mb.pdf as attachment.
ActivityMimeAttachmentId:6c842bed-34a0-ed11-aad1-000d3a993550
FileSizeInBytes: 25870370
Download attached files:
Downloading filename: WordDoc.docx...
Saved the attachment to \bin\Debug\net6.0\DownloadedWordDoc.docx.
Downloading filename: ExcelDoc.xlsx...
Saved the attachment to \bin\Debug\net6.0\DownloadedExcelDoc.xlsx.
Downloading filename: 25mb.pdf...
Saved the attachment to \bin\Debug\net6.0\Downloaded25mb.pdf.
Saved the attachment to \bin\Debug\net6.0\DownloadedAgain25mb.pdf.
Start: Create re-usable attachments
Created an email template.
Added WordDoc.docx to the email template.
Added ExcelDoc.xlsx to the email template.
Added 25mb.pdf to the email template.
Added all files as attachment to email template.
Created a second email activity.
Attached WordDoc.docx to the second email
Attached ExcelDoc.xlsx to the second email
Attached 25mb.pdf to the second email
Deleted the second email.
Attachment for WordDoc.docx still exists.
Attachment for ExcelDoc.xlsx still exists.
Attachment for 25mb.pdf still exists.
Current MaxUploadFileSize: 5242880