C# Code · Dynamics 365 WEB API · Dynamics Plugins

{Common Data Service} Pass shared variable to plugins execution pipeline from Organization service and WEB API


The shared variable property allows for including data that can be passed from the API or a plug-in to a step that occurs later in the execution pipeline.

You can set a string value that will be available to plug-ins within the ExecutionContext in the SharedVariables collection.

Note :- Shared variable passed from Organization Service and WEB API will be available in both Pre-operation and Post-Operation execution pipeline of that particular entity. This will be helpful where we need to perform some business logic based on value passed from Organization service and WEB API. We don’t need to store that details in any custom field.

Add a Shared Variable from the Organization Service

Entity objMyAccount = new Entity("account");
objMyAccount["name"] = "Test Shared Variable from C#";

var createRequest = new CreateRequest() { Target = objMyAccount };
//Set Shared Variable using tag property
createRequest["tag"] = "This is a value passed.";


Add a Shared Variable from the Web API

To pass this value using the Web API, simply use the tag query option.
For example: ?tag=This is a value passed.

var entity = {};
entity.name = "Test SharedVariable from WEB API";

var req = new XMLHttpRequest();
req.open("POST", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/accounts?tag=This is a value passed from web api", true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 204) {
            var uri = this.getResponseHeader("OData-EntityId");
            var regExp = /\(([^)]+)\)/;
            var matches = regExp.exec(uri);
            var newEntityId = matches[1];
        } else {

Pre-Operation Plugin code on Account Entity

As you can see we are setting shared variable to fax field on Account.

if (context.SharedVariables.Contains("tag"))
entity.Attributes.Add("fax", context.SharedVariables["tag"].ToString());
// Create a new accountnumber attribute, set its value, and add
// the attribute to the entity's attribute collection.
Random rndgen = new Random();
entity.Attributes.Add("accountnumber", rndgen.Next().ToString());

Below image shows Account record created from Console Application using c#. As you can see fax field value is mapped from shared variable value.

Below image shows Account record created from WEB API from Rest Builder . As you can see fax field value is mapped from shared variable value.

Post-Operation plugin code on Account Entity

// Create a task activity to follow up with the account customer in 7 days. 
                    Entity followup = new Entity("task");

                    string sharedVar = string.Empty;
                    if (context.SharedVariables.Contains("tag"))
                        sharedVar = context.SharedVariables["tag"].ToString();

                    followup["subject"] = "Send e-mail to the new customer.";
                    followup["description"] =
                        "Follow up with the customer. Check if there are any new issues that need resolution." + sharedVar;
                    followup["scheduledstart"] = DateTime.Now.AddDays(7);
                    followup["scheduledend"] = DateTime.Now.AddDays(7);
                    followup["category"] = context.PrimaryEntityName;

                    // Refer to the account in the task activity.
                    if (context.OutputParameters.Contains("id"))
                        Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                        string regardingobjectidType = "account";

                        followup["regardingobjectid"] =
                        new EntityReference(regardingobjectidType, regardingobjectid);

                    // Obtain the organization service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    // Create the task in Microsoft Dynamics CRM.
                    tracingService.Trace("FollowupPlugin: Creating the task activity.");

We are setting shared variable value to Description field on Task record which we are creating from post create plugin on Account.

TODO for blog reader – You could also write plugin code and register step on Pre-validation step to check if shared variable from Organization Service and WEB API is available.

Let me know your comments.

Hope this helps!

2 thoughts on “{Common Data Service} Pass shared variable to plugins execution pipeline from Organization service and WEB API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s