サンプル: Dataverse Web API を使用した画像操作

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

このサンプルでは、WebAPIService クラス ライブラリ (C#) の共通ヘルパー コードを使用します。

前提条件

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

サンプルを実行する方法

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

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

  3. appsettings.json ファイルを編集して、以下のプロパティ値を設定します:

    Property 方法
    Url ご使用の環境の Url。 プレースホルダー https://yourorg.api.crm.dynamics.com の値を環境の値に置き換えます。 詳細については、開発者向けリソースを表示する を参照してください。
    UserPrincipalName プレースホルダー you@yourorg.onmicrosoft.com の値を環境へのアクセスに使用する UPN の値に置き換えます。
    Password プレースホルダー yourPassword の値を使用するパスワードに置き換えます。
  4. appsettings.json ファイルを保存します

  5. F5 を押してサンプルを実行します。

サンプル出力

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

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 using 3 different methods:
Download full-sized files with actions: 5 should fail
        Download failed: No FileAttachment records found for imagedescriptorId: e40bdcf1-598d-ed11-81ad-000d3a9933c9 for image attribute: sample_imagecolumn of account record with id e30bdcf1-598d-ed11-81ad-000d3a9933c9
        Download failed: No FileAttachment records found for imagedescriptorId: 2258d4f7-598d-ed11-81ad-000d3a9933c9 for image attribute: sample_imagecolumn of account record with id 2158d4f7-598d-ed11-81ad-000d3a9933c9
        Download failed: No FileAttachment records found for imagedescriptorId: 2658d4f7-598d-ed11-81ad-000d3a9933c9 for image attribute: sample_imagecolumn of account record with id 2558d4f7-598d-ed11-81ad-000d3a9933c9
        Download failed: No FileAttachment records found for imagedescriptorId: 2a58d4f7-598d-ed11-81ad-000d3a9933c9 for image attribute: sample_imagecolumn of account record with id 2958d4f7-598d-ed11-81ad-000d3a9933c9
        Download failed: No FileAttachment records found for imagedescriptorId: 2e58d4f7-598d-ed11-81ad-000d3a9933c9 for image attribute: sample_imagecolumn of account record with id 2d58d4f7-598d-ed11-81ad-000d3a9933c9
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 144x144.png_downloaded_with_actions.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 144x400.png_downloaded_with_actions.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 400x144.png_downloaded_with_actions.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 400x500.png_downloaded_with_actions.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 60x80.png_downloaded_with_actions.png
Download full-sized files with chunks: 5 should fail
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 144x144.png_downloaded_with_chunks_full-sized.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 144x400.png_downloaded_with_chunks_full-sized.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 400x144.png_downloaded_with_chunks_full-sized.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 400x500.png_downloaded_with_chunks_full-sized.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 60x80.png_downloaded_with_chunks_full-sized.png
Download full-sized files in single requests: 5 should fail
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        No full-sized image data returned because record was created while CanStoreFullImage was false.
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 144x144.png_downloaded_with_stream_full-sized.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 144x400.png_downloaded_with_stream_full-sized.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 400x144.png_downloaded_with_stream_full-sized.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 400x500.png_downloaded_with_stream_full-sized.png
        Full-sized file downloaded to DownloadedImages\CanStoreFullImage true 60x80.png_downloaded_with_stream_full-sized.png
Deleting the image data from the columns using 3 different methods:
        CanStoreFullImage false 144x144.png sample_imagecolumn deleted with PATCH
        CanStoreFullImage false 144x400.png sample_imagecolumn deleted with PATCH
        CanStoreFullImage false 400x144.png sample_imagecolumn deleted with PATCH
        CanStoreFullImage false 400x500.png sample_imagecolumn deleted with PUT
        CanStoreFullImage false 60x80.png sample_imagecolumn deleted with PUT
        CanStoreFullImage true 144x144.png sample_imagecolumn deleted with PUT
        CanStoreFullImage true 144x400.png sample_imagecolumn deleted with DELETE
        CanStoreFullImage true 400x144.png sample_imagecolumn deleted with DELETE
        CanStoreFullImage true 400x500.png sample_imagecolumn deleted with DELETE
        CanStoreFullImage true 60x80.png sample_imagecolumn deleted with DELETE
Deleted the account 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 フォルダーにダウンロードされます。

    注意

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

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

このプログラムは、3 つの異なるメソッドを使用して画像ファイルをダウンロードします。

注意

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

アクション付きダウンロード

コードは静的な DownloadImageWithActions メソッドで、InitializeFileBlocksDownloadDownloadBlock 操作の使用をカプセル化し、Dataverse メッセージを使用してファイルをダウンロードするで説明されているように画像をダウンロードします。

注意

ダウンロードするフルサイズの画像がない場合、操作は失敗します。 エラー メッセージは、No FileAttachment records found for imagedescriptorId: <guid> for image attribute: sample_imagecolumn of account record with id <guid> です。

チャンクでダウンロード

このコードは、静的な DownloadImageWithChunks メソッドで、Web API を使用してチャンクでファイルをダウンロードする で説明されているように画像のダウンロード方法を説明します。

注意

ダウンロードするフルサイズの画像がない場合、操作は失敗せず、204 No Content を返します。

ストリームでダウンロード

このコードは、静的な DownloadImageWithStream メソッドで、Web API を使用してファイルを 1 回の要求でダウンロードする で説明されているように画像のダウンロード方法を説明します

注意

ダウンロードするフルサイズの画像がない場合、操作は失敗せず、204 No Content を返します。

画像データの削除

  1. このプログラムでは、PATCHPUTDELETE を使用して、3 つの異なるメソッドで画像値を削除する方法を示しています。
  2. プログラムは、以前と同じ基準を使用してレコードを再度取得することにより、レコードが削除されたことを確認します。 画像列の値が null 値です。

クリーンアップ

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

  • 取引先企業レコードの削除。
  • アカウント テーブルのプライマリ画像を元の値に戻す。
  • 画像列の削除。

関連情報

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

注意

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

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