この.NET 6.0 サンプルでは、Dataverse Web API を使用してイメージ列で操作を実行する方法を示します。
GitHub でこのサンプルを表示する
このサンプルでは、WebAPIService クラス ライブラリ (C#) の共通ヘルパー コードを使用します。
前提条件
- Microsoft Visual Studio 2022
- システム管理者権限またはシステムカスタマイザー権限を持つ Dataverse へのアクセス。
サンプルを実行する方法
PowerApps-サンプル リポジトリをクローンまたはダウンロードします。
Visual Studio 2022 を使用して、
PowerApps-Samples\dataverse\webapi\C#-NETx\ImageOperations\ImageOperations.slnファイルを開きます。ファイルを編集して、以下のプロパティ値を設定します:
プロパティ 手順 Urlご使用の環境のURL。 プレースホルダー の値を環境の値に置き換えます。 詳細については、開発者向けリソースを表示する を参照してください。 UserPrincipalNameプレースホルダー の値を環境へのアクセスに使用する UPN の値に置き換えます。 Passwordプレースホルダーの値を使用しているパスワードに置き換えてください。 ファイルを保存します
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.cs ファイルにあります。
このプロジェクトは、次の操作を実行します:
画像列の作成
このサンプルでは、アカウント テーブルのプライマリ画像である新しい画像列を作成する必要があります。 また、終了時にはシステムを元の状態に戻す必要があります。 したがって、プログラムは最初に次のことを行います。
メソッドを使用して元のプライマリ画像名を取得します。
存在しない場合は、メソッドを使用して、アカウントテーブルに新しい画像列を作成します。
ヒント
この画像列の 値は false です。
メソッドを使用して、新しい 画像列をプライマリにします。
画像データでアカウント レコードを作成する
- プログラムは、 フォルダーにあるファイル名に一致する 5 つのファイル名のリストをループします。
- 画像ごとに、プログラムは を に設定してアカウント レコードを作成し、ファイル を 値として設定します。
- 次に、プログラムは メソッドを使用して、 列定義 の値を true に設定します。
- プログラムはファイル名を順に処理し、同じ画像ファイルを使用して5つのアカウントレコードを作成し、その画像ファイルを値として設定します。 今回は、 は です
次のコードでは、 プロパティの値が、利用可能なデータがにどのように変化するかを確認できます。
アカウントレコードの取得
このコードは、画像データを含む、前のステップで作成された 10 件のアカウント レコードを取得します。
アカウント レコードごとに、画像データが という名前の付いた フォルダーにダウンロードされます。
ヒント
これらのレコードはすべてサムネイル サイズの画像です。
アカウント レコードの画像をダウンロードする
このプログラムは、3 つの異なるメソッドを使用して画像ファイルをダウンロードします。
ヒント
いずれの場合も、 が false のときにフルサイズの画像がアップロードされなかったため、10 回の操作のうち 5 回が失敗します。 それらのレコードは条件が真の間に作成され、その後成功します。
アクション付きダウンロード
コードは静的な メソッドで、InitializeFileBlocksDownload と DownloadBlock 操作の使用をカプセル化し、Dataverse メッセージを使用してファイルをダウンロードするで説明されているように画像をダウンロードします。
ヒント
ダウンロードするフルサイズの画像がない場合、操作は失敗します。 エラー メッセージは、 です。
分割ダウンロード
このコードは、静的な メソッドで、Web API を使用してチャンクでファイルをダウンロードする で説明されているように画像のダウンロード方法を説明します。
ヒント
ダウンロードするフルサイズの画像がない場合、操作は失敗せず、 を返します。
ストリームでダウンロード
このコードは、静的な メソッドで、Web API を使用してファイルを 1 回の要求でダウンロードする で説明されているように画像のダウンロード方法を説明します
ヒント
ダウンロードするフルサイズの画像がない場合、操作は失敗せず、 を返します。
画像データの削除
- このプログラムでは、、、 を使用して、3 つの異なるメソッドで画像値を削除する方法を示しています。
- プログラムは、以前と同じ基準を使用してレコードを再度取得することにより、レコードが削除されたことを確認します。 画像列の値が null 値です。
クリーンアップ
サンプルを実行する前の状態にするために、次の処理を行います。
- アカウントレコードの削除。
- アカウント テーブルのプライマリ画像を元の値に戻す。
- 画像列の削除。
参照
画像列のデータを使用する
コードを使って画像列定義を操作する
ファイル列のデータを使用する