1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

C#でDataverseにテーブルを作成してみた

Last updated at Posted at 2023-01-29

はじめに

PowerAppsのモデル駆動の設定が面倒なのでC#からDataverseを操作してみたくなった。
今回はdataverseにテーブルを作成するC#のコードを動かしてみる。

前提

参照ライブラリやCreateCrmService関数の中身は以前の以下の記事を参考に

C#でDataverseに接続してテーブル一覧を表示してみた
https://qiita.com/akihiroe_/items/2db8e2634d7962ef0df7

コード

CreateEntityRequestを作成して実行すれば良いだけのようだが、Web画面で作成する場合と同様にプライマリ列の指定が必要。

var organizationService = CreateCrmService(user, password, url);

var tableLogicalName = "test_table1";
var tableDisplayName = "テーブル1";
var primaryNameColumnLogicalName = "test_name";
var primaryNameColumnDisplayName = "名前列";

CreateEntityRequest createrequest = new CreateEntityRequest
{
    Entity = new EntityMetadata
    {
        SchemaName = tableLogicalName,
        DisplayName = new Label(tableDisplayName, 1041),
        DisplayCollectionName = new Label(tableDisplayName, 1041),
        OwnershipType = OwnershipTypes.UserOwned,
    },
    PrimaryAttribute = new StringAttributeMetadata
    {
        SchemaName = primaryNameColumnLogicalName,
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
        MaxLength = 100,
        DisplayName = new Label(primaryNameColumnDisplayName, 1041),
    }
};
var createResponse = organizationService.Execute(createrequest) as CreateEntityResponse;
Console.WriteLine(createResponse.Results["EntityId"]);

*Labelクラスの引数1041は日本語の言語コード

おわりに

テーブルの作成もシンプルなコードでいけました。当初列名にnameを指定していると「The schema name name for type Attribute is invalid or missing....」とエラーになったので注意。これはWeb画面でも強制されるソリューションの接頭辞をつけて解決しました。
なお、列の追加も参考情報で紹介されている方法でできました。これでExcelで記載したテーブル定義書から自動的にDataverseのテーブルを作成することができそう。:smiley:

参照情報

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?