本文提供了使用 客户端 API 从模型驱动应用页导航到自定义页面的示例。 了解如何在模型驱动应用中以完整页面、对话框或窗格视图的形式打开自定义页面。
本文概述了使用客户端 API 以完整页面、对话框或窗格的形式打开自定义页面的步骤。 它提供了 custom 作为 pageType 值在 navigateTo(客户端 API 参考)中的示例。
重要
自定义页面是一个具有重要产品变更的新功能。 它们目前在 自定义页面已知问题中概述了许多已知限制。
查找逻辑名称
以下每个客户端 API 示例都采用自定义页的逻辑名称作为必需参数。 逻辑名称是解决方案资源管理器中页面名称的值。
以无上下文的内联完整页的形式打开
在模型驱动应用客户端 API 事件处理程序中,调用以下代码并将 名称 参数更新为页面的逻辑名称。
// Inline Page
var pageInput = {
pageType: "custom",
name: "<logical name of the custom page>",
};
var navigationOptions = {
target: 1
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions)
.then(
function () {
// Called when page opens
}
).catch(
function (error) {
// Handle error
}
);
以记录上下文形式打开为内联全屏页面
此示例使用 recordIdNavigateTo 函数中的参数来提供要使用的记录的自定义页面。
// Inline Page
var pageInput = {
pageType: "custom",
name: "<logical name of the custom page>",
entityName: "<logical name of the table>",
recordId: "<record id>",
};
var navigationOptions = {
target: 1
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions)
.then(
function () {
// Called when page opens
}
).catch(
function (error) {
// Handle error
}
);
Param自定义页中的函数检索值,并使用 Lookup 函数检索记录。
App.OnStart=Set(RecordItem,
If(IsBlank(Param("recordId")),
First(<entity>),
LookUp(<entity>, <entityIdField> = GUID(Param("recordId"))))
)
重要
参数 recordId 必须是 GUID,因为它会更新 URL,并且应用从 URL 开始验证 recordId 是否为 GUID。
以居中对话框形式打开
在模型驱动应用客户端 API 事件处理程序中,调用以下代码,并将 名称 参数更新为自定义页面的逻辑名称。 此模式支持类似于 “主窗体”对话框的大小调整参数。
// Centered Dialog
var pageInput = {
pageType: "custom",
name: "<logical custom page name>",
};
var navigationOptions = {
target: 2,
position: 1,
width: {value: 50, unit:"%"},
title: "<dialog title>"
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions)
.then(
function () {
// Called when the dialog closes
}
).catch(
function (error) {
// Handle error
}
);
作为侧对话打开
在模型驱动应用客户端 API 事件处理程序中,调用以下代码,并将 名称 参数更新为自定义页面的逻辑名称。
// Side Dialog
var pageInput = {
pageType: "custom",
name: "<logical page name>",
};
var navigationOptions = {
target: 2,
position: 2,
width: {value: 500, unit: "px"},
title: "<dialog title>"
};
Xrm.Navigation.navigateTo(pageInput, navigationOptions)
.then(
function () {
// Called when the dialog closes
}
).catch(
function (error) {
// Handle error
}
);
通过网格主字段链接打开为包含记录 ID 的全屏页面
此示例使用 recordIdnavigateTo 函数中的参数来提供要使用的记录的自定义页面。 自定义 Param 页中的函数检索值,并使用 Lookup 函数检索记录。
可以在 实体绑定网格中替代数据行的默认打开行为中找到更完整的示例。
创建 类型为 JScript 的 Web 资源,并将 名称 参数更新为逻辑页名称。 将以下代码添加到 Web 资源。
function run(selectedItems) { let selectedItem = selectedItems[0]; if (selectedItem) { let pageInput = { pageType: "custom", name: "<logical page name>", entityName: selectedItem.TypeName, recordId: selectedItem.Id, }; let navigationOptions = { target: 1 }; Xrm.Navigation.navigateTo(pageInput, navigationOptions) .then( function () { // Handle success } ).catch( function (error) { // Handle error } ); } }自定义表功能区 CommandDefinition 中的 OpenRecordItem,以便更早调用该函数,并包含值为 SelectedControlSelectedItemReferences 的 CrmParameter。
<CommandDefinition Id="Mscrm.OpenRecordItem"> <Actions> <JavaScriptFunction FunctionName="run" Library="$webresource:cr62c_OpenCustomPage"> <CrmParameter Value="SelectedControlSelectedItemReferences" /> </JavaScriptFunction> </Actions> </CommandDefinition>在自定义页面中,重写 App 的 OnStart 属性,以使用
Param函数获取recordId和lookup记录。App.OnStart=Set(RecordItem, If(IsBlank(Param("recordId")), First(<entity>), LookUp(<entity>, <entityIdField> = GUID(Param("recordId")))) )注释
更改
OnStart属性后,需要从“应用”上下文菜单运行OnStart。 此函数仅在会话中执行一次。然后,自定义页面可以使用 RecordItem 参数作为记录。 以下示例演示如何在 EditForm 中使用它。
EditForm.Datasource=<datasource name> EditForm.Item=RecordItem
从可编辑网格中的选定记录打开,作为包含记录 ID 的居中对话框
如果要在视图中选择特定记录后执行操作,请使用可编辑网格触发 OnRecordSelect 事件。 此示例使用 recordIdnavigateTo 函数中的参数来提供要使用的记录的自定义页面。 在 GridEntity 对象中的 getId 方法可检索记录 ID。 自定义 Param 页中的函数检索值,并使用 lookup 函数检索记录。
在表中启用可编辑的网格 控件。
创建 类型为 JScript 的 Web 资源,并将 名称 参数更新为逻辑页名称。 将以下代码添加到 Web 资源。
function RunOnSelected(executionContext) { // Retrieves the record selected in the editable grid var selectedRecord = executionContext.getFormContext().data.entity; var Id = selectedRecord.getId().replace(/[{}]/g, ""); // Centered Dialog var pageInput = { pageType: "custom", name: "<logical page name>", recordId: Id, }; var navigationOptions = { target: 2, position: 1, width: { value: 50, unit: "%" }, title: "<dialog title>" }; Xrm.Navigation.navigateTo(pageInput, navigationOptions) .then( function () { // Called when the dialog closes } ).catch( function (error) { // Handle error } ); }在自定义页面中,重写 App 的 OnStart 属性,以使用
Param函数获取recordId和lookup记录。App.OnStart=Set(RecordItem, If(IsBlank(Param("recordId")), First(<entity>), LookUp(<entity>, <entityIdField> = GUID(Param("recordId")))) )注释
更改
OnStart属性后,需要从“应用”上下文菜单运行OnStart。 此函数仅在会话中执行一次。然后,自定义页面可以使用 RecordItem 参数作为记录。 以下示例演示如何在 EditForm 中使用它。
EditForm.Datasource=<datasource name> EditForm.Item=RecordItem