How to Detect Dataset PCF control is set for Home grid or Sub grid

By | August 22, 2019

Introduction

In this blog, we will see how to identify whether the current Dataset PCF control is used for home grid or sub grid of Dynamics 365 CRM.

Problem:

Create one Dataset PCF control and set that control on Account entity home grid. In this control read the current entity home grid records using view details as show in the below code:

If(context.parameters.sampleDataSet.getViewId()) {

 //read the records as per the view details and process on it

let viewId = context.parameters.sampleDataSet.getViewId();

} else {

//read the all home grid records guid and read it one by one

let recordsCollection = context.parameters.sampleDataSet.sortedRecordIds;

}

In the above code, first check if view details are present in context (context.parameters.sampleDataSet.getViewId())) or not. If there is view id within context then retrieve all records from that view using view id. If there is no view details in context then retrieve all records from CRM using guid.

So if we add this control on Account entity Home grid it will work fine. On load of home grid we will get current view details and as per the view details (view id) the records are read from CRM. However, this logic fails on Account entity sub grid.

Here, we have one custom entity ‘Publisher’ and there is one Account entity sub grid. If we set the Dataset PCF control to that sub grid, on load of sub grid we will get the current view details and because of that all records belonging to that view will get read from CRM. However, as only two records are associated for current publisher entity record, only that two records should be read from CRM. For this, we need to find some method to find out if current dataset PCF control is set for entity home grid or sub grid. On the load of sub grid it can be easily detected whether the current dataset PCF control is set for sub grid or not. If it is set for sub grid then we will get records guid collection using context.parameters.sampleDataSet.sortedRecordIds and read only the records associated with current record.

Solution:

On load of PCF control we get Context object, this object will refer CRM context. In this object you will get one property – contextString which defines location i.e. whether the current dataset PCF control is set for home grid or sub grid.

Please find below code to read the contextString.

context.navigation._customControlProperties.contextString

So, on load of dataset PCF control we can also check whether the current control is set for sub grid or not with the help of below code:

If(context.parameters.sampleDataSet.getViewId() && context.navigation._customControlProperties.contextString!=”subgrid”) {

//read the records as per the view details and process on it

let viewId = context.parameters.sampleDataSet.getViewId();

} else {

//read the all home grid records guid and read it one by one

let recordsCollection = context.parameters.sampleDataSet.sortedRecordIds;

}

Now, as per the above code on load of dataset PCF control, if current control is set for the sub grid then it will not read the records from view, the records will be read from CRM on the basis of guid one by one.

Conclusion

In this way, you can detect whether Dataset PCF control is set for Home grid or sub grid.

 

 

3 thoughts on “How to Detect Dataset PCF control is set for Home grid or Sub grid

  1. Martin Jesch

    “context.navigation._customControlProperties.contextString” is not mentioned in the official documentation (https://docs.microsoft.com/de-de/powerapps/developer/component-framework/reference/navigation), so I guess using it probably is unsupported. You don’t better don’t dig into internal object and make your code rely on it.
    You better define a property inside the control manifest and set it when adding the custom control to your listview or subgrid. It could be something like a boolean property “List Mode”.

    1. Inogic

      Hi Martin,

      Thank you for your suggestion. We have customers that don’t want to choose whether the control is for Home Grid or Sub Grid in the configuration step, they want PCF control to programmatically detect if the current grid is Home or Sub Grid. Also, we have checked if anywhere in the docs they have mentioned that the above property is unsupported but we are not able to find anything on that yet.

      We will check on this further, thanks for your inputs.

Comments are closed.