サンプル: Dataverse SDK for .NET を使用した画像操作

この .NET 6.0 のサンプルでは、Dataverse SDK for .NET を使用して画像列の操作を実行する方法を説明します。

このサンプルでは、Microsoft.PowerPlatform.Dataverse.Client.ServiceClient を使用します。

前提条件

  • Microsoft Visual Studio 2022
  • システム管理者権限またはシステムカスタマイザー権限を持つ Dataverse へのアクセス。

サンプルを実行する方法

  1. PowerApps-サンプル リポジトリをクローンまたはダウンロードします。

  2. Visual Studio 2022 を使用して PowerApps-Samples\dataverse\orgsvc\C#-NETCore\ImageOperations\ImageOperations.sln ファイルを開きます。

  3. appsettings.json ファイルを編集します。 接続文字列 UrlUsername のパラメーターをテスト環境に応じて適切に設定します。

    環境の Url は、Power Platform 管理センターで確認することができます。 次のフォームを持っています: https://<environment-name>.crm.dynamics.com.

  4. ソリューションをビルドし、目的のプロジェクトを実行します。

サンプルを実行すると、既定のブラウザーで、環境ユーザー アカウントを選択してパスワードを入力するように求められます。 サンプルを実行するたびにこの操作を行う必要がないように、appsettings.json ファイルの接続文字列にパスワードパラメータを挿入してください。 例:

{
"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"
  }
}

ヒント

DATAVERSE_APPSETTINGS というユーザー環境変数に、コンピューターの任意の場所に保存されている appsettings.json ファイルのパスを設定することができます。 環境変数が存在し、null でない場合、サンプルはその appsettings ファイルを使用します。 変数を有効にするには、変数を定義した後、必ずログアウトして再度ログインしてください。 環境変数を設定するには、設定 > システム > 概要にアクセスし、高度なシステム設定を選択し、環境変数を選択します。

サンプル出力

出力のサンプル次のようになります:

Creating image column named 'sample_ImageColumn' on the account table ...
Created image column named 'sample_ImageColumn' in the account table.
Create 5 records while CanStoreFullImage is false.
        Created account: 'CanStoreFullImage false 144x144.png'
        Created account: 'CanStoreFullImage false 144x400.png'
        Created account: 'CanStoreFullImage false 400x144.png'
        Created account: 'CanStoreFullImage false 400x500.png'
        Created account: 'CanStoreFullImage false 60x80.png'
Set the CanStoreFullImage property to True
Create 5 records while CanStoreFullImage is true.
        Created account: 'CanStoreFullImage true 144x144.png'
        Created account: 'CanStoreFullImage true 144x400.png'
        Created account: 'CanStoreFullImage true 400x144.png'
        Created account: 'CanStoreFullImage true 400x500.png'
        Created account: 'CanStoreFullImage true 60x80.png'
Retrieving records with thumbnail images:
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage false 144x144.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage false 144x400.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage false 400x144.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage false 400x500.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage false 60x80.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage true 144x144.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage true 144x400.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage true 400x144.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage true 400x500.png_retrieved.png
        Thumbnail-sized file column data saved to DownloadedImages\CanStoreFullImage true 60x80.png_retrieved.png
Attempt to download full-size images for all 10 records. 5 should fail.
        Download failed: No FileAttachment records found for imagedescriptorId: a81a473d-3a8d-ed11-81ad-000d3a993550 for image attribute: sample_imagecolumn of account record with id a51a473d-3a8d-ed11-81ad-000d3a993550
        Download failed: No FileAttachment records found for imagedescriptorId: 2a813f43-3a8d-ed11-81ad-000d3a993550 for image attribute: sample_imagecolumn of account record with id 29813f43-3a8d-ed11-81ad-000d3a993550
        Download failed: No FileAttachment records found for imagedescriptorId: 2e813f43-3a8d-ed11-81ad-000d3a993550 for image attribute: sample_imagecolumn of account record with id 2d813f43-3a8d-ed11-81ad-000d3a993550
        Download failed: No FileAttachment records found for imagedescriptorId: 34813f43-3a8d-ed11-81ad-000d3a993550 for image attribute: sample_imagecolumn of account record with id 32813f43-3a8d-ed11-81ad-000d3a993550
        Download failed: No FileAttachment records found for imagedescriptorId: 3d813f43-3a8d-ed11-81ad-000d3a993550 for image attribute: sample_imagecolumn of account record with id 3c813f43-3a8d-ed11-81ad-000d3a993550
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 144x144.png_downloaded.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 144x400.png_downloaded.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 400x144.png_downloaded.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 400x500.png_downloaded.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 60x80.png_downloaded.png
Deleted the records created for this sample.
Deleting the image column named 'sample_ImageColumn' on the account table ...
Deleted the image column named 'sample_ImageColumn' in the account table.
Sample completed.

