Dynamics 2016 Web API C# 创建产品
Dynamics 2016 Web API C# Create Products
我正在使用 Web Api 和 C# 将产品添加到我们的 Dynamics 2016 CRM。
这是代码 - returns 一个内部服务器错误。
我知道当我为联系人实体执行此操作时代码有效,所以这是传递给调用的内容的问题。
我不确定从这里去哪里,所以任何帮助都会很棒。
非常感谢!
using Newtonsoft.Json.Linq;
using System;
using System.Net;
using System.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace ProductsToCrm {
public partial class InsertProducts {
static void Main(string[] args) {
Task.WaitAll(Task.Run(async () => await Create_Products()));
}
static async Task Create_Products() {
Console.WriteLine("--Section Products started--");
try {
HttpClient httpClient = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential("uName", "uPassword", "uDomain") });
httpClient.BaseAddress = new Uri("http://this.orgname.org:port/orgname/api/data/v8.2/");
httpClient.Timeout = new TimeSpan(0, 1, 0);
httpClient.DefaultRequestHeaders.Add("User-Agent", "C# console program");
JObject jProduct = new JObject();
jProduct.Add("name", "testing_a_product1");
jProduct.Add("description", "I am testing");
string opportunity1Uri;
List<string> entityUris = new List<string>();
HttpRequestMessage createRequest1 =
new HttpRequestMessage(HttpMethod.Post, "products");
createRequest1.Content = new StringContent(jProduct.ToString(),
Encoding.UTF8, "application/json");
HttpResponseMessage createResponse1 =
await httpClient.SendAsync(createRequest1);
if(createResponse1.StatusCode == HttpStatusCode.NoContent) {
Console.WriteLine("Product '{0} {1}' created.", jProduct.GetValue("name"), jProduct.GetValue("description"));
opportunity1Uri = createResponse1.Headers.GetValues("OData-EntityId").FirstOrDefault();
entityUris.Add(opportunity1Uri);
Console.WriteLine("Created Entity URI: {0}", opportunity1Uri);
} else {
Console.WriteLine("Failed to create Entity for reason: {0} == {1} == {2}",
createResponse1.ReasonPhrase, createRequest1, "test");
}
} catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}
即使在 CRM UI 中,创建产品记录时也需要一些属性,例如名称、产品编号、defaultuomscheduleid(单位组)、defaultuomid(默认单位)和 quantitydecimal(支持小数)。您必须全部通过才能成功交易。
您可以使用 postman 或 CRM REST builder dev/test 目的。
创建产品的基本负载如下:Reference
{
"name": "new product",
"productnumber": "12345",
"defaultuomscheduleid@odata.bind": "/uomschedules(54692a01-4c22-45eb-a224-a157b9a6a0b3)",
"defaultuomid@odata.bind": "/uoms(d41f6ab2-6087-477b-a7f1-c4d1df10a57e)",
"quantitydecimal": 0
}
我正在使用 Web Api 和 C# 将产品添加到我们的 Dynamics 2016 CRM。
这是代码 - returns 一个内部服务器错误。 我知道当我为联系人实体执行此操作时代码有效,所以这是传递给调用的内容的问题。 我不确定从这里去哪里,所以任何帮助都会很棒。
非常感谢!
using Newtonsoft.Json.Linq;
using System;
using System.Net;
using System.Configuration;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace ProductsToCrm {
public partial class InsertProducts {
static void Main(string[] args) {
Task.WaitAll(Task.Run(async () => await Create_Products()));
}
static async Task Create_Products() {
Console.WriteLine("--Section Products started--");
try {
HttpClient httpClient = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential("uName", "uPassword", "uDomain") });
httpClient.BaseAddress = new Uri("http://this.orgname.org:port/orgname/api/data/v8.2/");
httpClient.Timeout = new TimeSpan(0, 1, 0);
httpClient.DefaultRequestHeaders.Add("User-Agent", "C# console program");
JObject jProduct = new JObject();
jProduct.Add("name", "testing_a_product1");
jProduct.Add("description", "I am testing");
string opportunity1Uri;
List<string> entityUris = new List<string>();
HttpRequestMessage createRequest1 =
new HttpRequestMessage(HttpMethod.Post, "products");
createRequest1.Content = new StringContent(jProduct.ToString(),
Encoding.UTF8, "application/json");
HttpResponseMessage createResponse1 =
await httpClient.SendAsync(createRequest1);
if(createResponse1.StatusCode == HttpStatusCode.NoContent) {
Console.WriteLine("Product '{0} {1}' created.", jProduct.GetValue("name"), jProduct.GetValue("description"));
opportunity1Uri = createResponse1.Headers.GetValues("OData-EntityId").FirstOrDefault();
entityUris.Add(opportunity1Uri);
Console.WriteLine("Created Entity URI: {0}", opportunity1Uri);
} else {
Console.WriteLine("Failed to create Entity for reason: {0} == {1} == {2}",
createResponse1.ReasonPhrase, createRequest1, "test");
}
} catch(Exception ex) {
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}
即使在 CRM UI 中,创建产品记录时也需要一些属性,例如名称、产品编号、defaultuomscheduleid(单位组)、defaultuomid(默认单位)和 quantitydecimal(支持小数)。您必须全部通过才能成功交易。
您可以使用 postman 或 CRM REST builder dev/test 目的。
创建产品的基本负载如下:Reference
{
"name": "new product",
"productnumber": "12345",
"defaultuomscheduleid@odata.bind": "/uomschedules(54692a01-4c22-45eb-a224-a157b9a6a0b3)",
"defaultuomid@odata.bind": "/uoms(d41f6ab2-6087-477b-a7f1-c4d1df10a57e)",
"quantitydecimal": 0
}