自定义表和列

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")

Important

所有自定义列名都必须包括自定义前缀值(例如“new_”)。 此要求可确保显式、可预测的命名,并符合 Dataverse 元数据要求。

有关处理自定义表元数据的详细信息:

  • create始终返回 GUID 列表(单条输入时长度=1)。
  • updatedelete 对于单个接口和多个接口返回 None
  • create 传递有效负载列表将触发批量创建,并返回 list[str] 个 ID。
  • get 支持通过记录 ID 检索单条记录或分页浏览结果集(建议使用 select 限制列)。
  • 对于接受记录 ID 作为参数的 CRUD 方法,请传递 36 个字符、带连字符的 GUID 字符串。 GUID 周围的括号是允许的,但不是必须的。

另见