一、Create
private Guid CreateUser()
{
// 获取服务
ServiceHelper helper = new ServiceHelper();
OrganizationServiceProxy proxy = helper.GetOrganizationServiceProxy();
// 定义实体字段内容
User entity = new User();
entity.Id = Guid.NewGuid();
entity.Name = "张三";
entity.Address = "北京市";
// 创建数据
return proxy.Create(entity);
}
二、Update
private void UpdateUser()
{
// 获取服务
ServiceHelper helper = new ServiceHelper();
OrganizationServiceProxy proxy = helper.GetOrganizationServiceProxy();
//获取数据
List<UserResultEntity> userResults = RetriveByExpression("张三");
if(userResults.Count > 0)
{
foreach (var userResult in userResults)
{
User user= new User();
user.Id = new Guid(userResult.id);
user.Address = "上海市";
proxy.Update(user);
}
}
}
三、Find
private UserResultEntity FindUserById(Guid id)
{
try
{
// 获取服务
ServiceHelper helper = new ServiceHelper();
OrganizationServiceProxy proxy = helper.GetOrganizationServiceProxy();
// 查询单笔数据
Microsoft.Xrm.Sdk.Query.ColumnSet columnList = new Microsoft.Xrm.Sdk.Query.ColumnSet(new string[] { "name"});
User entity =(User)proxy.Retrieve(User.EntityLogicalName, id, columnList);
return new UserResultEntity ()
{
id = entity.Id.ToString(),
name = entity.Name,
};
}
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
{
return null;
}
}
四、Delete
private void DeleteUser()
{
//获取服务
ServiceHelper helper = new ServiceHelper();
OrganizationServiceProxy proxy = helper.GetOrganizationServiceProxy();
//获取数据
List<UserResultEntity> userResults = RetriveByExpression("张三");
if (userResults.Count > 0)
{
foreach (var userResult in userResults)
{
proxy.Delete(User.EntityLogicalName, new Guid(userResult.id));
}
}
}