29 July 2009

CRM 4.0 : Field Level Security on Print form

CRM 4.0 doesn't provide a true field level security, e.g.: If developers hide attributes/tabs for certain users using crmForm.all.filed.style.display = "none"; These users can still see the field if they Print the record(CRM print preview). I have submitted a feedback to Microsoft about it.

There are no supported way to achieve that, this workaround is not supported and it's not the true field level security!

The file you need to modify is: \CRMWeb\_forms\print\print.aspx
Add the following code just before the 〈/html〉 tag.



<!--
Field level security on Print form
author: Jim Wang @ July 2009
http://jianwang.blogspot.com
-->

<script language="javascript">
var printFrame = document.getElementById("printMain");
var printWindow = document.frames["printMain"];
printFrame.onreadystatechange = function()
{
if(window.opener && printWindow.document.readyState == "complete")
{
//hide attributes
var allFields = opener.document.getElementsByTagName("TD");
for (var i = 0; i < allFields.length; i++)
{
var thisField = allFields[i];
if (thisField.style.display == "none")
{
printWindow.document.getElementById(thisField.id).style.display = "none";
}
}

//hide tabs
var printTabs = printWindow.document.getElementsByTagName("DIV");
var openerTabs = opener.document.getElementsByTagName("LI");
for (var i = 0; i < openerTabs.length; i++)
{
var openerTab = openerTabs[i];
if (openerTab.className && openerTab.className == "ms-crm-Tab")
{
if(opener.document.getElementById(openerTab.id).style.display == "none")
var printTab = printTabs[openerTab.id.replace("tab","").replace("Tab","")];
printTab.style.display = "none";
}
}

}
}
</script>