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

  • Suggested answer
    ashlega Profile Picture
    34,477 on at
    RE: How to add a dictionary object to shared variable of plugin context

    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.

  • Joseph McGregor Macdonald Profile Picture
    597 on at
    RE: How to add a dictionary object to shared variable of plugin context

    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
    khoait Profile Picture
    450 on at
    RE: How to add a dictionary object to shared variable of plugin context

    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.

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans