如何从嵌入自定义页面的PCF控件访问输入参数?能否传递实体对象?
如何从PCF控件访问自定义页面的输入参数及传递完整实体对象
一、PCF控件访问自定义页面的输入参数
要让嵌入自定义页面的PCF控件拿到传递的recordId,核心通过自定义页面变量绑定+PCF控件自定义属性实现:
- 在自定义页面绑定变量到PCF控件
你已经通过Set(inputParameters;JSON(Param("recordId")))把参数存到了变量里,接下来编辑自定义页面:
- 选中你的PCF控件,在右侧属性面板找到「自定义属性」(如果没有该选项,需要先在PCF控件的
ControlManifest.Input.xml里定义对应输入参数)。 - 将自定义属性的值绑定到
inputParameters变量。
- 在PCF控件代码中读取参数
先在PCF的ControlManifest.Input.xml里添加接收参数的定义:
<property name="recordIdParam" display-name-key="Record ID Parameter" description-key="Receives record ID from custom page" of-type="SingleLine.Text" usage="bound" required="true" />
然后在PCF的TypeScript代码里获取参数:
export class YourPCFControl implements ComponentFramework.StandardControl<IInputs, IOutputs> { private recordId: string; public init(context: ComponentFramework.Context<IInputs>, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container: HTMLDivElement): void { // 读取自定义属性的值并解析为原始ID this.recordId = JSON.parse(context.parameters.recordIdParam.raw || ""); // 后续可使用该recordId进行逻辑处理 } // 其他方法省略... }
二、传递完整实体对象到自定义页面及PCF控件
可以实现,步骤如下:
- 在现代命令中加载完整实体并传递
修改原命令代码,先调用Web API获取完整实体数据,再将序列化后的JSON作为自定义参数传入自定义页面:
// 先加载完整实体数据 Xrm.WebApi.retrieveRecord(OpportunityProduct.name, opportunityproductId, "?$select=name,priceperunit,quantity") .then(function(retrievedEntity) { const pageInput = { pageType: "custom", name: "we_technicaladdresssearchpage_5b4f6", entityName: OpportunityProduct.name, recordId: opportunityproductId, // 添加自定义参数传递完整实体 entityData: JSON.stringify(retrievedEntity) } as any; var navigationOptions = { target: 2, position: 1, width: { value: 90, unit: "%" } } as any; Xrm.Navigation.navigateTo(pageInput, navigationOptions); }) .catch(function(error) { console.error("加载实体失败:", error); });
- 在自定义页面接收并保存实体对象
更新自定义页面的变量设置:
// 同时保存ID和完整实体对象 Set(inputRecordId; Param("recordId")); Set(inputEntityData; JSON(Param("entityData")))
之后将inputEntityData绑定到PCF控件的对应自定义属性上。
- 在PCF控件中解析实体对象
先在ControlManifest.Input.xml添加对应属性:
<property name="entityDataParam" display-name-key="Entity Data Parameter" description-key="Receives full entity data from custom page" of-type="SingleLine.Text" usage="bound" required="false" />
然后在PCF代码里解析:
export class YourPCFControl implements ComponentFramework.StandardControl<IInputs, IOutputs> { private entityData: any; public init(context: ComponentFramework.Context<IInputs>, notifyOutputChanged: () => void, state: ComponentFramework.Dictionary, container: HTMLDivElement): void { // 解析完整实体对象 if (context.parameters.entityDataParam.raw) { this.entityData = JSON.parse(context.parameters.entityDataParam.raw); // 示例:获取实体字段值 this.entityData.name } } // 其他方法省略... }
内容的提问来源于stack exchange,提问作者Patrick Seewald
相关产品推荐
相关产品推荐

