Dynamics 365 Calling Update in SDK.REST Invokes Retrieve

Leave a comment

In this post we will look at how calling the update method in SDK.REST causes a retrieve to run as well. This is something to note as you may have plugins that run on the retrieve of an entity, which may cause unintended consequences if not handled properly.

Let’s create a plugin that will run on the Update of an account:

Add the plugin assemblies through NuGet:

And the code. We will simply write to the Plugin Trace Log when the update plugin has been invoked:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace Carl.UpdateAccountPlugin
{
    public class UpdatePlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            tracingService.Trace("Update Plugin has Run");
        }
    }
}

Add a strong name to the assembly.

Let’s register this against the Account Update using the Plugin Registration Tool:

Now let’s create a plugin to run on Retrieve of an Account:

Add the plugin assemblies through NuGet:

Add the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Xrm.Sdk;

namespace Carl.RetrieveAccountPlugin
{
    public class RetrievePlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            tracingService.Trace("Retrieve Plugin has Run");
        }
    }
}

Add a strong name to the assembly.

Now let’s register this on Account Retrieve:

Now we have the plugins ready to run.

Let’s try doing an update using the CRM REST Builder.

Open the REST Builder:

Create a request to update an account using SDK.REST:

You can see that the Update and Retrieve plugins have both been run even though we only called Update:

Now let’s create a request using the WebAPI:

Run the request to update the account:

Only the update runs and the retrieve is not called:

Using the WebApi is one way you can avoid the retrieve running if required.

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

 

Leave a Reply

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