web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Help with C# plugin and the email entity

(0) ShareShare
ReportReport
Posted on by 57

Hi,

I am trying to create my first C# plugin for D365 online. I want to capture tracked emails before they are created in D365 and edit the body of the email (strip out some HTML out of the body to reduce the size of the storage required).

I can modify the "subject" of the email successfully but no matter what I do I cannot change the "description" (body) of the email entity. I don't get an error just nothing is changed.

I have registered the plugin step against Message=Create, Entity=email and as a Synchronous Pre-Operation.

My plugin is firing when I expect it to as I have confirmed this by throwing an error.

My code is as follows:

public void Execute(IServiceProvider serviceProvider)
{
    // Plugin Step:
    // Message:         Create
    // Primary Entity:  email
    // Run in Context:  Calling User
    // Pipeline Stage:  PreOperation
    // Execution Mode:  Synchronous
    // Deployment:      Server

    IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
    Entity entity = (Entity)context.InputParameters["Target"];

    if (entity.Contains("subject"))
    {
        entity["subject"] = "Plugin: Test Subject";
    }

    //Clean the description (body of the email)
    if (entity.Contains("description"))
    {
        entity["description"] = "Plugin: Test Description";
    }
}

Thanks,

Mark

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello Mark,

    You code looks clean and correct for me. What I would recommend to try is to check your code using Plugin Profiler - www.youtube.com/watch

  • MKennerley Profile Picture
    57 on at

    Thanks Andrew for the debugging tip. How to debug D365 online was going to be my next question!

    However, I am still at a loss as to why the "description" doesn't get updated.

    When debugging through I can see that the "description" attribute is being changed to "Plugin: Test Description" but then when I look at the email through D365 itself only the Subject has been updated and the Description is unchanged.

    Not sure what else I can check for?

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    There is a chance that something updates description afterwards. Try to turn on auditing and check what is happenning.

    As alternative you can do following:

    1. Change plugin registration to Async.

    2. Rewrite the code to use Update to update description in this plugin.

    Not so elegant but it could do the trick.

  • Suggested answer
    MKennerley Profile Picture
    57 on at

    Thanks Andrew for your help. The tips on Debugging and Auditing helped point me in the right direction to solve this.

    Whilst the "Create" message was firing there was a parent message, "DeliverPromote", that was changing the Email Description back to the original after my changes.

    So I have changed my code (and the plugin step) to now fire from the "DeliverPromote" message (PreOperation & Synchronous).

    From here I can edit the body of the email before CRM tries to create the Email entity.

    Thanks,

    Mark

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 108 Super User 2025 Season 2

#2
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans