Overriding the RetrieveMultiple Integration Pattern Updated for Unified Client Interface

2 Comments

In a previous post in 2017, we looked at how to override the RetrieveMultiple through a plugin to display data in Dynamics 365 / Power Apps. We will now revisit this post to show how to use the same pattern when dealing with Unified Client Interface apps, where the old code didn’t work. This will be useful if you’re running the old code and are migrating to unified interface apps.

Let’s look at the same example as before. We would like to integrate our Retail Sales database with our Accounts in Dynamics 365. We would like to display products that our accounts have ordered. Instead of using out of the box sales entities, we will create our own entity called Retail Sales:

Go to https://make.powerapps.com/ and create a New Entity:

We will call the entity Retail Sale:

Let’s add 2 fields – Product and Amount, and then add a relationship with Account:

And add a relationship to the Account entity:

Change the view, we will use this on the subgrid:

We will display the fields Product and Amount:

On the Account Main form, we will add a Subgrid to display the Retail Sales:

Save and Publish the changes.

Now in Visual Studio let’s create a new plugin.

Grab the Microsoft.CrmSdk.CoreAssemblies:

First, the old code which doesn’t work (if you can’t wait, scroll down for the code that does work):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace Carl.OverrideRMUCI
{
    public class GetRetailSales : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Here we will simulate connecting to a 3rd party environment

            #region simulate3rdparty
            // Create a list of Retail Sales that would exist in a database in another system
            var retailSales = new List<RetailSale>();
            var retailSales1 = new RetailSale
            {
                Product = "Product1",
                Amount = 1
            };
            var retailSales2 = new RetailSale
            {
                Product = "Product2",
                Amount = 5
            };
            var retailSales3 = new RetailSale
            {
                Product = "Product3",
                Amount = 10
            };

            retailSales.Add(retailSales1);
            retailSales.Add(retailSales2);
            retailSales.Add(retailSales3);
            #endregion

            // Plugin Code
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            EntityCollection entityCollection = new EntityCollection();
            entityCollection.EntityName = "crc07_retailsale";

            foreach (var retailSale in retailSales)
            {
                Entity crmRetailSale = new Entity("crc07_retailsale");
                crmRetailSale.Attributes.Add("crc07_product", retailSale.Product);
                crmRetailSale.Attributes.Add("crc07_amount", retailSale.Amount);
                entityCollection.Entities.Add(crmRetailSale);
            }

            context.OutputParameters["BusinessEntityCollection"] = entityCollection;
        }
    }
    public class RetailSale
    {
        public string Product { get; set; }
        public Int32 Amount { get; set; }
    }
}

Sign the assembly, and compile, then in the plugin registration tool register the assembly and add the step below on RetrieveMultiple of the new entity:

Now if we run this by selecting an Account record in Dynamics 365, we see there is no data available in our subgrid:

Back to our code. Under Fields, get the Id field of the new entiyt:

The new code (that works with UCI). We’re adding the line to set the crc07_retailsaleid field:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;

namespace Carl.OverrideRMUCI
{
    public class GetRetailSales : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Here we will simulate connecting to a 3rd party environment

            #region simulate3rdparty
            // Create a list of Retail Sales that would exist in a database in another system
            var retailSales = new List<RetailSale>();
            var retailSales1 = new RetailSale
            {
                Product = "Product1",
                Amount = 1
            };
            var retailSales2 = new RetailSale
            {
                Product = "Product2",
                Amount = 5
            };
            var retailSales3 = new RetailSale
            {
                Product = "Product3",
                Amount = 10
            };

            retailSales.Add(retailSales1);
            retailSales.Add(retailSales2);
            retailSales.Add(retailSales3);
            #endregion

            // Plugin Code
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

            EntityCollection entityCollection = new EntityCollection();
            entityCollection.EntityName = "crc07_retailsale";

            foreach (var retailSale in retailSales)
            {
                Guid guid = new Guid();
                guid = Guid.NewGuid();

                Entity crmRetailSale = new Entity("crc07_retailsale");
                crmRetailSale.Attributes.Add("crc07_retailsaleid", guid);
                crmRetailSale.Attributes.Add("crc07_product", retailSale.Product);
                crmRetailSale.Attributes.Add("crc07_amount", retailSale.Amount);
                entityCollection.Entities.Add(crmRetailSale);
            }

            context.OutputParameters["BusinessEntityCollection"] = entityCollection;
        }
    }
    public class RetailSale
    {
        public string Product { get; set; }
        public Int32 Amount { get; set; }
    }
}

Update the plugin, then go to an account record. We see the products and amounts now coming through in the subgrid:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

2 Responses to Overriding the RetrieveMultiple Integration Pattern Updated for Unified Client Interface

  1. Thank you Carl this post really helped me.

    I can now see more than just the first record in grid after adding id.
    Paging doesn’t work though. It shows 1 – 10 of 100 but I cant page.

    Any advice would be greatly appreciated

Leave a Reply

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