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;

            }

             // The InputParameters collection contains all the data passed in the message request.

            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

Popular posts from this blog

2. On Click of case Reject button, show popup with reject reason text, once filled and submitted, reject reason field should get filled and case should be in read only

8. On selection of asset in case entity, read the project in asset, project start date and end date from project entity, set the dates on case entity from as project start date and end dates

1. In Case Entity, based on Incident Type selection, filter the case category using the category-incident master entity