実際の動作

このサンプルのコードは、Program.cs ファイルに含まれています。

プロジェクトは Utility クラスを使用して、スキーマ データの作成または取得を含む操作を実行します。 このクラスは Utility.cs ファイルにあります。

このプロジェクトは、次の操作を実行します:

画像列の作成

このサンプルでは、アカウント テーブルのプライマリ画像である新しい画像列を作成する必要があります。 また、終了時にはシステムを元の状態に戻す必要があります。 そのため、次の手順が実行されます。

  1. Utility.GetTablePrimaryImageName メソッドを使用して元のプライマリ画像名を取得します。

  2. まだ存在しない場合は、Utility.CreateImageColumn メソッドを使用して、アカウント テーブルで sample_ImageColumn という名前の新しい画像を作成します。

    注意

    この画像列の CanStoreFullImage 値は false です。

  3. Utility.SetTablePrimaryImageName メソッドを使用して、sample_ImageColumn プライマリ画像を作成します。

画像データでアカウント レコードを作成する

  1. プログラムは、Images フォルダーにあるファイル名に一致する 5 つのファイル名のリストをループします。
  2. 画像ごとに、nameCanStoreFullImage false {fileName} に設定してアカウント レコードを作成し、ファイル byte[]sample_ImageColumn 値として設定します。
  3. 次に、プログラムは Utility.UpdateCanStoreFullImage メソッドを使用して、sample_ImageColumn 定義 CanStoreFullImage の値を true に設定します。
  4. プログラムはファイル名をループし、同じ画像ファイルを使用して 5 つのアカウント レコードを作成し sample_ImageColumn 値を設定します。 今回は、nameCanStoreFullImage true {fileName} に設定します。

次のプログラムでは、CanStoreFullImage プロパティの値が、利用可能なデータがにどのように変化するかを確認できます。

取引先企業レコードの取得

  1. このプログラムは、画像データを含む、前のステップで作成された 10 件のアカウント レコードを取得します。

  2. アカウント レコードごとに、画像データが {recordName}_retrieved.png という名前の付いた DownloadedImages フォルダーにダウンロードされます。

    注意

    これらのレコードはすべてサムネイル サイズの画像です。

アカウント レコードの画像をダウンロードする

プログラムは静的 DownloadFile メソッドを使用し、InitializeFileBlocksDownloadRequestDownloadBlockRequest クラスの使用をカプセル化してファイルをダウンロードします。

注意

CanStoreFullImage が false のときにフルサイズの画像がアップロードされなかったため、10 回の操作のうち 5 回が失敗します。 このレコードは、CanStoreFullImage が成功したときに作成されました。

画像データの削除

  1. プログラムは、sample_ImageColumn 値を null 値に設定し、レコードを更新してアカウント レコードごとに画像データを削除します。
  2. プログラムは、以前と同じ基準を使用してレコードを再度取得することにより、レコードが削除されたことを確認します。 CanStoreFullImage 属性は null 値なので、値は返されません。

クリーンアップ

サンプルを実行する前の状態にするために、次の処理を行います:

  • 取引先企業レコードの削除
  • プライマリ画像を元の値に戻す
  • 画像列の削除

関連情報

ファイル列のデータを使用する
コードを使って画像列定義を操作する
ファイル列のデータを使用する
サンプル: Dataverse Web API を使用したファイル操作

注意

ドキュメントの言語設定についてお聞かせください。 簡単な調査を行います。 (この調査は英語です)

この調査には約 7 分かかります。 個人データは収集されません (プライバシー ステートメント)。