MS CRM 2011 Form与Web Resource在JScript中的相互调用

在Form中可以添加一个html的web resource,在web resource里可以用JScript来使用REST Endpoint或者SOAD Endpoint。

 

你可以在Form中添加一个web resource,也可以为Form添加一个指向某web resource的Navigation Link。

MS CRM 2011 Form与Web Resource在JScript中的相互调用_第1张图片

MS CRM 2011 Form与Web Resource在JScript中的相互调用_第2张图片

 

不论是以哪种方式,web resource可以用parent.window.Xrm来获得CRM Form中的Xrm对象。举一个小例子,在account的Form中添加一个html的web resource bm_ContactReadPane.htm来模拟出一种类似Outlook的Read Pane的功能:选择不同的primary contact,web resource就显示出相应的contact的full name和 business phone。

 

bm_ContactReadPane.htm代码:

复制代码
<html> 
<head> 
    <title>title> 
    <script src="ClientGlobalContext.js.aspx">script> 
    <script type="text/jscript" src="bm_ContactReadPane.js">script> 
    <script type="text/jscript" src="bm_json.js">script>    
    <style type="text/css"> 
        td 
        { 
            font-family: Segoe UI, Tahoma, Arial; 
            font-size: 11px 
        } 
    style> 
head> 
<body onload="ContactReadPaneLibrary.OnLoad()"> 
<div> 
<table> 
    <tr> 
        <td>Full Name:td> 
        <td><input id="TextFullname" type="text" disabled="disabled" />td> 
    tr> 
    <tr> 
        <td>Business Phone:td> 
        <td><input id="TextBusinessPhone" type="text"/>td> 
    tr> 
    <tr> 
        <td>td> 
        <td><input id="TextHiddenId" type="text" style="display:none"/>td> 
    tr> 
table> 
div> 
body> 
html> 
复制代码

要注意上面代码中对的引用,引用了它以后才可以使用CRM的REST Endpoint或者 SOAP Endpoint。如果你的web resource为bm_html/ContactReadPane.htm,那么引用就要改为script src="../ClientGlobalContext.js.aspx">,注意文件夹的结构。

 

bm_ContactReadPane.js的代码:

复制代码
if (typeof (ContactReadPaneLibrary) == "undefined") { 
    ContactReadPaneLibrary = { __namespace: true }; 
}


ContactReadPaneLibrary = { 
    Name: "ContactReadPaneLibrary", 
    OnLoad: function () { 
        if (parent.window.Xrm.Page.getAttribute("primarycontactid").getValue() != null && parent.window.Xrm.Page.getAttribute("primarycontactid").getValue().length > 0) { 
            var primartycontact = parent.window.Xrm.Page.getAttribute("primarycontactid").getValue()[0]; 
            var contactid = primartycontact.id; 
            var oDataPath = Xrm.Page.context.prependOrgName("/xrmservices/2011/organizationdata.svc"); 
            var filter = "/ContactSet(guid'" + contactid + "')?$select=ContactId,Telephone1,FullName"; 
            var retrieveRecordsReq = new XMLHttpRequest(); 
            retrieveRecordsReq.open("GET", oDataPath + filter, true); 
            retrieveRecordsReq.setRequestHeader("Accept", "application/json"); 
            retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); 
            retrieveRecordsReq.onreadystatechange = function () { 
                ContactReadPaneLibrary.RetrieveContactCallBack(this); 
            }; 
            retrieveRecordsReq.send(); 
        } 
        else { 
            document.getElementById("TextFullname").value = ""; 
            document.getElementById("TextBusinessPhone").value = ""; 
            document.getElementById("TextHiddenId").value = ""; 
        } 
    }, 
    RetrieveContactCallBack: function (retrieveRecordsReq) { 
        if (retrieveRecordsReq.readyState == 4 /* complete */) { 
            if (retrieveRecordsReq.status == 200) { 
                //Success 
                //alert(retrieveRecordsReq.responseText); 
                if (retrieveRecordsReq.responseText) { 
                    var retrievedRecord = JSON.parse(retrieveRecordsReq.responseText).d; 
                    document.getElementById("TextFullname").value = retrievedRecord.FullName; 
                    document.getElementById("TextBusinessPhone").value = retrievedRecord.Telephone1 == null ? "" : retrievedRecord.Telephone1; 
                    document.getElementById("TextHiddenId").value = retrievedRecord.ContactId; 
                } 
            } 
            else { 
                alert("Error : " + retrieveRecordsReq.status + ": " + 
                    retrieveRecordsReq.statusText + ": " + 
                    JSON.parse(retrieveRecordsReq.responseText).error.message.value); 
            } 
        } 
    } 
}
复制代码

account Form中的代码,PrimaryContactOnChange为account的primary contact onchange的 event handler。

复制代码
if (typeof (AccountLibrary) == "undefined") { 
    AccountLibrary = { __namespace: true }; 
}


AccountLibrary = { 
    Name: "AccountLibrary", 
    PrimaryContactOnChange: function () { 
        var contactReadPane = Xrm.Page.getControl("WebResource_ContactReadPane").getObject().contentWindow; 
        var contactReadPane2 = Xrm.Page.getControl("WebResource_ContactReadPane2").getObject().contentWindow; 
        contactReadPane.location.reload(); 
        contactReadPane2.ContactReadPaneLibrary.OnLoad(); 
    } 
} 
复制代码

 

我在account的form中添加了两个web resource,一个在更改form的primary contact的value以后需要刷新,另一个不需要刷新。

Form中的script调用web resource中的script,要使用对象Xrm.Page.getControl("WebResource_ContactReadPane").getObject().contentWindow。contactReadPane.location.reload()实现web resource的刷新,contactReadPane2.ContactReadPaneLibrary.OnLoad()是AJAX的实现,不需刷新,直接更改Full Name和Business Phone。

 

最后看一下效果图:

MS CRM 2011 Form与Web Resource在JScript中的相互调用_第3张图片

 

总结:web resource中的JScript需要用parent.window.Xrm来获得CRM Form中的Xrm对象,而CRM Form中的JScript需要使用Xrm.Page.getControl("WebResource_ContactReadPane").getObject().contentWindow对象来刷新web resource或调用web resource中的JScript函数。










本文转自JF Zhu博客园博客,原文链接: http://www.cnblogs.com/jfzhu/archive/2013/02/14/2912580.html  ,如需转载请自行联系原作者




你可能感兴趣的:(MS CRM 2011 Form与Web Resource在JScript中的相互调用)