Working with JSON objects in Dynamics CRM Plugins

By | October 7, 2016

While working with Dynamics CRM plugin, we encountered a scenario where we were calling a Web API, which returns data in JSON format.

Normally .NET framework provides many methods to use JSON in C# code, many third party solutions such as Newtonsoft.Json library are available. Mostly we use JavaScriptSerializer to read JSON data.

However, when we register Plugin inside Sandbox, and if we try to use javascriptSerializer method in plugin, it will throw security exception error as show below:

The error message we get:

Unhandled Exception: Microsoft.Crm.CrmException: Unexpected exception from plug-in (Execute): XXXXXXXX.CRM2015.WorkflowActivities.XXXXXXXX: System.MethodAccessException: Attempt by security transparent method ‘XXXXXXXX.CRM2015.WorkflowActivities.XXXXXXXX.SetLocationInfo(Microsoft.Xrm.Sdk.IOrganizationService, Microsoft.Xrm.Sdk.ITracingService, System.String)’ to access security critical method ‘System.Web.Script.Serialization.JavaScriptSerializer..ctor()’ failed.

Assembly ‘XXXXXXXX.CRM2015.WorkflowActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3f9fc15734725b08′ is partially trusted, which causes the CLR to make it entirely security transparent regardless of any transparency annotations in the assembly itself.  In order to access security critical code, this assembly must be fully trusted.

Assembly ‘System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35′ is a conditionally APTCA assembly which is not enabled in the current AppDomain.  To enable this assembly to be used by partial trust or security transparent code, please add assembly name ‘System.Web.Extensions, PublicKey=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9′ to the the PartialTrustVisibleAssemblies list when creating the AppDomain.
at Microsoft.Crm.Sandbox.SandboxCodeUnit.Execute(IExecutionContext context)
at Microsoft.Crm.Workflow.Services.ProxyCustomActivity.Execute(CodeActivityContext executionContext)

This error occurs since we cannot access third party dll’s in CRM context.

To resolve this, we can use either ILMerge(utility for merging multiple .NET assemblies into a single one).

However there is an alternate way to get it done without using third party solution, by using DataContractJsonSerializer in CRM plugin.

Below code will demonstrate you how to use DataContractJsonSerializer in CRM plugin.

To use JSON in CRM plugin

  1. Create a DataContract class that will define datamodel for JSON data that we receive from Web API.
  • Deserialize Data obtained in JSON string to an object

  • Serialize Data in JSON string

Hope this will help you in using JSON data in Dynamics CRM plugin!

Completed with Dynamics CRM implementation? What’s next? Monitor User Adoption in Dynamics CRM. Try User Adoption Monitor.

Leave a Reply