【问题标题】:Creating Dynamics 365 entity record using WebAPI only仅使用 WebAPI 创建 Dynamics 365 实体记录
【发布时间】:2018-12-17 09:26:31
【问题描述】:

我已使用 C# 和 Azure 连接到我的 CRM。我的要求是我需要使用 WebAPI only 创建实体记录。在早期版本中,我使用过 IOrganization 服务,它运行良好。现在,我需要切换到 WebAPI。 我能够使用 webapi 读取记录,但不知道如何创建记录。 我尝试在网上搜索,但找不到任何相关的文章/教程。 任何帮助,将不胜感激。 提前致谢。

【问题讨论】:

  • 顺便说一句,如果你想为 JavaScript 构建 WebAPI 调用,Jason Lattimer 的CRMRestBuilder 工具是必不可少的。

标签: c# asp.net-web-api dynamics-crm dynamics-365 dynamics-crm-webapi


【解决方案1】:

This documentation 是您正在寻找的。正如您已经提到的,您可以使用 web api 读取记录,您可以使用下面的 sn-p 在 C# 中使用 web api 创建新的联系人记录。

JObject contact1 = new JObject();   
contact1.Add("firstname", "Peter");  
contact1.Add("lastname", "Cambel");  

HttpRequestMessage createRequest1 = new HttpRequestMessage(HttpMethod.Post, https://xyz.crm.dynamics.com/api/data/v9.0/contacts");  
createRequest1.Content = new StringContent(contact1.ToString(), Encoding.UTF8, "application/json");  

HttpResponseMessage createResponse1 = await httpClient.SendAsync(createRequest1);  

if (createResponse1.StatusCode == HttpStatusCode.NoContent)  //204  
{  
    Console.WriteLine("Contact '{0} {1}' created.", contact1.GetValue("firstname"), contact1.GetValue("lastname"));  
    contact1Uri = createResponse1.Headers.GetValues("OData-EntityId").FirstOrDefault();  
    entityUris.Add(contact1Uri);  
    Console.WriteLine("Contact URI: {0}", contact1Uri);  
}  
else  
{  
    Console.WriteLine("Failed to create contact for reason: {0}", createResponse1.ReasonPhrase);  
    throw new CrmHttpResponseException(createResponse1.Content);  
}  

【讨论】:

    【解决方案2】:

    此示例创建一个新的帐户实体。响应 OData-EntityId 标头包含已创建实体的 Uri

    POST [Organization URI]/api/data/v9.0/accounts HTTP/1.1
    Content-Type: application/json; charset=utf-8
    OData-MaxVersion: 4.0
    OData-Version: 4.0
    Accept: application/json
    
    {
    "name": "Sample Account",
    "creditonhold": false,
    "address1_latitude": 47.639583,
    "description": "This is the description of the sample account",
    "revenue": 5000000,
    "accountcategorycode": 1
    }
    

    回应

    HTTP/1.1 204 No Content
     OData-Version: 4.0
     OData-EntityId: [Organization URI]/api/data/v9.0/accounts(7eb682f1-ca75-e511-80d4- 
     00155d2a68d1)
    

    要创建新实体,您必须确定有效的属性名称和类型。

    【讨论】:

    • 感谢您的快速响应,但我在哪里执行此操作?我需要使用 C# 来创建实体。抱歉,我是这方面的初学者。
    • 这是 c# 代码,亲爱的,您只需在 json 文件上方创建并发送请求 throw api
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2019-02-17
    • 2021-03-07
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多