Wednesday, June 03, 2009

Scrolling Announcements on Home Page for CRM 4.0

Lately, I've started looking into problems people face when dealing with CRM. Well it is a part of my job and provides me a great learning opportunity.

A day or two before I got this requirement : The end user wanted to scroll Announcements[CRM entity : newsarticle] on top of home page. I made a simple script which can be used. This one fetches all Announcements so you might want to set some filters like expiry date or active on date.

Here are the steps to scroll announcement on CRM Home page. Kindly note that I do not take any responsibility if something goes wrong with this approach.
1. Go to CRMWeb\_root\ and take backup of bar_top.aspx and keep it safe somewhere. In case things goes wrong, you will need it to restore things back.
2. Open bar_top.aspx in Visual Studio
3. Add the following code snippet at the end of the file.

<!-- SCRIPT BEGIN : Scroll Announcements -->

<script type="text/javascript" language="javascript">
window.attachEvent('onload', ScrollAnnouncement);

function ScrollAnnouncement() {
var xml = "" +
"<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<soap:Envelope xmlns:soap=\"\" xmlns:xsi=\"\" xmlns:xsd=\"\">"
+ GenerateAuthenticationHeader()
" <soap:Body>" +
" <Execute xmlns=\"\">" +
" <Request xsi:type=\"RetrieveMultipleRequest\" ReturnDynamicEntities=\"true\">" +
" <Query xmlns:q1=\"\" xsi:type=\"q1:QueryExpression\">" +
" <q1:EntityName>businessunitnewsarticle</q1:EntityName>" +
" <q1:ColumnSet xsi:type=\"q1:ColumnSet\">" +
" <q1:Attributes>" +
" <q1:Attribute>articletitle</q1:Attribute>" +
" </q1:Attributes>" +
" </q1:ColumnSet>" +
" <q1:Distinct>false</q1:Distinct>" +
" </Query>" +
" </Request>" +
" </Execute>" +
" </soap:Body>" +
"</soap:Envelope>" +

var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");

xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttpRequest.setRequestHeader("Content-Length", xml.length);

var resultXml = xmlHttpRequest.responseXML;

var entityNode = resultXml.selectNodes("//BusinessEntities")[0];
var marqueeHTML = "<marquee class='ms-crm-MastHead-SignIn-User'>";
for (var i = 0; i < entityNode.childNodes.length; i++) {
if (i == 0) {
marqueeHTML += entityNode.childNodes[i].childNodes[0].childNodes[0].text
} else {
marqueeHTML += "&nbsp; &nbsp; " + entityNode.childNodes[i].childNodes[0].childNodes[0].text;
marqueeHTML += "<marquee>";

var headerTD = document.getElementById("tdLogoMastHeadBar");
headerTD.innerHTML += marqueeHTML;

<!-- SCRIPT END : Scroll Announcements -->

4. Check on home page.
You will see a scrolling marquee of the announcements title on CRM Header.

Monday, June 01, 2009

Hiding Menu Items from top menu bar in CRM 4.0

Here is a little script which you can call to hide menu item from top menu.
Put this script on page load or on any other event where you would like it.
You might also wanna add it as document.HideMenuItem = new function(targetMenu, targetMenuItem) {//code goes here};

function HideMenuItem(targetMenu, targetMenuItem) {
var menuLIs = document.getElementById("mnuBar1").getElementsByTagName("LI");
for (var i = 0; i < menuLIs.length; i++) {
if (menuLIs[i].title && menuLIs[i].title.indexOf(targetMenu) > -1) {
var targetDivs = menuLIs[i].getElementsByTagName("DIV");
for (var j = 0; j < targetDivs.length; j++) {
var targetLIs = targetDivs[j].getElementsByTagName("LI");
for (var k = 0; k < targetLIs.length; k++) {
if (targetLIs[k].innerHTML.indexOf(targetMenuItem) > -1) {
targetLIs[k].style.display = "none";

All you have to do is call it with two parameters first parameter is the text without space so if you have root menu named Sub Test, you should call it SubTest. And the second parameter is the whole text of the menu item you need to hide.

HideMenuItem("ISV.NEW", "Coming Soon...");
HideMenuItem("SubTest", "Test Sub 1");
HideMenuItem("ISV.NEW", "Web Only");

Let me know if you need any more info on the same.
PS: Thanks to Andrew, there was a bug in the previous version of script, which didn't work with all the menus but now it has been fixed.