Use case: I have two entity Awards and Awards category. We have SubGrid on the Awards entity form for Category with Custom Ribbon button. We want to have the button should be visible on specific view only and below are steps to do so.

Step 1: Configure the Ribbon Button

Create a Ribbon Button, create a command and enable rule. Added enable to Command and command to ribbon button. To enable rule, create a custom rule and pass the JavaScript and parameter.

Step 2: Get View id based on the CRM parameter:

Now, Get Subgrid view id by passing the CRM parameter in the custom rule.

When CRM parameter is Primary Control

function EnableRuleusingPrimaryControl(primaryControl){
    //Get the SubGrid Control
    var subGridControl = primaryControl.getControl("Categories");
    var viewSelector = subGridControl.getViewSelector();
    var currentView = viewSelector.getCurrentView();
    var currentViewId = currentView.id;
    currentViewId = currentViewId.replace("{","").replace("}","");
}

When CRM parameter is Selected Control

function EnableRuleusingSelectedControl(selectedControl){
    var viewSelector = selectedControl.getViewSelector();
    var currentView = viewSelector.getCurrentView();
    var currentViewId = currentView.id;
    currentViewId = currentViewId.replace("{","").replace("}","");
}

Step 3: Write script for Enable Rule for Custom Ribbon Button.

We will write a script that will enable or disable the Ribbon based on the view ID using the above code based on the passed parameter.

function EnableRule(crmParameter){
    //Logic to Get the View Id
    var currentViewId = "";
    if(currentViewId === ""){
        return true;
    }else{
        return false;
    }
}

Result:

I have written the enable rule that Button should be visible only on the Active Award Category view:

Change Award button is visible when the view is “Active Award Categories”
Change Award button is hidden when the view is not “Active Award Categories”

Result View of Current View that we get from the Controls in Web Browser Console App

Full Code:

unction EnableRuleusingPrimaryControl(primaryControl){
    //Get the SubGrid Control
    var subGridControl = primaryControl.getControl("Categories");
    var viewSelector = subGridControl.getViewSelector();
    var currentView = viewSelector.getCurrentView();
    var currentViewId = currentView.Id;
    currentViewId = currentViewId.replace("{","").replace("}","");

    if(currentViewId === ""){
        return true;
    }else{
        return false;
    }

}

function EnableRuleusingSelectedControl(selectedControl){
    var viewSelector = selectedControl.getViewSelector();
    var currentView = viewSelector.getCurrentView();
    var currentViewId = currentView.Id;
    currentViewId = currentViewId.replace("{","").replace("}","");

    if(currentViewId === ""){
        return true;
    }else{
        return false;
    }

}

The post [Quick Tip] Get SubGrid View Id using Primary Control or Selected Control for Ribbon button’s enable rule in Dynamics 365 CRM. appeared first on Cloudfronts - Microsoft Dynamics 365 | Power BI | Azure.