How to read JSON format data
How to read JSON format data:
Write a plugin code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using Newtonsoft.Json;
namespace Shared_variables
{
public class jsonData : IPlugin
{
public string fullName;
public string email;
public string rejectReason;
private string _secureConfiguration;
private string _unsecureConfiguration;
public jsonData(string unsecureConfiguration, string
secureConfiguration)
{
_secureConfiguration =
secureConfiguration;
_unsecureConfiguration =
unsecureConfiguration;
}
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service
provider.
IPluginExecutionContext context =
(IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
// Obtain the organization service reference.
IOrganizationServiceFactory factory
= (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// IOrganizationService service =
factory.CreateOrganizationService(context.UserId);
IOrganizationService service =
factory.CreateOrganizationService(context.InitiatingUserId);
ITracingService tracer =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
if
(context.Depth > 1)
{
return;
}
if
(context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{
try
{
Entity studentEntity =
(Entity)context.InputParameters["Target"];
var unsecureData =
JsonConvert.DeserializeObject<jsonData>(_unsecureConfiguration);
var secureData =
JsonConvert.DeserializeObject<jsonData>(_secureConfiguration);
studentEntity["crb67_fullname"] = unsecureData.fullName;
studentEntity["crb67_email"] = unsecureData.email;
studentEntity["crb67_rejectreason"] = secureData.rejectReason;
service.Update(studentEntity);
}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
}
Output:-
Comments
Post a Comment