Consuming external API in CRM plugin

Note: First we need external API to consume in our plugin.

External API:”http://hhkffuiuhhha-env.sz5p3bapha.us-east-1.elasticbeanstalk.com:8080/api/dataLog”


using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;


namespace Project
{
    public class account
    {
        public string name { get; set; }
        public string address { get; set; }


    }
    public class PluginClass1 : IPlugin
    {
        #region Secure/Unsecure Configuration Setup
        private string _secureConfig = null;
        private string _unsecureConfig = null;


        public PluginClass1(string unsecureConfig, string secureConfig)
        {
            _secureConfig = secureConfig;
            _unsecureConfig = unsecureConfig;
        }
        #endregion
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracer = (ITracingService)
serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(
typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)
serviceProvider.GetService(
typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.
CreateOrganizationService(context.UserId);


            try
            {
                string accname;
                
                string accaddress;
                Entity entity = (Entity)context.InputParameters[“Target”];
                if (context.MessageName == “Create” || context.MessageName == “Update”)


                {
                   
                    if (entity.LogicalName == “account”)
                    {
                        Guid currentrecordid = entity.Id;
                        //retriving all the columns for the present  record
                        Entity currentrecord = service.Retrieve(“account”, currentrecordid, new ColumnSet(“name”, “address1_country”));
                       accname = currentrecord.Attributes[“name”].ToString();
                       accaddress = currentrecord.Attributes[“address1_country”].ToString();
                        var account = new account();
                        account.name = accname;
                        account.address = accaddress;
                        if (entity.Attributes.Contains(“name”))
                            {
                            account.name = entity.Attributes[“name”].ToString();
                        }
                        if (entity.Attributes.Contains(“name”))
                        {
                            account.address = entity.Attributes[“address1_country”].ToString();
   }
   DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(account));
    MemoryStream memoryStream = new MemoryStream();
   serializer.WriteObject(memoryStream, account);
  var jsonObject = Encoding.Default.GetString(memoryStream.ToArray());
   WebClient client1 = new WebClient();
  client1.Headers[HttpRequestHeader.ContentType] = “application/json”;
 var serviceUrl = “http://hhkffuiuhhha-env.
sz5p3bapha.us-east-1.
elasticbeanstalk.com:8080/api/dataLog
“;
  string reply = client1.UploadString(serviceUrl, “POST”, jsonObject);
  string rp = reply;
                    }
                }
            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
    }
}

0 thoughts on “C# Plugins”

  1. Prescription Viagra Prix Xenical Amoxicillin Next Day Shipping viagra Doxycycline And Dosing With Cephalexin

Leave a Reply

Your email address will not be published. Required fields are marked *