CRM 2011: Dynamically change sub-grid fetchXml - left navigation pane

In CRM 2011 account entity there are 2 left navigation links called Open Activities and Closed activities. When we select these, the sub-grid populates activites associated with the related contacts also. To filter them based on the regarding field we can use javascript to pass the fetchXml dynamically and change the view.

For this you need the IFrame id in which the grid gets loaded.

To get the IFrame control:

var frame = document.frames["areaActivitiesFrame"].frameElement;

Bind “onreadystatechange” event for the Iframe:

frame.onreadystatechange = function () {
if (event.srcElement.readyState == “complete”) {
BindGrid(“Open”, frame);
}
};

Get grid control and pass the fetchXml

function BindGrid(state, frame) {
var currentId = Xrm.Page.data.entity.getId();
var currentName = Xrm.Page.getAttribute(“name”).getValue();
var fetchXmlStr = getFetchXml(currentId, currentName, state);
fetchXmlStr += getFetchXml(null, null, state);
frame.contentWindow.document.getElementById(“AppGridFilterContainer”).style.display = “none”; //To hide filter
frame.contentWindow.document.getElementById(“newViewSelector”).innerText = state + “Activities:”; //Change the text
frame.contentWindow.document.getElementById(“crmGrid_Account_ActivityPointers_SavedNewQuerySelector”).style.display = “none”;
frame.contentWindow.document.getElementById(“crmGrid_Account_ActivityPointers”).control.setParameter(“fetchXmlForFilters”, fetchXmlStr);

frame.contentWindow.document.getElementById(“crmGrid_Account_ActivityPointers”).control.setParameter(“fetchXml”, fetchXmlStr);
frame.contentWindow.document.getElementById(“crmGrid_Account_ActivityPointers”).control.refresh();

}

After getting the grid contol you can make any changes you want in the grid: changing the header text, hiding elements inside the grid…

Thanks.