Overview
Question also posted on SO. I am on Dynamics 2016 on premise.
I have a Business Process Flow in Microsoft Dynamics to handle creation of a new client. When the process finishes, I am attaching a workflow that kicks off an action which calls a plugin to do some custom processing. I am following this article to set up this process. When this plugin is triggered in Dynamics, it appears to run successfully, but does not create the records I am expecting it to create. However, when I profile the plugin and run it through the Plugin Registration Tool/Visual Studio the records are created that I expect to see.
When I added trace logs to my plugin, I noticed that it appears I am receiving a 401 - Unauthorized
error from Dynamics when I call the Web API. When I looked at the network traffic while debugging it looks like I receive an initial 401
error, but then a retry successfully gets the records I am trying to get.
Code
This is the section of code I think the 401-Unauthorized
error is coming from
private void GetGlobalFieldNameMapsAsync(String url = "ccseq_globalfieldnamemaps?$select=ccseq_system,ccseq_entitytype,ccseq_fieldtype,ccseq_name,ccseq_datatype")
{
HttpClient client = new HttpClient(new HttpClientHandler() { Credentials = new NetworkCredential("admin", "password", "domain") });
HttpResponseMessage responseMessage = new HttpResponseMessage();
try
{
client.BaseAddress = new Uri(Helpers.GetSystemUrl(APIConnector.Application.Dynamics));
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
client.DefaultRequestHeaders.Add("OData-Version", "4.0");
responseMessage = client.GetAsync(url).Result;
JObject responseData = JObject.Parse(responseMessage.Content.ReadAsStringAsync().Result);
if (responseData["@odata.nextLink"] != null)
{
GetGlobalFieldNameMapsAsync(Convert.ToString(responseData["@odata.nextLink"]));
}
foreach (var v in responseData["value"])
{
GlobalFieldNameMap newGFNM = new GlobalFieldNameMap();
newGFNM.System.Value = Convert.ToInt32(v["ccseq_system"]);
newGFNM.EntityType.Value = Convert.ToInt32(v["ccseq_entitytype"]);
newGFNM.FieldType.Value = Convert.ToInt32(v["ccseq_fieldtype"]);
newGFNM.FieldName.Value = Convert.ToString(v["ccseq_name"]);
newGFNM.DataType.Value = Convert.ToInt32(v["ccseq_datatype"]);
FieldNameMap.Add(newGFNM);
}
}
catch (Exception e)
{
throw new Exception(e.Message + " - " + responseMessage.Content.ReadAsStringAsync().Result);
}
}
Network Traffic
I can't seem to puzzle out how I am seeing the plugin successfully run during debugging, but seeing it fail when running directly in dynamics. The plugin appears to be running under the same user regardless of whether it's running in Dynamics or on my local machine. I'm passing in admin credentials so they should have full permissions to do anything in the system. Any thoughts?
I have asked this question and this question which may be related, but don't quite cover this issue.
#CRM2016 #Technical #Advanced #CustomerEngagement
------------------------------
Tim Hutchison
Business Analyst/Developer
Cohen & Company
Cleveland OH
------------------------------