Search code examples
dynamics-crm

Is there any way to store a variable globally in a Dynamics CRM plugin


We have a plugin for Dynamics CRM that needs to call an API using an OAuth token. We would like to store the token until it expires, rather than getting a new token each time the plugin is executed. I tried to use SharedVariables on the context, but that doesn't seem to work to do this (found some conflicting information on how Shared Variables work, so not sure what they are really for).

Is there anything available to Dynamics CRM plugins that will allow you to store a variable across executions?


Solution

  • Plugins are executed and kept alive in .NET app domains and therefore you can actually use a static field or a MemoryCache object. Be aware that objects cached by a MemoryCache even survive updates of the plugin assembly, so using that with fixed keys can lead to unexpected results. Of course you would need to make a thread safe design.

    Shared variables is a collection of data that is available during the processing of an individual request (create, update etc.). It is used to share data between the various stages of the plugin pipeline (validate/pre/post/async).