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

D365 Online中SiteMap加载的HTML Web Resource的Xrm对象问题咨询

Let's break down your confusion and get you the compliant, future-proof solution you need:

First, clearing up the documentation vs. behavior discrepancies

  • The confusion around ClientGlobalContext.js.aspx comes from a key detail: Microsoft’s docs state this script doesn’t directly provide the Xrm object—that’s correct. Its primary purpose is to expose the GetGlobalContext() method. The reason you saw Xrm working when you included it is because your Angular HTML Web Resource is loaded as part of the SiteMap, meaning it’s embedded within the main model-driven app’s frame. The Xrm object was already present from the parent app, and including the script didn’t overwrite or block it.
  • As for parent.Xrm working despite docs saying it shouldn’t: Older docs might have described scenarios where Web Resources are loaded in isolated popups (where parent wouldn’t point to the main app frame), but when loaded directly in the SiteMap (as part of the main app’s UI), your Web Resource lives in a subframe where parent does reference the main app’s window. That said, relying on parent.Xrm is risky—if Microsoft ever changes the app’s frame structure, this could break.

The compliant, non-deprecated approach

The official, future-proof way to access the client API (including Web API methods like retrieveMultipleRecords) is to use the global context retrieved via GetGlobalContext()—this is explicitly recommended by Microsoft and avoids relying on fragile frame references or direct Xrm access.

Here’s how to implement it properly:

  1. Keep the reference to ClientGlobalContext.js.aspx (this is required to get GetGlobalContext()):
<head>
    <!-- Reference the global context script -->
    <script src="ClientGlobalContext.js.aspx" type="text/javascript"></script>
</head>
  1. Use the context object instead of direct Xrm calls:
// Get the global context
const globalContext = GetGlobalContext();

// Call Web API via the context object
globalContext.webApi.retrieveMultipleRecords("account", "?$select=name,accountid")
    .then((result) => {
        // Handle your retrieved records
        console.log("Records retrieved:", result.entities);
    })
    .catch((error) => {
        console.error("Error fetching records:", error);
    });

Why this is better than your current workarounds

  • Avoids frame dependency: GetGlobalContext() works regardless of how your Web Resource is loaded (SiteMap, popup, etc.)—it doesn’t rely on parent or direct Xrm access.
  • Future-proof: This method is explicitly documented and supported by Microsoft, so you don’t have to worry about it being deprecated or breaking in future model-driven app updates.

If you absolutely don’t want to include ClientGlobalContext.js.aspx, you can use Xrm.Utility.getGlobalContext()—but you need to ensure Xrm is fully initialized first (since it depends on the parent app’s Xrm object). Here’s a safe way to do that:

function initializeApp() {
    if (typeof Xrm !== "undefined") {
        const globalContext = Xrm.Utility.getGlobalContext();
        // Proceed with your Web API calls here
    } else {
        // Retry if Xrm isn't loaded yet
        setTimeout(initializeApp, 100);
    }
}

document.addEventListener("DOMContentLoaded", initializeApp);

That said, the first approach with ClientGlobalContext.js.aspx is still the most reliable.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 17:12:36