Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

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
    ashlega 34,475 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.

  • 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
    khoait 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans