Dynamics CRM 2011通过客户端代码选择视图

在实施的过程中我们经常会遇到这样的场景,有个系统标准的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="<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>"+
"<entity name='product'>"+
"<attribute name='name' />"+
"<attribute name='new_cpxh' />"+
"<attribute name='new_productname' />"+
"<attribute name='producttypecode' />"+
"<attribute name='new_cplb' />"+
"<attribute name='new_sjcp' />"+
"<attribute name='productnumber' />"+
"<attribute name='productid' />"+
"<order attribute='name' descending='false' />"+
"<filter type='and'>"+
"<condition attribute='new_fl' operator='eq' value='100000000' />"+
"<condition attribute='new_productlevel' operator='eq' value='2' />"+
"</filter>"+
"</entity>"+
"</fetch>";
var layoutXml = "<grid name='resultset' " +
"object='1' " +
"jump='name' " +
"select='1' " +
"icon='1' " +
"preview='1'>" +
"<row name='result' " +
"id='productid'>" +
"<cell name='name' " +
"width='300' />" +
"<cell name='new_cplb' " +
"width='100' />" +
"<cell name='productnumber' " +
"width='100' />" +
"<cell name='new_cpxh' " +
"width='150' />" +
"<cell name='new_sjcp' " +
"width='150' />" +
"</row>" +
"</grid>";
Xrm.Page.getControl("productid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml,false);
Xrm.Page.getControl("productid").setDefaultView(viewId);
}

使用SDK中的方法是我喜欢的方式(之前碰到过使用非标准的方法结果在Rollup的时候出现BUG的情况),不过这种方法稍微麻烦一点,FetchXml可以通过高级查找直接得到,而LayoutXml通常要手动去拼凑(估计在solution或者数据库View表中有办法获取,只是没去尝试了),但是还是建议用这种方法

原文地址:https://www.cnblogs.com/liaochifei/p/3173700.html