在实施的过程中我们经常会遇到这样的场景,有个系统标准的Lookup字段对应的不是一种Entity,如很多地方的客户实际上可选account或者contact,有的地方可选systemuser或者team,而客户或者实际情况要求只能选择一种Entity,这时我们可以将Lookup字段的视图锁定或者默认为某一个视图。
常用的方法有两种,第一种比较简单,但是不是SDK支持的方法,很可能在下一版本就不支持了;第二种方法是SDK中提供的标准方法
方法一:通过Dom设置视图
var contactList = document.getElementById("optionalattendees");
contactList.setAttribute("lookuptypes", "8");
contactList.setAttribute("defaulttype", "8");
Xrm.Page.getControl("optionalattendees").setDefaultView("{E88CA999-0B16-4AE9-B6A9-9EDC840D42D8}");
方法二:通过addCustomView
controlObj.addCustomView( viewId , entityName , viewDisplayName , fetchXml , layoutXml , isDefault ) |
function setCustomViewForQuoteProduct(){
var viewId="{597E9C69-9E45-E211-A8C9-00155D095101}";
var entityName = "product";
var viewDisplayName = "整机查询视图";
var fetchXml="
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
var layoutXml = "
"jump='name' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"
"
"
"
"
"
"
"
Xrm.Page.getControl("productid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml,false);
Xrm.Page.getControl("productid").setDefaultView(viewId);
}
使用SDK中的方法是我喜欢的方式(之前碰到过使用非标准的方法结果在Rollup的时候出现BUG的情况),不过这种方法稍微麻烦一点,FetchXml可以通过高级查找直接得到,而LayoutXml通常要手动去拼凑(估计在solution或者数据库View表中有办法获取,只是没去尝试了),但是还是建议用这种方法