Wednesday, December 22, 2010

Show attribute schema names within form editor

Maybe it's just me, but occasionally I find value in viewing attribute schema names in context of a form. This capability was on my wish list for Dynamics CRM 2011 but it didn't make the cut. Fortunately, you can run the JavaScript code I provided below to do just that.

You can execute this script and any other JavaScript code using the built-in "Developer Tools" in IE8 or by using IE WebDeveloper. First, load the form editor view (CRM displays the form editor UI in its own window). Then press Ctrl+N to open the editor in a window that allows you to get to the IE toolbar. Either press F12 (IE 8 only) or use IE WebDeveloper. Run the script in the interactive script window that's provided in each tool.
document.getElementsByClassName = function(cl,doc) {

  var retnode = [];
  var myclass = new RegExp('\\b'+cl+'\\b');
  var elem = doc.getElementsByTagName('*');
  for (var i = 0; i < elem.length; i++) {
    var classes = elem[i].className;
    if (myclass.test(classes)) retnode.push(elem[i]);
  }
  return retnode;
};
var doc = document.getElementById("contentIFrame").contentWindow.document;
var crmFormFields = document.getElementsByClassName("field",doc);
var crmReadOnlyFields = document.getElementsByClassName("rofield",doc);
if (crmReadOnlyFields.length > 0) {
  crmFormFields = crmFormFields.concat(crmReadOnlyFields);
}
var crmFieldId;
var crmFormField;
for (var i = 0; i < crmFormFields.length; i++) {
  crmFormField = crmFormFields[i];
  if (crmFormField.parentNode.parentNode.parentNode.parentNode.name) {
    crmFieldId = crmFormField.parentNode.parentNode.parentNode.parentNode.name;
    crmFormField.innerText = crmFieldId;
    crmFormField.style.color = "#000000";
  }
}
Here's a snapshot of part of the Account form. After running the script, the display name for each attribute is replaced with the schema name.