crm - 如何在Dynamics CRM 2016中检查是否加载了web资源控件

标签 crm dynamics-crm-2016

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

  var webResource = $(window.parent.Xrm.Page.getControl(webResourceName).getObject().contentWindow.window.document.getElementById(dropDownName));

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

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

最佳答案

您有多种选择。

有你的网页资源触发代码

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

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

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

使用jQuery的加载方法

我不记得我是否真的尝试过这个,但你应该能够使用 jQuery 的加载方法。在 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
});

检测网络资源何时准备就绪

这种方法可以满足您的要求,也是我在开箱即用的表单上使用 jQuery 之前使用的方法(包括它供 future 可能使用 jQuery 不可用的旧版 CRM 的读者使用) .它等待 web 资源完成加载,然后调用回调。将此函数添加到加载到您的 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);
}

然后像这样使用它:

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);
    // ....
});

关于crm - 如何在Dynamics CRM 2016中检查是否加载了web资源控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39852140/

相关文章:

dynamics-crm - Dynamics CRM 营销列表动态日期条件

sdk - Dynamics CRM 使用 C# 代码合并两个联系人,从 SDK 修改示例

dynamics-crm-2011 - 使用 Excel 工作表和自定义标识符更新 CRM 数据

c# - 带有引用的插件不适用于 CRM 2011

mysql - 与 CRM 相比,MSReport 显示的到期日期晚了 1 天?

dynamics-crm - 如何在 Dynamics 365 中使用聚合作为系统 View 实现自定义 FetchXML

javascript - 重新加载父窗口 CRM 在线 Google Chrome

javascript - 使用动态过滤器在新窗口中打开 View

javascript - 无法重定向到 Dynamics CRM 仪表板内的新网页

c# - Dynamics CRM Web api 401 获取 Oauth token 后未经授权