如何检查 Dynamics CRM 2016 中是否加载了 Web 资源控件

Pon*_*mal 3 crm dynamics-crm-2016

必须从另一个 webresource 控件访问 Webresource 控件使用以下 javascript,

  var webResource = $(window.parent.Xrm.Page.getControl(webResourceName).getObject().contentWindow.window.document.getElementById(dropDownName));
Run Code Online (Sandbox Code Playgroud)

但有时它工作正常,有时它返回空值。

因此可以检查网络资源是否已加载。

Pol*_*ant 5

您有多种选择。

拥有您的网络资源触发代码

我最喜欢的方法是走另一个方向:在您的 Web 资源中,添加代码以开始在父 CRM 表单上执行。您可以使用 jQuery 的就绪方法,或者您可以在此处阅读的不涉及 jQuery 的多种方法之一。如果您使用 jQuery,您的 Web 资源可能如下所示:

$.ready(function() {
    window.parent.Xrm.Page.getAttribute('name').setValue('test'); // or whatever your webresource needs to do
});
Run Code Online (Sandbox Code Playgroud)

这个想法是,Web 资源在准备就绪时会触发一些逻辑,这样您就可以避免必须检测 Web 资源何时完成加载的麻烦。

使用jQuery的load方法

我不记得我是否真的尝试过这个,但你应该能够使用 jQuery 的 load 方法。在 CRM 表单上的脚本中,执行如下操作:

$('iframe#WebResource_xyz').on('load', function() {
    // Here, the 'this' object will refer to the iframe object

    this.contentWindow.document.getElementById(dropDownName); // you might have to tweak this slightly, didn't test it
});
Run Code Online (Sandbox Code Playgroud)

检测 Web 资源何时准备就绪

这种方法可以满足您的要求,也是我在 jQuery 可用于开箱即用的表单之前使用的方法(包括为可能使用 jQuery 不可用的旧版本 CRM 的未来读者提供的方法)。它等待网络资源完成加载,然后调用回调。将此函数添加到加载到 CRM 表单上的脚本中:

// Waits for web resource to be ready and then invokes the callback.
// webResourceId: the id of either a web resource or an iframe on the form
// urlCheck: this string will be checked for in the iframe's url to make 
//           sure it is on the right page. can be any part of the url, 
//           doesn't have to be the whole thing.
// callback: Called once the iframe is ready. The context of the callback 
//           method will be set to the iframe's window, so the callback can 
//           use "this" to refer to the iframe window.
function waitForWebResourceReady(webResourceId, urlCheck, callback) {
    var tryCount = arguments[3] || 0;
    var control = Xrm.Page.getControl(webResourceId);

    if (!control ||
        !control.getObject() ||
        !control.getObject().contentWindow ||
        !control.getObject().contentWindow.location ||
        !control.getObject().contentWindow.location.href ||
        control.getObject().contentWindow.location.href.indexOf(urlCheck) < 0 ||
        control.getObject().readyState !== 'complete') {
        if (tryCount > 50) {
            console.log("waitForWebResourceReady: " + 
                "Failed to reach ready state on " + webResourceId);
            return;
        }

        console.log("waitForWebResourceReady: " + webResourceId + " not ready yet");
        window.setTimeout(function () {
            waitForWebResourceReady(webResourceId, urlCheck, callback, ++tryCount);
        }, 20);
        return;
    }

    console.log("waitForWebResourceReady: " + webResourceId + " is ready");
    callback.call(control.getObject().contentWindow);
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

waitForWebResourceReady('WebResource_xyz', 'mycontrol.html', function () {
    // In this context, 'this' will refer to the window object of the webresource
    var dropdown = this.document.getElementById(dropDownName);
    // ....
});
Run Code Online (Sandbox Code Playgroud)