CRM 2011 根据当前视图抓取网格里面所有的数据
1.在对应实体的HomePage新增按钮土地地图,调用landinfomap.JS(上传到系统WEB资源中)
2. New_landdata.JS中关键代码
//取经过筛选之后视图的FETCHXML,并保存在实体new_tempdata中(因XML过长,URL传值受到限制,目前只找到此方法),然后把保存的记录ID传参到LandDataMap.aspx页面(需另外做个批量删除的作业,来删除实体new_tempdata不需要的数据,可在数据管理—>> 批量删除记录里面创建)
function landinfomap(IDArr) { var fetchxml = IDArr.GetParameter("effectiveFetchXml"); fetchxml = fetchxml.substring(fetchxml.indexOf(">") + 1, fetchxml.length) //去掉XML中的第一句 fetchxml = "<fetch mapping=\"logical\" version=\"1.0\">" + fetchxml; var tempdata = new Object(); tempdata.new_fetchxml = fetchxml; var jsontempdata = JSON.stringify(tempdata); var createtempdata = CreateEntityRecordByISV("new_tempdata", false); //同步 createtempdata.send(jsontempdata); if (createtempdata.status == 201) { var newTempdata = JSON.parse(createtempdata.responseText).d; var value = new Array(); value[0] = new Object(); value[0].id = newTempdata.new_tempdataId; value[0].typename = "new_tempdata"; } else { alert("发生错误: " + JSON.parse(createphonecallReq.responseText).error.message.value); } var url = "../../ISV/LandDataMap.aspx?"; var someValue = window.showModalDialog(url + "landid=" + newTempdata.new_tempdataId, "", "dialogWidth=800px;dialogHeight=600px;status=no;help=no;scrollbars=no"); } //创建实体信息 function CreateEntityRecordByISV(entityschema, mode) { if (typeof (mode) == "undefined") { mode = false; } var serverUrl = Xrm.Page.context.getClientUrl(); var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + entityschema + "Set"; var createEntityReq = new XMLHttpRequest(); createEntityReq.open("POST", ODataPath, mode); createEntityReq.setRequestHeader("Accept", "application/json"); createEntityReq.setRequestHeader("Content-Type", "application/json; charset=utf-8"); return createEntityReq; }
3 LandDataMap.JS
接收landinfomap.JS传过来的参数,并通过LandDataInfo.ashx去取数据
//取土地 function GetLandInfo() { //jquery取数据 $.ajax({ type: "post", contentType: "application/json", url: "_common/LandDataInfo.ashx?serverUrl=" + encodeURIComponent(serverUrl) + "&landid=" + encodeURIComponent(landid), data: "", dataType: '', success: function (retstr) { var cname; var cid; var clng; var clat; var makeInfo; var regArr = retstr.split(';'); markerArr = new Array(); map = new BMap.Map("divMap"); map.centerAndZoom(new BMap.Point(regArr[2], regArr[3]), 14); // 初始化地图,设置中心点坐标和地图级别 (经度,纬度) map.addControl(new BMap.NavigationControl()); // 添加平移缩放控件 map.addControl(new BMap.OverviewMapControl()); //添加缩略地图控件 map.enableScrollWheelZoom(); //启用滚轮放大缩小 map.addControl(new BMap.MapTypeControl({ mapTypes: [BMAP_NORMAL_MAP, BMAP_HYBRID_MAP] })); //添加地图类型控件 for (var i = 0; i < regArr.length; i++) { cname = regArr[i]; i++; cid = regArr[i]; i++; clng = regArr[i]; i++; clat = regArr[i]; if (clng != "" && clat != "") { //加载地图标注 makeInfo = new Object(); makeInfo.title = cname; makeInfo.content = cname; makeInfo.lng = clng; makeInfo.lat = clat; makeInfo.isOpen = 0; makeInfo.ID = cid; if (rtype == "C") makeInfo.icon = { DType: "C", w: 60, h: 40, l: 46, t: 21, x: 9, lb: 12 }; markerArr.push(makeInfo); } } map.clearOverlays(); addMarker(); } }); }
4. LandDataInfo.ashx
根据传过来的ID,先从new_tempdate取出保存的XML文件。因我这边需取经纬度,在原视图没有该列,所以我执行取出来的XML拿到GUID,然后重新再取了一次经纬度. CreateXml方法是采用分页 Cookie 对 FetchXML 查询的结果进行分页(因Fetchxml查询不能超过5千条,采用此方法解决)
public void ProcessRequest(HttpContext context) { string serverUrl = DNTRequest.GetQueryString("serverUrl"); string landid = DNTRequest.GetQueryString("landid"); serverUrl = serverUrl + "/XRMServices/2011/Organization.svc"; CRMOrg crmOrg = new CRMOrg(); OrganizationServiceProxy orgSvc = crmOrg.GetOrganization(serverUrl); string retstr = ""; retstr = GetLandInfo(orgSvc, landid); retstr = retstr.Replace("{", ""); retstr = retstr.Replace("}", ""); retstr = retstr.Replace("null", ""); context.Response.Write(retstr); } //取土地信息 private string GetLandInfo(OrganizationServiceProxy orgSvc, string landid) { string retstr = ""; string fetchxml= ""; string landdataid; string landaddress; string lat; //纬度 string lng; #region //取存储的xml文档 string tempXml = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='new_tempdata'> <attribute name='new_fetchxml' /> <filter type='and'> <condition attribute='new_tempdataid' operator='eq' value='{0}'/> </filter> </entity> </fetch>", new Guid(landid)); EntityCollection tempdataRetrieved = null; tempdataRetrieved = orgSvc.RetrieveMultiple(new FetchExpression(tempXml)); foreach (var tempdataResult in tempdataRetrieved.Entities) { fetchxml= (tempdataResult.Contains("new_fetchxml")) ? tempdataResult.Attributes["new_fetchxml"].ToString() : ""; } #endregion int fetchCount = 300; //定义每页查询的数目 int pageNumber = 1; //起始页 string pagingCookie = null; while (true) { string xml = CreateXml(fetchxml, pagingCookie, pageNumber, fetchCount); EntityCollection returnCollection = orgSvc.RetrieveMultiple(new Microsoft.Xrm.Sdk.Query.FetchExpression(xml)); foreach (var entity in returnCollection.Entities) { landdataid = entity.Attributes.Contains("new_landdataid") ? (entity.Attributes["new_landdataid"]).ToString() : ""; #region //取经纬度 string competeXml = string.Format(@"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'> <entity name='new_landdata'> <attribute name='new_landdataid' /> <attribute name='new_landaddress' /> <attribute name='new_lat' /> <attribute name='new_lng' /> <order attribute='new_lat' descending='false' /> <filter type='and'> <condition attribute='statecode' operator='eq' value='0' /> <condition attribute='new_landdataid' operator='eq' value='{0}'/> <condition attribute='new_lng' operator='not-null' /> <condition attribute='new_lat' operator='not-null' /> </filter> </entity> </fetch>", new Guid(landdataid)); EntityCollection landRetrieved = null; landRetrieved = orgSvc.RetrieveMultiple(new FetchExpression(competeXml)); foreach (var landResult in landRetrieved.Entities) { landaddress = (landResult.Contains("new_landaddress")) ? landResult.Attributes["new_landaddress"].ToString() : ""; lat = (landResult.Contains("new_lat")) ? landResult.Attributes["new_lat"].ToString() : ""; lng = (landResult.Contains("new_lng")) ? landResult.Attributes["new_lng"].ToString() : ""; retstr += landaddress + ";" + landdataid + ";" + lng + ";" + lat + ";"; } #endregion } if (returnCollection.MoreRecords) { pageNumber++; } else { break; } } retstr = retstr.TrimEnd(';'); return retstr; } public string CreateXml(string xml, string cookie, int page, int count) { StringReader stringReader = new StringReader(xml); XmlTextReader reader = new XmlTextReader(stringReader); // Load document XmlDocument doc = new XmlDocument(); doc.Load(reader); return CreateXml(doc, cookie, page, count); } public string CreateXml(XmlDocument doc, string cookie, int page, int count) { XmlAttributeCollection attrs = doc.DocumentElement.Attributes; if (cookie != null) { XmlAttribute pagingAttr = doc.CreateAttribute("paging-cookie"); pagingAttr.Value = cookie; attrs.Append(pagingAttr); } XmlAttribute pageAttr = doc.CreateAttribute("page"); pageAttr.Value = System.Convert.ToString(page); attrs.Append(pageAttr); XmlAttribute countAttr = doc.CreateAttribute("count"); countAttr.Value = System.Convert.ToString(count); attrs.Append(countAttr); StringBuilder sb = new StringBuilder(1024); StringWriter stringWriter = new StringWriter(sb); XmlTextWriter writer = new XmlTextWriter(stringWriter); doc.WriteTo(writer); writer.Close(); return sb.ToString(); }