Customer Engagement & Dynamics CRM Forum

 View Only
Expand all | Collapse all

Dynamics C# Plugin fails when run, but succeeds when Debugging

  • 1.  Dynamics C# Plugin fails when run, but succeeds when Debugging

    Posted Aug 08, 2018 07:54 PM


    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 - Unauthorizederror 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.


    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

  • 2.  RE: Dynamics C# Plugin fails when run, but succeeds when Debugging

    Posted Aug 09, 2018 08:48 AM


    Make sure under which account the plugin is running under. The fact that you are getting a 401 indicates that the plugin ran under an unauthorized account which is different from yours. Although the second try success is a mystery to me.


    The plugin registration tool will always run under the account that you connect to Crm with.


    Rex Kenley Tan, MCP
    Kent OH

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.

If you've found this thread useful, dive deeper into User Group community content by role