【发布时间】:2017-11-28 15:44:43
【问题描述】:
我正在尝试使用 web-api 在 Dynamics 365/CRM 上创建一些记录。它在进行 GUID 检索时起作用。
在我的场景中,我应该通过 webservices 使用来自 azure 的 web api。当它被调用时,它应该查询实体 Lead Sources 并在实体 Lead 上设置 GUID。
获取查询结果时出现错误。
这是我的代码:
using System;
using System.Net;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
namespace Integration.Marketing_Activities_Creation
{
class Create
{
List<string> entityUris = new List<string>();
string LeadSource1Uri;
static void Main(string[] args)
{
Create.RunAsync().Wait();
}
public static async Task RunAsync()
{
String clientId = "0000000-0000-0000-0000-00000000";
String redirectUrl = "http://localhost";
String user = "new@organization.onmicrosoft.com";
String pass = "********";
String baseAddress = "https://crm-instance.api.crm.dynamics.com/api/data/";
String baseAddressFull = baseAddress + "v8.2/";
AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(
new Uri(baseAddress)).Result;
//List<string> entityUris = new List<string>();
String authorityUrl = ap.Authority;
String resourceUrl = ap.Resource;
AuthenticationContext authContext = new AuthenticationContext(authorityUrl, false);
UserCredential credentials = new UserCredential(user, pass);
AuthenticationResult result;
result = authContext.AcquireToken(resourceUrl, clientId, credentials);
// = authContext.AcquireToken(resource, clientId, new Uri(redirectUrl));
var token = result.AccessToken;
var client = new HttpClient();
client.BaseAddress = new Uri(baseAddressFull);
client.Timeout = new TimeSpan(0, 2, 0);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
/*** Create Leads ***/
string LeadName = "Lead first Name";
string LeadLastName = "Lead second Name";
string LeadEmail = "3webapi@lead1.com";
string LeadTopic = "WebApi Lead 3";
string LeadSource = "Contact Us";
HttpResponseMessage response;
string queryLeadSource;
queryLeadSource = "new_leadsources?$select=new_leadsourceid,new_name&$filter=new_name eq '" + LeadSource + "'";
string LSId=null;
response = await client.GetAsync(queryLeadSource,
HttpCompletionOption.ResponseContentRead);
if (response.IsSuccessStatusCode)
{
JObject lsRetreived = JsonConvert.DeserializeObject<JObject>(await
response.Content.ReadAsStringAsync());
LSId = lsRetreived["new_leadsourceid"].ToString(); /*** outgoing on this line i get an exception and the app crashes :( ***/
}
else
{
Console.WriteLine("Error retrieving tracking Lead Source ID!");
throw new CrmHttpResponseException(response.Content);
}
JObject newLead = new JObject();
newLead.Add("firstname", LeadName);
newLead.Add("lastname", LeadLastName);
newLead.Add("emailaddress1", LeadEmail);
newLead.Add("subject", LeadTopic);
newLead.Add("new_leadsource@odata.bind", "/new_leadsources("+ LSId + ")");
HttpResponseMessage responsePost = await client.PostAsJsonAsync("leads", newLead);
}
}
}
【问题讨论】:
-
你得到的异常是什么?什么是 InnerException,如果有的话?
-
嗨 Martijn,我收到此消息“对象引用未设置为对象的实例。”字符串,然后运行控制台展开 :D :( 当我在 LSId = lsRetreived["new_leadsourceid"].ToString(); 上检索 GUID 时,它是第 81 行的对象
-
这里也返回执行 static void Main(string[] args) { Create.RunAsync().Wait(); }
-
您的查询返回一个实体对象数组。因此
lsRetreived["new_leadsourceid"]返回null并且.ToString()抛出NullReferenceException。只需在行上设置一个断点并检查对象lsRetreived。 -
嗨,Henk,不,它不返回 null,它返回我发送的 LeadSource 变量的 GUID
标签: c# asp.net-web-api dynamics-crm