2014-02-14 51 views
6

我試圖從已插入到CRM 2011中的窗體的HTML Web資源中訪問Xrm.Page.data對象。但是,關於如何嘗試訪問Xrm實體,我發現它未定義或者Xrm.Page.data爲空。對於網絡資源的代碼如下:無法從CRM 2011中的HTML Web資源中訪問Xrm.Page.data

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<script type="text/javascript"> 

function OpenMyApp(e){ 
    alert('Xrm defined: ' + (typeof Xrm != 'undefined')); 
     // The line above returns the string 'Xrm defined: false' 

    alert('window.top.opener.parent.Xrm defined: ' + (typeof window.top.opener.parent.Xrm != 'undefined')); 
     // The line above returns the string 'window.top.opener.parent.Xrm defined: true' 


    alert('frames[0].Xrm defined: ' + (typeof frames[0].Xrm != 'undefined')); 
     // the line above will actually throw an error and stop the script, because the frames collection is empty. 

    alert(window.top.opener.parent.Xrm.Page.data); 
     // the line above returns null. 

    // var myId = Xrm.Page.data.entity.attributes.get("new_field_i_want").getValue(); 
     // The line above is what I would like to see work. 

    e.preventDefault(); 
} 
</script> 

</head> 
<body> 
<a onClick="OpenMyApp(event);" href="#">My Link</a> 
</body> 
</html> 

我已經從一個JavaScript函數,該函數在一個窗體事件觸發(如表一庫的一部分內訪問Xrm.Page.data成功。加載)。只是當它嵌入在我遇到這個問題的窗體上的HTML Web資源中時。任何人都可以解釋我做錯了什麼,如果真的有辦法以這種方式訪問​​Xrm.Page.data我想要做什麼?

謝謝。

回答

13

嘗試使用以下語法訪問XRM:

window.parent.Xrm.Page.getAttribute()... 

window.parent.Xrm.Page.getControl()... 

window.parent.Xrm.Page.context... 

alert(window.parent.Xrm.Page.data.entity.attributes.get("new_field_i_want").getValue()); 

從你的示例代碼。

+0

另外窩rks for Mscrm.CrmUri – GPGVM

+0

我有同樣的問題。我在Chrome 37中運行這個。我有一個web資源在實體表單的IFrame中顯示。我嘗試了所有組合,但仍無法使用Xrm對象。幫助將不勝感激! –

2

這適用於在iframe/dialog中加載了網頁資源的情況。它得到進入父框架,然後查找所有可用的框架,並檢查這架具有

Xrm.Page.data!= NULL

代碼...

$.each(parent.window.frames, function(i,val){ 
    if (parent.window.frames[i].Xrm.Page.data != null) { 
      parent.window.frames[i].Xrm.Page.data.entity.attributes.get('ownerid').setValue([{ id: '{' + sourceKey + '}', name: name, entityType: "systemuser" }]); 
      parent.window.frames[i].Xrm.Page.data.entity.save(); 
      break; 
    } 
}); 
+2

'break'對'$ .each'不起作用 –

1

基於TWilly的回答,我創建了下面的函數來檢索Xrm對象

GetXrm: function() { 

    var frame = $.grep(parent.window.frames, function (e) { 
     if (e.Xrm.Page.data) 
      return true; 
    }); 

    return frame[0].Xrm; 
} 
相關問題