生成页的 Dataverse 表方法

使用对象的表方法 dataApi 创建、检索、更新和删除 Dataverse 表行,以及查询选择列。

方法 Description
createRow 在指定的表中创建新行。
deleteRow 从指定的表中删除行。
getChoices 检索指定选择列名称的选项。
queryTable 使用指定选项查询表。
retrieveRow 从具有指定选项的指定表中检索行。
updateRow 更新指定表中的现有行。

createRow 方法

在指定的表中创建新行。

Parameters

设置这些必需参数的值。

Name 类型 Description
tableName 字符串 要在其中创建行的表的逻辑名称。
row 对象 要创建的行数据。

Returns

包含作结果的 承诺 。 作成功后,结果为所创建行的 ID (Guid) 值。

createRow 示例

// Define the row data to create new account
var row =
    {
        "name": "Sample Account",
        "creditonhold": false,
        "address1_latitude": 47.639583,
        "description": "This is the description of the sample account",
        "revenue": 5000000,
        "accountcategorycode": 1,
    }

try {
  // Create a new account record
  const newAccountId = await dataApi.createRow("account", row);
  console.log("Account created with ID: " + newAccountId);

  // Create a contact with a lookup to an account
  const newContactId = await dataApi.createRow('contact', {
    firstname: 'John',
    lastname: 'Doe',
    emailaddress1: 'john.doe@contoso.com',
    "parentcustomerid@odata.bind": `/account(${newAccountId})`, // Lookup format
  });
}
catch (error) {
  console.log(error.message);
}

deleteRow 方法

从指定的表中删除行。

Parameters

设置这些必需参数的值。

Name 类型 Description
tableName 字符串 要在其中删除行的表的逻辑名称。
rowId 字符串 要删除的行的 ID。

Returns

包含作结果的 承诺 。 作成功后,它不返回任何值。

deleteRow 示例

let rowId = "5531d753-95af-e711-a94e-000d3a11e605";
try {
  await dataApi.deleteRow("account", rowId);
}
catch (error) {
  console.log(error.message);
}

getChoices 方法

检索指定选择列名称的选项。

Parameters

此参数是必需的。

Name 类型 Description
enumName string 格式的选择列的名称 tablename-columnname

Returns

包含作结果的 承诺 。 作成功后,它将返回选项数组。 每个选项都有以下属性:

Name 类型 Description
label 字符串 选项的本地化标签值。
value number 选项的数值。

getChoices 示例

// Returns the accountcategorycode column options from the account table
const categoryChoices = await dataApi.getChoices("account-accountcategorycode");
// Returns the statecode column options from the contact table
const stateChoices = await dataApi.getChoices('contact-statecode');
// Returns the statuscode column options from the account table
const statusChoices = await dataApi.getChoices('account-statuscode');

queryTable 方法

使用指定的选项查询表。

Parameters

设置这些必需参数的值。

Name 类型 Description
tableName string 要查询的表的逻辑名称
query QueryTableOptions 用于查询表的选项

QueryTableOptions

Name 类型 Description
select string[] (推荐)要检索的列名数组。
filter string (可选)OData 筛选器表达式(例如 statecode eq 0, )。
orderBy string (可选)OData orderby 表达式(例如,name asccreatedon desc)。
pageSize number (可选)每页要返回的最大行数。

Returns

包含作结果的 承诺 。 作成功后,promise 将返回一个包含具有以下属性结果的数据表的对象:

Name 类型 Description
rows Object[] 行数据的数组
hasMoreRows boolean 指示是否有更多行可用
loadMoreRows function 用于加载下一页结果的函数。 (可选)

queryTable 示例

// Query tasks with options
const result = await dataApi.queryTable("task", {
    select: ["activityid", "subject", "scheduledend", "prioritycode", "statecode"],
    orderBy: "scheduledend asc",
    pageSize: 50,
    filter: "statecode eq 0"
});

// Query accounts with pagination
const pagedAccounts = await dataApi.queryTable('account', {
  select: ['name'],
  pageSize: 50,
});

console.log(`Page 1: ${pagedAccounts.rows.length} accounts`);

if (pagedAccounts.hasMoreRows && pagedAccounts.loadMoreRows) {
  const nextPage = await pagedAccounts.loadMoreRows();
  console.log(`Page 2: ${nextPage.rows.length} accounts`);
}

Remarks

注释

为了获得最佳性能,请始终使用 QueryTableOptionsselect 属性限制返回的列数。

retrieveRow 方法

使用指定的选项从指定表中检索行。

Parameters

设置这些必需参数的值。

Name 类型 Description
tableName string 要从中检索的表的逻辑名称
options RetrieveRowOptions 用于检索行的选项

RetrieveRowOptions

Name 类型 Description
id string 要检索的行的 ID (Guid)
select string[] (推荐)要检索的列名数组。 如果省略,则返回所有列。

Returns

包含作结果的 承诺 。 操作成功后,它将返回包含记录数据的对象,包括所有选定的列。

retrieveRow 示例

// Retrieve an account with all columns
const account = await dataApi.retrieveRow('account', {
  id: '30dc51e9-947d-47d8-ad48-4fc48fba4a95',
});

// Retrieve specific columns only
const contact = await dataApi.retrieveRow('contact', {
  id: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
  select: ['firstname', 'lastname', '_parentcustomerid_value'],
});

updateRow 方法

更新指定表中的现有行。

Parameters

设置这些必需参数的值。

Name 类型 Description
tableName 字符串 要在其中更新行的表的逻辑名称。
rowId 字符串 要更新的行的 ID。
row Object 要更新的行数据。

Returns

包含作结果的 承诺 。 作成功后,它不返回任何值。

updateRow 示例

let rowId = "5531d753-95af-e711-a94e-000d3a11e605"

// Define the row to update a record
var row =
    {
        "name": "Updated Sample Account ",
        "creditonhold": true,
        "address1_latitude": 47.639583,
        "description": "This is the updated description of the sample account",
        "revenue": 6000000,
        "accountcategorycode": 2
    }

// update the record

try {
   await dataApi.updateRow("account", rowId, row);
}
catch (error){
  console.log(error.message);
}