Read all column values in sub-grid on the form using client API–Dynamics CRM 2016

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.

image

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.

image

So attributes returned us a collection of XrmGridEntityAttributes. Interesting isn’t it? So let’s dig a bit deeper.

image

And there you go! You have the email address and telephone of the contact at your disposal.

Hope this helped!

Debajit Dutta
Business Solutions MVP

4 thoughts on “Read all column values in sub-grid on the form using client API–Dynamics CRM 2016”

  1. Hey, this would really help me, but I don’t understand how you implement this. I have a subgrid called Outages_Phonecall on my Contacts page, and I need to access the address rows (not a primary field) so I can map them on google maps.
    Could you give more instruction on how to implement this?
    Thank you.

    1. Hi,
      The address you are looking for is I guess in the telephone entity. The telephone entity does not have any address fields oob. so I guess it is the custom address fields. If this is the case, I have a sample code which fetches the city from the telphone subgrid (Outages_Phonecalls)
      function checkForPhoneCallAddress() {
      // get the grid in the contact form.
      var phoneCallGrid = Xrm.Page.getControl(“Outages_Phonecalls”).getGrid();
      // get the first Row Data
      var firstRowData = phoneCallGrid.getRows().get(0).getData();
      // get the entity object associated with this data.
      var phoneCallEntity = phoneCallEntity.getEntity();
      // get the city attribute
      var city = phoneCallEntity.getAttributes().get(“new_city”).getValue();
      // logging the value of the city in the console.
      console.log(“City: ” + city);
      }

  2. Hey, that’s what I was doing, but I found it only works within an entity form. It doesn’t seem to work on a Dashboard page. 
    Even document.getElementByID(“name”).control fails on a Dashboard.
    Do you know how to by-pass this? Just trying to count how many rows there are.

Comments are closed.