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

Datetime:2017-04-20 05:22:03         Topic: C#          Share        Original >>
Here to See The Original Article!!!

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 the 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) {  
            //Extract the tracing service for use in debugging sandboxed plug-ins.  
            ITracingService tracingService = (ITracingService) serviceProvider.GetService(typeof(ITracingService));  
            // Obtain the execution context from the service provider.  
            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"]; //refer the Post Image creation procedures given in the Figure 9 & 10.  
                //To get the email address of the account that is common for contacts too  
                EntityReference pmtype = (EntityReference) entity.Attributes["test_email"];  
                // calling the function to get the values from the sub grid.  
                EntityCollection ProviderMeasuresEC = GetProviderMeasuresList(orgService, pmtype.Name, Convert.ToString(pmtype.Id));  
            }  
        }  
        // To get the entity collection from the Sub Grid  
        private static EntityCollection GetProviderMeasuresList(IOrganizationService orgSvc, string searchItem, string searchItemValue) {  
            //Create Fetch Expression.  
            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>";  
            // Obtain results from the query expression.  
            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 thisarticle.

Summary

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








New