This .NET 6.0 sample demonstrates how to perform operations using file data with Annotation
(Note) 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 AnnotationOperations
project demonstrates:
-
Creating an note associated with an account record.
-
Setting the the
annotation.documentbody
property with the Base64 encoded string value for the file. -
Retrieving the note and saving the file.
-
Updating the note to refer to a different file.
-
Using the following Dataverse Web API actions to work with notes, especially for large files.
-
Downloading the
annotation.documentbody
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 with notes:
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 performs the following operations:
-
Create an account record to associate the notes to.
-
Create a note associated to the account, setting the
annotation.documentbody
to the Base64 encoded string value of theWordDoc.docx
file. -
Retrieve the note with the
documentbody
,mimetype
, andfilename
properties. -
Convert the
documentbody
tobyte[]
and save the file. -
Update the note, changing the file to
ExcelDoc.xlsx
by setting thedocumentbody
,mimetype
, andfilename
properties. -
Retrieve the note and save the file again.
-
Use
Utility.SetMaxUploadFileSize
to increase the maximum file size for the environment to 131,072,000 Bytes (125 MB). -
Use the static
UploadNote
method to update the note using the large25mb.pdf
file and upload it in chunks.The static
UploadNote
method encapsulates the use of these Web API actions: -
Download the note file using the static
DownloadNote
method.The static
DownloadNote
method encapsulates the use of these Web API actions: -
Download the large
25mb.pdf
file individually usingGET /annotations(<annotationid>)/documentbody/$value
. This returns a Base64 encoded string value that can be converted intobyte[]
and saved.This is done using the WebAPIService/Messages/DownloadAnnotationFileRequest and WebAPIService/Messages/DownloadAnnotationFileResponse classes.
-
Delete the account record. All the notes associated with it are deleted as well.
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
Created an account record to associate notes with.
Created note with attached Word document.
Retrieved note with attached Word document.
Saved the Word document to \bin\Debug\net6.0\DownloadedWordDoc.docx.
Updated note with attached Excel document.
Retrieved note with attached Excel document.
Saved the Excel document to \bin\Debug\net6.0\DownloadedExcelDoc.xlsx.
Updated MaxUploadFileSize to: 131072000
Uploading 25mb.pdf...
Uploaded 25mb.pdf
AnnotationId: 2da8aaf5-3ca0-ed11-aad1-000d3a993550
FileSizeInBytes: 25870370
Saved the PDF document to \bin\Debug\net6.0\Downloaded25mb.pdf.
Saved the PDF document to \bin\Debug\net6.0\DownloadedAgain25mb.pdf.
Deleted the account record.
Current MaxUploadFileSize: 5242880