Microsoft introduced the Xrm.Page object model to interact with the sub-grids on your forms from 2015 Online Update 1 and in CRM 2016 (for on-premise as well). Wonderful isn’t it. You can query the number of rows in the sub-grid, get the primary attribute value, guid and the logical name of each of the record in the sub-grid.
However one big limitation I could find is that, if your sub-grid is having multiple columns, except the primary attribute column, values of no other columns are returned by the existing API. Let us take an example here.
I have a contacts sub-grid on my account entity. The sub-grid has columns – email address, telephone and others.
The requirement here was to read all the column values in sub-grid. The recommended way to get to the grid row data is by the code below.
var rowData = Xrm.Page.getControl(“Contacts”).getGrid().getRows().get(<rowindex>).getData();
var entity = rowData.getEntity();
var entityReference = entity.getEntityReference();
var primaryAttributeValue = entity.getPrimaryAttributeValue();
The above code gets the data for the row at the specified <row index>. You can fetch the entity logical name, the guid of each row data as well as the primary attribute value for each record. But as you can see there is no way to get the other column values like email, telephone etc
Off-course we could get the ID of the data rows and fire a web-api query to get the other column values. And that is supported way.
However getting dirty with the code and playing with it excites me much and here also I tried to dig a bit deep. Before I dig deeper, a statutory warning which should not be taken lightly.
** All the methods that I would be talking below is not documented in the SDK and hence unsupported and hence might now work in future versions of CRM. So if you implement, please make sure you get an approval from your client. **
Let us take the above code only. Till we get the entity, we would use the same code. However to get the attributes of the entity, we will use the getAttributes method of the entity. So let see how this looks in the developer toolbar.
So attributes returned us a collection of XrmGridEntityAttributes. Interesting isn’t it? So let’s dig a bit deeper.
And there you go! You have the email address and telephone of the contact at your disposal.
Hope this helps!