仅使用WebAPI创建Dynamics 365实体记录



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

您正在查找此文档。正如您已经提到的,您可以使用web api读取记录,您可以在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);  
}  

此示例创建一个新的帐户实体。响应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)

若要创建新实体,必须标识有效的属性名称和类型。

最新更新