CRM 4.0: Create Dynamic Menu Items

We had a requirements to add Dynamic Menu Items to CRM 4.0 Toolbar. I have followed following blog post by Sonoma Partners for implementing Dynamic Menu Items. I have modified their code with simple JavaScript DOM.

http://blog.sonomapartners.com/2009/12/adding-dynamic-menu-items.html

You can follow above mentioned link to create a stub for the implementation and then refer below mentioned code which uses JavaScript DOM and not JQuery to create them.

function createLinksMenu() {

    var links = getCallLinks();
    var dummyElement;

    var ul = document.getElementById("mnuNew_0_CallCustomer");
    var items = ul.getElementsByTagName("li");
    for (var i = 0; i < items.length; ++i) {
        // do something with items[i], which is a <li> element
        if (items[i].innerHTML.indexOf("Dummy") > 0) {
            dummyElement = items[i].cloneNode(true);
            break;
        }
    }

    // Loop through all the links 
    //   then clone the dummy node, update it as necessary and append it to the menu
    for (var i = 0; i < links.length; i++) {
        //alert(links[i].Name);

        var elem = document.getElementById("mnuNew_0_CallCustomer");
        var newEle = dummyElement.cloneNode(true);
        newEle.id = "CallCustomerMenu" + i;
        newEle.className = "ms-crm-MenuItem-Label";
        newEle.tabIndex = -1;
        newEle.action = "callNumber('" + links[i].Url + "');";
        newEle.innerHTML = "<SPAN class='ms-crm-MenuItem-Label'><A class='ms-crm-MenuLink' tabIndex='-1' onclick='return false;' href='javascript:onclick();' target='_self'><SPAN class='ms-crm-MenuItem-Icon'></SPAN><SPAN class='ms-crm-MenuItem-Text' tabIndex='0'>" + links[i].Name + "</SPAN></A></SPAN>";
        elem.appendChild(newEle);
    }

    // remove dummy node
    items = ul.getElementsByTagName("li");
    for (var i = 0; i < items.length; i++) {
        //alert(items[i].innerHTML.indexOf("Dummy"));

        // do something with items[i], which is a <li> element
        if (items[i].innerHTML.indexOf("Dummy") > 0) {
            items[i].style.display = "none";
            break;
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s