Dynamic CRM 之查询

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;
			}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值