Update a Plugin assembly without using the Plugin Registration Tool

Summary

It can get quite laborious using the Plugin Registration tool frequently when you are working on an assembly.

Here is a quick way to update an assembly in Dynamics 365 CE without using the Plugin registration tool.

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Tooling.Connector;
using System;
using System.IO;
using System.Net;

namespace Dynamics365.Connections
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            try
            {
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                string envcon = @"AuthType = AD; Url = http://127.0.0.1/crm/;Domain=eax360;Username=administrator;Password=*******";
                CrmServiceClient conn = new CrmServiceClient(envcon);

                IOrganizationService service;
                service = (IOrganizationService)conn.OrganizationWebProxyClient != null ? (IOrganizationService)conn.OrganizationWebProxyClient : (IOrganizationService)conn.OrganizationServiceProxy;

                Guid id = new Guid("a56462b4-d832-41f4-af5a-84ee0ca5266d"); // Use the query below to get the AAD

                const string assemblyPath = @"C:\Users\syed\source\repos\PlatformServices\ProjectOne\assembly.dll";

                DateTime now = DateTime.Now;

                // Update Assembly
                Entity entity = service.Retrieve("pluginassembly", id, new ColumnSet(true));

                entity["description"] = "Updated at: " + now;

                entity["content"] = Convert.ToBase64String(File.ReadAllBytes(assemblyPath));

                service.Update(entity);

                // To get the assembly ID

                //OrganizationServiceContext orgContext = new OrganizationServiceContext(service);
                //{
                //    var q = from p in orgContext.CreateQuery("pluginassembly")

                //            select new
                //            {
                //                name = p.GetAttributeValue<String>("name"),
                //                id = p.GetAttributeValue<Guid>("pluginassemblyid")
                //            };

                //    foreach (var results in q)
                //    {
                //        Console.WriteLine(results.name + " " + results.id);
                //    }

                //    orgContext.Dispose();
                //}
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
            }
        }
    }
}