You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Business Central中通过Page Parts创建嵌套Web Service?

在Business Central中通过Page Parts创建嵌套Web Service的问题

我在Business Central中创建了如下API页面:

page 60012 CustomerPL
{
    PageType = API;
    EntitySetName = 'customerPLs';
    EntityName = 'customerPL';
    APIPublisher = 'MyCompany';
    APIGroup = 'MyCompany';
    SourceTable = "Customer";
    InsertAllowed = false;
    ModifyAllowed = false;
    DeleteAllowed = false;

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field("Cliente"; Rec."No.")
                {
                    ApplicationArea = All;
                }
                part(partCustomerBanksPL; 60013)
                {
                    ApplicationArea = All;
                    EntitySetName = 'partCustomerBanksPLs';
                    EntityName = 'partCustomerBanksPL';
                    SubPageLink = "Customer No." = field("No.");
                }
            }
        }
    }
}

我的需求是创建包含银行、地址等信息的Customer Web Service,生成嵌套JSON。对应的ListPart代码如下:

page 60013 partCustomerBanksPL
{
    ApplicationArea = All;
    Caption = 'Bancos clientes PL';
    PageType = ListPart;
    SourceTable = "Customer Bank Account";
    
    layout
    {
        area(content)
        {
            repeater(General)
            {
                field("Code"; Rec."Code")
                {
                }
                field(Name; Rec.Name)
                {
                }
                field(IBAN; Rec.IBAN)
                {
                }
                field("SWIFT Code"; Rec."SWIFT Code")
                {
                }
            }
        }
    }
}

执行如下GET请求时出现错误:

https://api.businesscentral.dynamics.com/v2.0/{{tenant_id}}/Sandbox_PL/ODataV4/Company('MyCompany')/customerPL?$expand=partCustomerBanksPL

错误信息:

{
    "error": {
        "code": "BadRequest",
        "message": "Could not find a property named 'partCustomerBanksPL' on type 'NAV.customerPL'.  CorrelationId:  34fca7a0-ae62-4218-90f1-09ad53e0cfc0."
    }
}

请问如何在Business Central中用Page Parts创建嵌套Web Service?(注:代码已更新为匹配名称和驼峰命名)


解决方案

要通过Page Parts实现嵌套的API响应,需调整以下几个核心配置:

1. 为ListPart页面补充API属性

ListPart必须配置API相关属性,才能被识别为可关联的OData实体:

page 60013 partCustomerBanksPL
{
    ApplicationArea = All;
    Caption = 'Bancos clientes PL';
    PageType = ListPart;
    SourceTable = "Customer Bank Account";
    // 添加API标识属性
    APIPublisher = 'MyCompany';
    APIGroup = 'MyCompany';
    EntitySetName = 'customerBankAccountsPL';
    EntityName = 'customerBankAccountPL';

    layout
    {
        area(content)
        {
            repeater(General)
            {
                field("Code"; Rec."Code")
                {
                }
                field(Name; Rec.Name)
                {
                }
                field(IBAN; Rec.IBAN)
                {
                }
                field("SWIFT Code"; Rec."SWIFT Code")
                {
                }
            }
        }
    }
}

2. 修正主API页面的Part关联配置

主API页面的Part需要显式指定Name属性(对应OData导航属性名,需严格驼峰命名),同时移除冗余的EntitySetName和EntityName配置:

page 60012 CustomerPL
{
    PageType = API;
    EntitySetName = 'customerPLs';
    EntityName = 'customerPL';
    APIPublisher = 'MyCompany';
    APIGroup = 'MyCompany';
    SourceTable = "Customer";
    InsertAllowed = false;
    ModifyAllowed = false;
    DeleteAllowed = false;

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field("Cliente"; Rec."No.")
                {
                    ApplicationArea = All;
                    APIName = 'customerNo'; // 可选:规范JSON字段名
                }
                part(partCustomerBanksPL; 60013)
                {
                    ApplicationArea = All;
                    Name = 'partCustomerBanksPl'; // 导航属性名,需驼峰
                    SubPageLink = "Customer No." = field("No.");
                }
            }
        }
    }
}

3. 调整OData请求参数

请求中的$expand参数必须和主API页面Part的Name属性完全匹配,同时实体集名称需使用复数形式:

https://api.businesscentral.dynamics.com/v2.0/{{tenant_id}}/Sandbox_PL/ODataV4/Company('MyCompany')/customerPLs?$expand=partCustomerBanksPl

4. 验证API元数据

部署代码后,可通过以下URL查看API元数据,确认导航属性是否正确生成:

https://api.businesscentral.dynamics.com/v2.0/{{tenant_id}}/Sandbox_PL/ODataV4/$metadata

在元数据中查找customerPL实体,检查是否包含partCustomerBanksPl导航属性。


内容的提问来源于stack exchange,提问作者kuhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 00:22:49