Dynamics 365 服务端组织服务 CURD(方法)

一、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));
            }
        }   
    }
发布了21 篇原创文章 · 获赞 0 · 访问量 607

猜你喜欢

转载自blog.csdn.net/Stodger0216/article/details/103325691
今日推荐