Microsoft Dynamics CRM 2011 – Javascript to display associated views

By - October 21, 2011

This post is a companion post of Microsoft Dynamics CRM 2011 – Activity Sub-grids only display regarded records, which details a known CRM 2011 issue with Activity Sub-grids. This article provides CRM 2011 javascript to display the activity associated views, in the same manner as we did in CRM 4.0. Please read the end of this post where I detail the caveats of using this workaround with CRM 2011. The original javascript for 4.0 was authored by Daniel Cai here: mscrm-40-remove-existing-xxxxx-to-this.html.

/**

* Load an associated view into an IFrame, hide it from LHS navigation menu,

* and remove "Add Existing" button in the associated view.

* @author Daniel Cai, http://danielcai.blogspot.com/

*

* Parameters:

* @param iframe: The IFrame's object, e.g. crmForm.all.IFrame_Employer_Address

* @param navItemId: LHS navigator's HTML element ID of the associated view.

It usually starts with "nav".

* @param relName: The relationship name, this parameter is only required

* when you want to remove "Add Existing" button.

* Modified by Bill Caldwell for CRM 2011. There was some laziness here in not porting all the code to CRM 2011 javascript

* Removed, "Removed 'Add Existing' button" because N/A in 2011. Removed code to remove from left navigation on form since this can be done OOB with CRM 2011.

* Insert entire code into On load function or call function from on load event (see last line of code)

* No code to account for "new" record, so it looks ugly on new record. Very easy to add this though.

* This is not an issue if your contacts are always created from leads (as was the case with my project).

* The subgrid for Current activities is probably fine. For historical, it's not since only "regarding" records show.

*/

function loadAssociatedViewInIFrame(iframe, navItemId, relName) {

var clickActionPattern = /loadArea(['"]{1}([A-Za-z0-9_]+)['"]{1}(, ?['"]x26roleOrdx3d(d)['"])*).*/;

var roleOrd;

var getFrameSrc = function (areaId) {

var url = "areas.aspx?oId=" + encodeURI(Xrm.Page.data.entity.getId());

url += "&oType=" + crmForm.ObjectTypeCode;

url += "&security=" + crmFormSubmit.crmFormSubmitSecurity.value;

url += "&tabSet=" + areaId;

url += (!roleOrd) ? "" : "&roleOrd=" + roleOrd;

return url;

};

var onReadyStateChange = function () {

if (iframe.readyState === 'complete') {

var frameDoc = iframe.contentWindow.document;

// Remove the padding space around the iframe

//frameDoc.body.scroll = "no";

//frameDoc.body.childNodes[0].rows[0].cells[0].style.padding = "0px";

}

};

(function init() {

if (!Xrm.Page.data.entity.getId()) return;

var navItem = document.getElementById(navItemId);

if (!navItem) return;

var clickAction = navItem.getAttributeNode('onclick').nodeValue;

if (!clickAction || !clickActionPattern.test(clickAction))

return;

//navItem.style.display = 'none';

var areaId = clickAction.replace(clickActionPattern, '$1');

roleOrd = clickAction.replace(clickActionPattern, '$3');

iframe.src = getFrameSrc(areaId);

iframe.allowTransparency = true; // Get rid of the white area around the IFrame

iframe.attachEvent('onreadystatechange', onReadyStateChange);

})();

};

loadAssociatedViewInIFrame(crmForm.all.IFRAME_History, 'navActivityHistory', 'Contact_ActivityPointers');

Caveats:

  • When using the javascript, you are not able to click in the grid area and see the ribbon change to be able to add an activity.
  • You have to go to the “Add” tab at the top to be able to add new activities.
  • The add, add existing and other toolbar buttons are no longer available in associated views in CRM 2011. Related to above item.
  • The subgrids (because they are part of the form) automatically refresh when you add a new activity. This old school method using the javascript still requires you to click the refresh icon to see new activities or see them move from current to history.
  • When I (Bill C) rewrote this for CRM 2011, I still used some CRM 4.0 object references which can probably be replaced with CRM 2011 references. They worked for me and I was under the gun, so I didn’t do it.

By: Bill Caldwell – Colorado Microsoft Dynamics CRM Partner

Follow Bill on Google+

Bill Caldwell is a director and lead Microsoft Dynamics CRM consultant in the Denver, Colorado practice.  Bill is a certified Microsoft Dynamics CRM consultant and specializes in customizations, reports, customized queries and integrations.  Bill is an active member of the Dynamics community since joining RSM in 2007.  Bill is also experienced with the Microsoft development suites, which includes SQL Server, Visual Studio.Net and SQL Server Reporting Services.  Prior to this, Bill spent five years working in industry as an accountant and financial analyst. Contact Information: Email: Bill.Caldwell@rsmus.com Phone: 303.298.6465 Follow Bill on Google+

Receive Posts by Email

Subscribe and receive notifications of new posts by email.