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 :
Microsoft Dynamics CRM (Archived)

How to add a dictionary object to shared variable of plugin context

(0) ShareShare
ReportReport
Posted on by

I am having a dictionary object with 5 values in it. Now I want to add this to the plugin context shared variable. Is it possible?

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    khoait Profile Picture
    450 on at

    Hi Ancy. I don't think it is possible. You may want to create a custom entity with just name and value fields to store your dictionary data, and retrieve them from other plugins.

  • Joseph McGregor Macdonald Profile Picture
    597 on at

    You question does not make it clear of the code context where you have the dictionary object but I think it is possible using one of the following approaches

    This script I have copied below demonstrates sharing a dictionary object between prevalidation, preoperation and postoperation stages for the create, update or delete messages. Note that the preoperation and postoperation stages actually have to access the parent context to get a shared variable from the prevalidation message

    If the context where you have the dictionary is outside of a plugin, and you want to pass the dictionary into code in the plugin message, you would probably have to use a custom field(s). You could set a text field to the serialised dictionary, then get and deserialise it inside the plugin

                    if(IsStage(PluginStage.PreValidationEvent))
                    {
                        var dictionary = new Dictionary<string, string>
                        {
                            { "Key1", "Value1" },
                            { "Key2", "Value2" },
                            { "Key3", "Value3" },
                        };
                        IPluginExecutionContext.SharedVariables.Add("DICTIONARY1", dictionary);
                    }
                    if (IsStage(PluginStage.PreOperationEvent))
                    {
                        var getDictionary = (Dictionary<string, string>)IPluginExecutionContext.ParentContext.SharedVariables["DICTIONARY1"];
    
                        var dictionary2 = new Dictionary<string, string>
                        {
                            { "Key1", "Value1" },
                            { "Key2", "Value2" },
                            { "Key3", "Value3" },
                        };
                        IPluginExecutionContext.SharedVariables.Add("DICTIONARY2", dictionary2);
                    }
                    if (IsStage(PluginStage.PostEvent))
                    {
                        var getDictionary1 = (Dictionary<string, string>)IPluginExecutionContext.ParentContext.SharedVariables["DICTIONARY1"];
                        var getDictionary2 = (Dictionary<string, string>)IPluginExecutionContext.SharedVariables["DICTIONARY2"];
                    }


  • Suggested answer
    ashlega Profile Picture
    34,477 on at

    Would add to Joseph's response that you have to keep serialization in mind:

    msdn.microsoft.com/.../gg328579.aspx

    "It is important that any type of data added to the shared variables collection be serializable otherwise the server will not know how to serialize the data and plug-in execution will fail."

    Dictionary itself should not a problem as long as everything there is serializable.

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 > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans