To Get Multi-Select Values From Sub-Grid Within Entity Form From Plugin Using C#

Introduction

This article demonstrates how to get the values in the sub-grid of an Entity Form from the plugin using C#. We do not need to use JavaScript or hidden field to achieve this.

Creation of Custom Entity Form

First step is to create custom solution.

 

Figure 1

Go to Settings -> Solutions and select the “New Solution” icon as shown above.

 

Figure 2

Provide the name of the solution and other related mandatory field values.

 

Figure 3

You can add the an already-existing entity or a new custom entity by clicking “New”. Here, in this example, I have added existing “Account” and “Contact” entity.

Creating N:N relationship:

Below image shows you how to create the N:N Relationship from “Account” to “Contact”.

 

Figure 4

Select the “Other Entity” from the dropdown and name the relationship as per your wish. Also, fill in the fields that are highlighted.

Adding sub-grid to the custom Entity Form

For creating custom entity form in “Account” entity, please follow the below steps.

 

Figure 5

By default, it will have some list of fields in “Account” entity. You can add or remove fields in the new form. Now, we need to insert the sub-grid to the new Entity Form.

 

Figure 6

On selecting the “Sub-Grid” from the top ribbon under the “INSERT” tab, you will get the below form to set properties of the “Sub-Grid”.

 

Figure 7

In the “Data Source” section, select Entity as “Contact” and Default View to “Active contacts” view. Also, provide the values for fields highlighted in the above image.

Now, you can see the “Sub-Grid” in your new custom Entity Form.

Custom Plugin creation

To create custom plugin, open Visual Studio, select "Empty project", and include C# class file to the project.

 

Figure 8

Source Code 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.Xrm.Sdk;  
  7. using Microsoft.Xrm.Sdk.Messages;  
  8. using System.ServiceModel.Description;  
  9. using Microsoft.Xrm.Sdk.Query;  
  10. using System.Runtime.Serialization;  
  11. namespace Dynamics_365_Portal.PMDMappings {  
  12.     public class PluginToGetSubGridValue: IPlugin {  
  13.         public void Execute(IServiceProvider serviceProvider) {  
  14.             //Extract the tracing service for use in debugging sandboxed plug-ins.  
  15.             ITracingService tracingService = (ITracingService) serviceProvider.GetService(typeof(ITracingService));  
  16.             // Obtain the execution context from the service provider.  
  17.             Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)  
  18.             serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));  
  19.             IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory));  
  20.             IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);  
  21.             if (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity) {  
  22.                 Entity entity = (Entity) context.PostEntityImages["PostImage"]; //refer the Post Image creation procedures given in the Figure 9 & 10.  
  23.                 //To get the email address of the account that is common for contacts too  
  24.                 EntityReference pmtype = (EntityReference) entity.Attributes["test_email"];  
  25.                 // calling the function to get the values from the sub grid.  
  26.                 EntityCollection ProviderMeasuresEC = GetProviderMeasuresList(orgService, pmtype.Name, Convert.ToString(pmtype.Id));  
  27.             }  
  28.         }  
  29.         // To get the entity collection from the Sub Grid  
  30.         private static EntityCollection GetProviderMeasuresList(IOrganizationService orgSvc, string searchItem, string searchItemValue) {  
  31.             //Create Fetch Expression.  
  32.             string fetchXML = @ "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" + "<entity name='test_Contact'>" + "<attribute name='test_Contactid' />" + "<attribute name='test_name' />" + "<order attribute='test_name' descending='false' />" + "<link-entity name='test_test_account_contact_test_provi' from='test_Contactid' to='test_Contactid' visible='false' intersect='true'>" + "<link-entity name='test_account' from='test_accountid' to='test_accountid' alias='ac'>" + "<filter type='and'>" + "<condition attribute='test_email' operator='eq' uiname='" + searchItem + "' uitype='test_email' value='{" + searchItemValue + "}' />" + "</filter>" + "</link-entity>" + "</link-entity>" + "</entity>" + "</fetch>";  
  33.             // Obtain results from the query expression.  
  34.             EntityCollection ec = orgSvc.RetrieveMultiple(new FetchExpression(fetchXML));  
  35.             return ec;  
  36.         }  
  37.     }  
  38. }   

FetchXML to query the sub-grid:

The below FetchXML is created using the advanced find in Dynamics 365.

 

Figure 9

Here, the key is, filtering from “Contacts” mapping to link-entity (N:N relationship) looking for the common fields and filtering it. 

  1. @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>" +  
  2.   
  3. "<entity name='test_Contact'>" +  
  4.   
  5. "<attribute name='test_Contactid' />" +  
  6.   
  7. "<attribute name='test_name' />" +  
  8.   
  9. "<order attribute='test_name' descending='false' />" +  
  10.   
  11. "<link-entity name='test_test_account_contact_test_provi' from='test_Contactid' to='test_Contactid' visible='false' intersect='true'>" +  
  12.   
  13. "<link-entity name='test_account' from='test_accountid' to='test_accountid' alias='ac'>" +  
  14.   
  15. "<filter type='and'>" +  
  16.   
  17. "<condition attribute='test_email' operator='eq' uiname='" + searchItem + "' uitype='test_email' value='{" + searchItemValue + "}' />" +  
  18.   
  19. "</filter>" +  
  20.   
  21. "</link-entity>" +  
  22.   
  23. "</link-entity>" +  
  24.   
  25. "</entity>" +  
  26.   
  27. "</fetch>";   

To create PostImage using Plugin Registration Tool

 

Figure 10

Need to provide the image type, as shown below.

Figure 11

To register the plugin and debug, please refer to this article.

Summary

Hope, my article will be helpful for those who are looking for retrieving multi-select values from sub-grid using plugin.

Up Next
    Ebook Download
    View all
    Learn
    View all