SDK では、 カスタム テーブル と列の作成、更新、削除 (CUD) 操作、オプションのソリューションの関連付け、およびテーブル定義の取得と一覧表示がサポートされています。
カスタム テーブルを操作するためのコード例を見てみましょう。
# Create a custom table, including the customization prefix value in the schema names for the table and columns.
table_info = client.tables.create("new_Product", {
"new_Code": "string",
"new_Description": "memo",
"new_Price": "decimal",
"new_Active": "bool"
})
# Create with custom primary column name and solution assignment
table_info = client.tables.create(
"new_Product",
columns={
"new_Code": "string",
"new_Price": "decimal"
},
solution="MyPublisher", # Optional: add to specific solution
primary_column="new_ProductName", # Optional: custom primary column (default is "{customization prefix value}_Name")
)
# Get table information
info = client.tables.get("new_Product")
print(f"Logical name: {info['table_logical_name']}")
print(f"Entity set: {info['entity_set_name']}")
# List all tables
tables = client.tables.list()
for table in tables:
print(table)
# Add columns to existing table (columns must include customization prefix value)
client.tables.add_columns("new_Product", {"new_Category": "string"})
# Remove columns
client.tables.remove_columns("new_Product", ["new_Category"])
# List all columns (attributes) for a table to discover schema
columns = client.tables.list_columns("account")
for col in columns:
print(f"{col['name']} ({col.get('AttributeType')})")
# List only specific properties
columns = client.tables.list_columns(
"account",
select=["LogicalName", "SchemaName", "AttributeType"],
filter="AttributeType eq 'String'",
)
# Clean up
client.tables.delete("new_Product")
サポートされている列の種類
次の型文字列は、 create() および add_columns()で受け入れられます。
| タイプ | 使用可能なエイリアス |
|---|---|
string |
text |
memo |
multiline |
int |
integer |
decimal |
money |
float |
double |
bool |
boolean |
datetime |
date |
file |
— |
オプションセット (選択) 列の場合は、 IntEnum サブクラス (またはメンバーに整数値を持つ Enum ) を文字列ではなく列型の値として直接渡します。 SDK では、クラス メンバーを使用してオプション セットの値を定義します。
from enum import IntEnum
class Priority(IntEnum):
LOW = 1
MEDIUM = 2
HIGH = 3
table_info = client.tables.create("new_Task", {
"new_Title": "string",
"new_Priority": Priority, # optionset column
})
TableInfo の戻り値オブジェクト
client.tables.create() メソッドが TableInfo オブジェクトを返します。 プロパティに直接アクセスするか、旧バージョンとの互換性のために従来の dict-key 表記を使用します。
table_info = client.tables.create("new_Product", {"new_Code": "string"})
print(table_info.schema_name) # new_Product
print(table_info.logical_name) # new_product
print(table_info.entity_set_name) # new_products
print(table_info.columns_created) # ['new_Code', ...]
# Legacy dict-key access still works
print(table_info["table_schema_name"])
add_columns()メソッドと remove_columns() メソッドは、作成または削除する列名の一覧を返します。
get() メソッドは、テーブルのメタデータを返すか、テーブルが存在しない場合はNoneを返します。これにより、存在チェックに役立ちます。
代替キー
代替キーは、Dataverse で生成された GUID ではなく、1 つ以上のビジネス列によってレコードを識別します。
アップサート操作には代替キーが必要です。 table>Keys の下の Power Apps Maker ポータルで定義するか、client.tables.create_alternate_keyを使用してプログラムで定義します。
# Create an alternate key on the accountnumber column
key = client.tables.create_alternate_key(
"account",
"account_accountnumber_ak",
["accountnumber"],
display_name="Account Number",
)
print(f"Created key {key.schema_name} ({key.metadata_id}), status={key.status}")
# The key status transitions from Pending to Active asynchronously - poll before upserting
for k in client.tables.get_alternate_keys("account"):
if k.schema_name == "account_accountnumber_ak":
print(f"{k.schema_name}: {k.status}")
Important
PendingからActiveへの移行はすぐには行われません。 作成直後にキーの状態を確認し、 Active されるまで待ってから upsert 要求を発行します。 アクティブな代替キーがない場合、Dataverse は 400 エラーで upsert 要求を拒否します。
Important
すべてのカスタム列名には、カスタマイズ プレフィックス値 ("new_" など) を含める必要があります。 この要件により、明示的で予測可能な名前付けが保証され、Dataverse メタデータの要件に合わせて調整されます。
カスタム テーブル メタデータの操作の詳細については、以下を参照してください。
-
createは常に GUID のリストを返します (単一入力の場合は length=1)。 -
updateとdeleteは、1 つのインターフェイスと複数のインターフェイスの両方のNoneを返します。 - ペイロードのリストを
createに渡すと、一括作成が実行され、ID のlist[str]が返されます。 -
getでは、レコード ID を使用した単一レコードの取得または結果セットのページングがサポートされています (列を制限する選択を優先します)。 - レコード ID を受け取る CRUD メソッドの場合は、GUID 文字列 (ハイフネーションされた 36 文字) を渡します。 GUID を括弧で囲むことは受け入れられますが、必須ではありません。