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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Xrm.Sdk;
- using Microsoft.Xrm.Sdk.Messages;
- using System.ServiceModel.Description;
- using Microsoft.Xrm.Sdk.Query;
- using System.Runtime.Serialization;
- namespace Dynamics_365_Portal.PMDMappings {
- public class PluginToGetSubGridValue: IPlugin {
- public void Execute(IServiceProvider serviceProvider) {
-
- ITracingService tracingService = (ITracingService) serviceProvider.GetService(typeof(ITracingService));
-
- Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
- serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));
- IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory));
- IOrganizationService orgService = serviceFactory.CreateOrganizationService(context.UserId);
- if (context.PostEntityImages.Contains("PostImage") && context.PostEntityImages["PostImage"] is Entity) {
- Entity entity = (Entity) context.PostEntityImages["PostImage"];
-
- EntityReference pmtype = (EntityReference) entity.Attributes["test_email"];
-
- EntityCollection ProviderMeasuresEC = GetProviderMeasuresList(orgService, pmtype.Name, Convert.ToString(pmtype.Id));
- }
- }
-
- private static EntityCollection GetProviderMeasuresList(IOrganizationService orgSvc, string searchItem, string searchItemValue) {
-
- 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>";
-
- EntityCollection ec = orgSvc.RetrieveMultiple(new FetchExpression(fetchXML));
- return ec;
- }
- }
- }
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.
- @"<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>";
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.