Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5984849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:26:23+00:00 2026-05-22T22:26:23+00:00

I’m trying to bind a plugin to the update contact event in Microsoft Dynamics

  • 0

I’m trying to bind a plugin to the update contact event in Microsoft Dynamics CRM 2011.
I’ve made a plugin and i already registered the assembly and step for my organisation.

screenshot: CRM registration tool

For this moment, i’m using sample code for my plugin.

public class Plugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)
        serviceProvider.GetService(typeof(IPluginExecutionContext));

    Entity entity;

    // Check if the input parameters property bag contains a target
    // of the create operation and that target is of type Entity.
    if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
    {
        // Obtain the target business entity from the input parameters.
        entity = (Entity)context.InputParameters["Target"];

        // Verify that the entity represents a contact.
        if (entity.LogicalName != "contact") { return; }
    }
    else
    {
        return;
    }

    try
    {
        IOrganizationServiceFactory serviceFactory =
            (IOrganizationServiceFactory)serviceProvider.GetService(
        typeof(IOrganizationServiceFactory));
        IOrganizationService service =
        serviceFactory.CreateOrganizationService(context.UserId);

        var id = (Guid)context.OutputParameters["id"];

        AddNoteToContact(service, id);
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
        throw new InvalidPluginExecutionException(
        "An error occurred in the plug-in.", ex);
    }
}

private static void AddNoteToContact(IOrganizationService service, Guid id)
{
    using (var crm = new XrmServiceContext(service))
    {

        var contact = crm.ContactSet.Where(
        c => c.ContactId == id).First();
        Debug.Write(contact.FirstName);

        var note = new Annotation
        {
            Subject = "Created with plugin",
            NoteText = "This Note was created by the example plug-in",
            ObjectId = contact.ToEntityReference(),
            ObjectTypeCode = contact.LogicalName
        };

        crm.AddObject(note);
        crm.SaveChanges();
    }

}
}

But every time i modify a contact form and save it, i get this error:

The given key was not present in the dictionary

I’ve been looking for answers for a week now. I hope there is someone here who can guide me to the sollution for this problem. I can give every code or information you need. But for now, i can’t think of anything more that maybe can help you to see where my error is located. Any help is very much appreciated.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:26:24+00:00Added an answer on May 22, 2026 at 10:26 pm

    M.Medhat is absolutely correct, but let’s expand on it a bit more so you understand.

    The first thing that you need to know is the difference between InputParameters vrs OutputParameters. A quick read at this MSDN article describing the difference between InputParameters and OutputParameters.

    Make sure to note this statement:

    If a plug-in is registered for a pre-event, the OutputParameters property bag would not contain a value for the “id” key because the core operation would not yet have occurred.

    Hence, this code would break:

    var id = (Guid)context.OutputParameters["id"];
    

    Since you’ve already created an entity (by casting it off of InputParameters) you could delete that line and do something like this:

    AddNoteToContact(service, entity.id);
    

    Don’t forget about tracing, it’s your best friend. It can show information when an exception is thrown. Here’s a good link on it: tracing

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.