1.CRM连接的方法:
2.CRM之查询:
public void InitCRMWebService(ref OrganizationServiceProxy service)
{
try
{
string url, domain, username, pass;
url = ConfigurationManager.AppSettings["CRMURL"]; //CRM网址
domain = ConfigurationManager.AppSettings["Domain"]; //域名
username = ConfigurationManager.AppSettings["UserName"]; //账户名
pass = ConfigurationManager.AppSettings["PassWord"]; //密码
Uri orgServiceUri = new Uri(url);
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new System.Net.NetworkCredential(username, pass, domain);
OrganizationServiceProxy _service = new OrganizationServiceProxy(orgServiceUri, null, credentials, null);
// This statement is required to enable early-bound type support.
_service.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
service = _service;
}
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
{
}
}
public string FindMember(string phone)
{
string ReturnStr = "";
try
{
OrganizationServiceProxy service = null;//初始化CRM服务
OrganizationServiceProxy.service =InitCRMWebService(ref service); //配置连接crm的数据
QueryExpression ContactExpression = new QueryExpression
{
EntityName = "contact", //查询的实体名称
ColumnSet = new ColumnSet("fullname", "gendercode", "mobilephone"), //姓名,性别,手机号码,
Criteria = new FilterExpression()
};
ConditionExpression telExpression1 = new ConditionExpression("mobilephone", ConditionOperator.Like, phone); //查询条件
FilterExpression childFilder = new FilterExpression();
childFilder.FilterOperator = LogicalOperator.Or;
childFilder.AddCondition(telExpression1);
//实例化list集合
List<FindMember> FMList = new List<FindMember>();
FindMember FM = new Model.FindMember();
foreach (Entity s in contactchildEntity.Entities)
{
Guid new_contactID = s.Id;
Entity new_contactEntity = new Entity("contact");
new_contactEntity.Id = s.Id;
//对姓名进行判断
string fullname = (s["fullname"] != null && s["fullname"].ToString() != "") ? s["fullname"].ToString() : "";
int gendercode = 0;
//查询实体中的该字段属性是否为空,如果为空,则给该字段赋值(如果不对此进行判断,则会报错"给定关键字不在字典中")
if (s.Attributes.Keys.Contains("gendercode") !=true)
{
gendercode = 1; //性别默认为1;
}
else
{
gendercode = ((OptionSetValue)s["gendercode"]).Value; //获取数据库中的性别
}
string mobilephone = (s["mobilephone"] != null && s["mobilephone"].ToString() != "") ? s["mobilephone"].ToString() : ""; //手机号
FM.fullname = fullname;
FM.phone = mobilephone;
FM.sex = gendercode;
}
FMList.Add(FM);
ReturnStr = "{\"status\":\"success\",\"result\":" + JsonConvert.SerializeObject(FMList) + "}";
}
catch (Exception ex)
{
ReturnStr = "{\"status\":\"error\",\"result\":" + ex.Message + "}";
}
return ReturnStr;
}