Saturday, August 30, 2008

CRM 3.0 - Hiding the Delete Button Hack

My day to day job revolves around supported solutions on CRM 4.0 and Microsoft .Net however as we all know customers some times are demanding and asks for features which doesn't come under umbrella of supportability. And as a solution provider we have to meet it by hook or crook.

In this case, I came across a mail on one of our WW Community. The problem statement was pretty clear : The customer wanted to hide delete button that appears on the CRM GridView for each and every entity(maybe selected entities too). Now they wanted the users to have rights to delete CRM records so we can't just take away their rights(Yeah.. that's the first thing that came to my mind...). But they wanted to do it via some custom applications not from the CRM Web UI. After doing some R&D I found that it can be achieved via the steps mentioned in next paragraph. I would like to declare that this code/method is NOT AT ALL supported by CRM Product group. This is just to demo that certain feature can be disabled in CRM without affecting roles.

1. Locate and back up loader.aspx from CRMWeb folder.
2. Open loader.aspx in Visual Studio or any text editor of your choice.
3. At the closure of the first script tag, copy-paste the following code

<!--/******Disable Delete Button Hack BEGIN*********/-->
<script language="JavaScript" type="text/javascript">
function HideDeleteButton()
{
//Check whether the iframe is completely loaded or not
if(document.getElementById("stage").readyState == "complete")
{
try
{
//Those menu items are part of mnuBar1 span, so we will get the reference of mnuBar1 first
//and then all the SPAN elements(represents individual menu items)
var spanElements = document.frames[2].document.getElementById("mnuBar1").getElementsByTagName("SPAN");

//iterate through the menu items and check whether the title is Delete or not
//if yes, this is the one we are looking for, set its display to none and break the loop
for(var i = 0; i <= spanElements.length; i++)
{
try
{
if(spanElements[i].title == "Delete")
{
spanElements[i].style.display = "none";
break;
}
}
catch(e){continue;}//continue in case of error
}
}
catch(e){}//ignore any other error
}
}
</script>
<!--/******Disable Delete Button Hack END*********/-->

4. There are frameset definitions at the end of the page, change
<frame name="stage" scrolling="yes" src="<%HttpUtility.HtmlAttributeEncode(homePageUrl, Response.Output);%>"></frame>
to
<frame name="stage" scrolling="yes" src="<%HttpUtility.HtmlAttributeEncode(homePageUrl, Response.Output);%>" onreadystatechange="HideDeleteButton()"></frame>
5. Test by opening up the workplace.

This script might have other side effects which I've not fully explored yet so use it at your own risk.


No comments: