博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Microsoft Dynamics CRM 4 将电话记录绑定在DataGrid上。

Posted on 2012-10-26 20:19  Hamilton Tan  阅读(298)  评论(3编辑  收藏  举报

C#代码:

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.Sdk.Utility;
using Microsoft.Crm.Sdk.Metadata;

using Frensworkz.CRM.XCMGCC.Web.Common;
using Microsoft.Crm.SdkTypeProxy.Metadata;

namespace Frensworkz.CRM.XCMGCC.Web.ISV.test
{
public partial class phoneCallList : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//绑定数据
string PhoneNumber = "13410632517";//注意:改值可以通过父窗口把值传过来。
if (PhoneNumber != string.Empty)
{
// gvPhoneCall.DataSource = GetAllPhoneCallByPhoneNo(PhoneNumber);
//gvPhoneCall.DataBind();
GetPhoneCallInfo(PhoneNumber);
}
}
}


#region 获取所有的电话信息
public void GetPhoneCallInfo(string teltephone)
{
var service = OrgService;

//排序
OrderExpression orders = new OrderExpression();
orders.AttributeName = "phonenumber";
orders.OrderType = OrderType.Ascending;

//获取集合信息
BusinessEntityCollection busers = Query.instance.QueryByAttributee(service, "phonecall", new string[] { "phonenumber" }, new object[] { teltephone }, new ArrayList { orders }, null, new string[] { "phonenumber", "new_company", "new_city", "new_type", "activityid", "from", "directioncode" });
var medatataservice = CrmServiceUtility.GetMetadataService("http://10.9.2.232:5555", "xcmg");//获取元数据服务

if (busers.BusinessEntities.Count >0)
{
foreach(var ite in busers.BusinessEntities)
{
DynamicEntity dynEntity = (DynamicEntity)ite;
if (dynEntity.Properties.Contains("from"))
{
//把from的CrmReference类型转换成Lookup类型
CrmReference from = (CrmReference)((DynamicEntity[])dynEntity.Properties["from"])[0].Properties["partyid"];
Lookup lookup = new Lookup();
lookup.name = from.name;
lookup.Value = from.Value;
dynEntity.Properties["from"] = lookup;
}
}
}


UniversalEntityList dalist = new UniversalEntityList(busers, medatataservice);
gvPhoneCall.DataSource = dalist;
gvPhoneCall.DataBind();

}
#endregion

#region 根据电话号码获取通话记录
//根据电话号码获取通话记录
public DataTable GetAllPhoneCallByPhoneNo(string phoneno)
{

//获取service服务
var service = OrgService;

//根据phonenumber升序
OrderExpression orderExpression = new OrderExpression();
orderExpression.AttributeName = "phonenumber";
orderExpression.OrderType = OrderType.Ascending;

BusinessEntityCollection bussinessEntityCollection = Query.instance.QueryByAttributee(service, "phonecall", new string[] { "phonenumber" }, new string[] { phoneno }, new ArrayList { orderExpression }, null, new string[] { "phonenumber", "new_company", "new_city", "new_type", "activityid", "from", "directioncode" });
DataTable dt = new DataTable();//实例化DataTable

//判断集合中有数据
if (bussinessEntityCollection.BusinessEntities.Count > 0)
{

//把字段添加到DataTabel上。
dt.Columns.Add("phonenumber");
dt.Columns.Add("new_company");
dt.Columns.Add("new_city");
dt.Columns.Add("new_type");
dt.Columns.Add("activityid");
dt.Columns.Add("from");
dt.Columns.Add("directioncode");
//定义并初始化变量
string phonenumbe = string.Empty;string company = string.Empty;Guid cityid = Guid.Empty;
string cityname = string.Empty;int typeid = 0;string typename = string.Empty;
Guid acid = Guid.Empty; Guid fromid = Guid.Empty; string fromname = string.Empty;
Boolean dnode = false;

#region
//循环所有的记录
foreach (var item in bussinessEntityCollection.BusinessEntities)
{
DataRow dr = dt.NewRow();
DynamicEntity dynamicEntity = (DynamicEntity)item;
if (dynamicEntity.Properties.Contains("phonenumber")) phonenumbe = dynamicEntity.Properties["phonenumber"].ToString();
if (dynamicEntity.Properties.Contains("new_company")) company = dynamicEntity.Properties["new_company"].ToString();
if (dynamicEntity.Properties.Contains("new_city")) cityid = (dynamicEntity.Properties["new_city"] as Lookup).Value;
if (dynamicEntity.Properties.Contains("new_city")) cityname = (dynamicEntity.Properties["new_city"] as Lookup).name;
if (dynamicEntity.Properties.Contains("new_type")) typeid = (dynamicEntity.Properties["new_type"] as Picklist).Value;
if (dynamicEntity.Properties.Contains("new_type")) typename = (dynamicEntity.Properties["new_type"] as Picklist).name;
if (dynamicEntity.Properties.Contains("activityid")) acid = ((Key)dynamicEntity.Properties["activityid"]).Value;
if (dynamicEntity.Properties.Contains("from")) fromname = ((CrmReference)(((DynamicEntity[])dynamicEntity.Properties["from"])[0].Properties["partyid"])).name;
if (dynamicEntity.Properties.Contains("from")) fromid = ((CrmReference)(((DynamicEntity[])dynamicEntity.Properties["from"])[0].Properties["partyid"])).Value;

 

dr["phonenumber"] = phonenumbe;
dr["new_company"] = company;
dr["new_city"] = cityname;
dr["new_type"] = typename;
dr["activityid"] = acid;
dr["from"] = fromname;
dr["directioncode"] = dnode;

dt.Rows.Add(dr);
}
#endregion
}
return dt;
}
#endregion

}
}

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="phoneCallList.aspx.cs" Inherits="Frensworkz.CRM.XCMGCC.Web.ISV.test.phoneCallList" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>PhoneCallList</title>
<link href="/ISV/test/style/contact.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:GridView ID="gvPhoneCall" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True">
<Columns>
<asp:TemplateField HeaderText="联系人">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("from.Value.name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="phonenumber" HeaderText="电话号码" />
<asp:BoundField DataField="new_company" HeaderText="公司" />
<asp:TemplateField HeaderText="城市">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("new_city.Value.name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:BoundField DataField="new_type" HeaderText="类型" />
<asp:BoundField DataField="directioncode" HeaderText="直接" />
<asp:HyperLinkField Text="录音" HeaderText="录音"
DataNavigateUrlFormatString="http://10.9.2.232:5555/xcmg/activities/phone/edit.aspx?id={0}"
DataNavigateUrlFields="activityid"/>
<asp:HyperLinkField Text="查看" HeaderText="查看" DataNavigateUrlFormatString="http://10.9.2.232:5555/xcmg/activities/phone/edit.aspx?id={0}" DataNavigateUrlFields="activityid"/>
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>

css:

 

table {
width:100%;
height:100%;
}
th {
background-color:#F0F0F0;
height:20px;
border-bottom:1px solid #999999;
border-top:1px solid #999999;
border-left:0px;
border-right:0px;
}
td {
border-bottom:1px solid #c4ddff;
text-align:center;
height:20px;
border-left:0px;
border-right:0px;
}