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

如何在Dynamics CRM记录列表中通过JavaScript获取视图名称?

Absolutely! You can definitely retrieve the current view name for a Dynamics CRM record list using JavaScript—let’s break down the approach based on whether you’re working with the classic CRM UI or the modern model-driven apps (Power Apps):

Classic Dynamics CRM UI Method

If you’re still using the classic interface, you can access the grid control directly via Xrm.Page to get the active view details:

function getCurrentClassicViewName() {
    // Replace "yourGridControlName" with the actual name of your subgrid control
    const gridControl = Xrm.Page.getControl("yourGridControlName");
    
    if (gridControl) {
        const currentView = gridControl.getViewSelector().getCurrentView();
        return currentView.name; // Returns the human-readable display name of the view
    }
    return null; // Returns null if the grid control isn't found
}

Quick tip: To find your grid’s control name, open the form editor, select the subgrid, and check the "Name" field in the properties pane.

Modern Model-Driven Apps (Power Apps) Method

For the modern UI, Xrm.Page is deprecated, so we use the form context and async/await since many grid operations are now asynchronous:

async function getCurrentModernViewName(formContext) {
    // Pass the form context from your event handler (e.g., on form load)
    const gridControl = formContext.getControl("yourGridControlName");
    
    if (gridControl && gridControl.getGrid) {
        const grid = gridControl.getGrid();
        const currentView = await grid.getViewSelector().getCurrentView();
        return currentView.name;
    }
    return null;
}

// Example usage in a form load event:
function onFormLoad(executionContext) {
    const formContext = executionContext.getFormContext();
    getCurrentModernViewName(formContext).then(viewName => {
        console.log("Current view name:", viewName);
        // Do something with the view name here
    });
}
Standalone List View (Not on a Form)

If you’re on a standalone list page (like navigating directly to a record type’s list), you’ll need to fetch the view details via the Web API using the view ID from the URL:

async function getStandaloneListViewName() {
    const globalContext = Xrm.Utility.getGlobalContext();
    const queryParams = new URLSearchParams(window.location.search);
    const viewId = queryParams.get("viewid");
    
    if (!viewId) return null;
    
    try {
        const response = await fetch(
            `${globalContext.getClientUrl()}/api/data/v9.2/savedqueries(${viewId})`,
            {
                headers: {
                    "OData-MaxVersion": "4.0",
                    "OData-Version": "4.0",
                    "Accept": "application/json",
                    "Content-Type": "application/json; charset=utf-8"
                }
            }
        );
        
        const viewData = await response.json();
        return viewData.name;
    } catch (error) {
        console.error("Error retrieving view name:", error);
        return null;
    }
}

// Call the function to get the view name
getStandaloneListViewName().then(viewName => {
    console.log("Current list view name:", viewName);
});

Important notes:

  • Always ensure the grid has finished loading before calling these functions—otherwise, you might get a null result. You can use grid load events to trigger your code.
  • Make sure the current user has read permissions on the savedqueries entity (which stores view definitions) for the Web API method to work.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:20:59