I'm trying to create some records on Dynamics 365/CRM using web-api. It works when doing GUID retrieves.
In my scenario I should use the web api from azure via webservices. Wdhen it is invoked it should query entity Lead Sources and set the GUID on entity Lead.
The error occurs when getting the query's result.
This is my code:
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 = "[email protected]";
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 = "[email protected]";
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("[email protected]", "/new_leadsources("+ LSId + ")");
HttpResponseMessage responsePost = await client.PostAsJsonAsync("leads", newLead);
}
}
}
lsRetreived["new_leadsourceid"]
returnsnull
and.ToString()
throws aNullReferenceException
. Just set a breakpoint on the line and inspect objectlsRetreived
. – Henk van Boeijen