Editar

Compartilhar via


Sample: File operations with Attachments and Notes using the Dataverse SDK for .NET

This .NET 6.0 sample demonstrates how to perform operations using file data with Attachment (ActivityMimeAttachment) and Note (Annotation) tables using the Dataverse SDK for .NET.

This sample uses the Microsoft.PowerPlatform.Dataverse.Client.ServiceClient Class.

Prerequisites

  • Microsoft Visual Studio 2022
  • Access to Dataverse with system administrator or system customizer privileges.

How to run the sample

  1. Clone or download the PowerApps-Samples repository.

  2. Open the PowerApps-Samples\dataverse\orgsvc\C#-NETCore\AttachmentAndAnnotationOperations\AttachmentAndAnnotationOperations.sln file using Visual Studio 2022.

    This solution contains two projects that include samples:

    • ActivityMimeAttachmentOperations: Demonstrates using Attachments.
    • AnnotationOperations: Demonstrates using Annotations.

    In Solution Explorer, right-click the project you want to run and choose Set as Startup Project.

  3. In either project, edit the appsettings.json file. Set the connection string Url and Username parameters as appropriate for your test environment.

    The environment Url can be found in the Power Platform admin center. It has the form https://<environment-name>.crm.dynamics.com.

  4. Build the solution, and then run the desired project.

When the sample runs, you will be prompted in the default browser to select an environment user account and enter a password. To avoid having to do this every time you run a sample, insert a password parameter into the connection string in the appsettings.json file. For example:

{
"ConnectionStrings": {
    "default": "AuthType=OAuth;Url=https://myorg.crm.dynamics.com;Username=someone@myorg.onmicrosoft.com;Password=mypassword;RedirectUri=http://localhost;AppId=51f81489-12ee-4a9e-aaae-a2591f45987d;LoginPrompt=Auto"
  }
}

Read the following important information about using a connection string in application code.

Important

Microsoft recommends that you use the most secure authentication flow available. The authentication flow described in this article requires a very high degree of trust in the application, and carries risks that are not present in other flows. You should only use this flow when other more secure flows, such as managed identities, aren't viable.

Tip

You can set a user environment variable named DATAVERSE_APPSETTINGS to the file path of the appsettings.json file stored anywhere on your computer. The samples will use that appsettings file if the environment variable exists and is not null. Be sure to log out and back in again after you define the variable for it to take affect. To set an environment variable, go to Settings > System > About, select Advanced system settings, and then choose Environment variables.

Demonstrates

This sample is a solution with two projects. See the respective README files for details on each project.

Utility class

Both projects use a shared Utility class to perform common operations. This class contains three static methods:

GetMimeType

Based on a FileInfo parameter, this function uses Microsoft.AspNetCore.StaticFiles.FileExtensionContentTypeProvider to try to get the mimetype of the file. If this cannot be determined, it returns application/octet-stream

GetMaxUploadFileSize

Using the IOrganizationService service parameter, this function returns the integer maxuploadfilesize value from the organization table.

SetMaxUploadFileSize

Using the IOrganizationService service parameter, this function sets the integer maxuploadfilesize value from the organization table to the value of the integer maxUploadFileSizeInBytes parameter.

See also

Use file data with Attachment and Note records
Sample: Attachment and Annotation file operations using Dataverse Web API