Microsoft Dynamics CRM 2011 – Hide Areas of a Form

// Toggle the Ribbon Toolbar to Show/Hide (same as clicking the show/hide Ribbon button)
window.top.document.getElementById("minimizeribbon").fireEvent("onclick");

// Hide the Ribbon Toolbar and move the form Content area to the top of the window.
window.top.document.getElementById("crmTopBar").style.display = "none";
window.top.document.getElementById("crmContentPanel").style.top = "0px"; // Move Form Content area up to top of window, initial style.top is 135px

// Hide Left Hand Nav bar / pane
document.getElementById("crmNavBar").parentElement.style.display = "none";
document.getElementById("tdAreas").parentElement.parentElement.parentElement.parentElement.colSpan = 2;

// Hide the Breadcrumb and Record Set Toolbar
document.getElementById("recordSetToolBar").parentElement.style.display = "none";

// Hide the Form Footer Bar
document.getElementById("crmFormFooter").parentElement.style.display = "none";

Advertisement

Get the CRM server url from Silverlight [If the silverlight page is in a form]

private static String GetServerUrlFromCrmContext()
{
    try
    {
        // If the Silverlight is in a form, this will get the server url
        ScriptObject xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
        if (xrm != null)
        {
            ScriptObject page = (ScriptObject)xrm.GetProperty("Page");
            ScriptObject pageContext = (ScriptObject)page.GetProperty("context");

            String serverUrl = (String)pageContext.Invoke("getServerUrl");
            return serverUrl;
        }
        else
        {
            goto DevUrl;
        }
    }
    catch
    {
        goto DevUrl;
    }

    DevUrl:
    return "http://10.100.4.100:5555/MyOrganization";
}

Reference an Image Web Resource from Site Map SubArea

Adding an image to the site map can be done by two ways.

  1. Giving a image path from the icon folder
  2. Giving a path from the web resources

Giving a path from the web resources is very effective in CRM 2011, as web resources are always reside with CRM Solutions. Follow the steps given below to add an image from web resources.

  • Upload the image as a web resource to the solution. (E.g. swcmt_CommitteesSiteMapIcon24x24)

  • Publish the solution and export the un-managed solution.
  • Extract the zip file and open the “customizations.xml” file from the Visual Studio (or any other xml editor).
  • Find the “<Area>” tag that the new image needed.
  • Modify the xml as follows;

<Area Id="SWCmt" ResourceId="Area_Committee" Title="Committees" ShowGroups="true" Icon=”$webresource:swcmt_CommitteesSiteMapIcon24x24″ DescriptionResourceId=”Committee_Description”>

      
It shows in the CRM application as follows;


Adding a custom button to CRM 2011 Form

Adding a button to a CRM Form is quite challenging as we don’t have any IDE to design it. But still it’s possible using JavaScript.
Here’s the JavaScript code for that.
// Create the button, using the new_custombutton field as a container
        CreateButtonCRM5(‘swpmt_cancelddi’, ‘Cancel DDI’, ’75 px’, ’16_cancel.png’, CustomClickFunction);


// Custom Function
function CustomClickFunction() {
    var response = confirm(“DDI Status is to be changed to ‘Cancelling’. Do you want to continue?”);
    if (response) {
        Xrm.Page.getAttribute(‘swpmt_ddistatus’).setValue(4);
    }
}


// Add a button to the CRM Form – Begin


// CODE
// Create Dynamic Button for CRM 2011
function removeChildNodes(ctrl) {
    while (ctrl.childNodes[0]) {
        ctrl.removeChild(ctrl.childNodes[0]);
    }
}


function CreateButtonCRM5(fieldName, buttonText, buttonWidth, iconName, clickEvent) {
    functiontocall = clickEvent;
    crmForm.all.item(fieldName + “_c”).style.display = “none”;


    var li = document.createElement(“LI”);
    li.setAttribute(‘id’, fieldName + ‘LI’);
    li.setAttribute(‘className’, ‘ms-crm-Menu’);
    li.setAttribute(‘title’, buttonText);
    li.setAttribute(‘onclick’, functiontocall);
    li.setAttribute(‘onmousedown’, push_custom_button);
    li.setAttribute(‘onmouseup’, release_custom_button);
    li.style.width = buttonWidth;
    li.style.cursor = “hand”;
    li.style.textAlign = “center”;
    li.style.overflow = “hidden”;


    var span = document.createElement(“span”);
    span.setAttribute(‘className’, ‘ms-crm-Menu-Label’);
    span.setAttribute(‘id’, fieldName + ‘Span’);
    span.style.cursor = “hand”;
    li.appendChild(span);
    li.onmouseover = function () { span.setAttribute(‘className’, ‘ms-crm-Menu-Label-Hovered’); }
    li.onmouseout = function () { span.setAttribute(‘className’, ‘ms-crm-Menu-Label’); }


    var a = document.createElement(“a”);
    a.setAttribute(‘id’, fieldName + ‘A’);
    a.setAttribute(‘className’, ‘ms-crm-Menu-Label’);
    a.onclick = function () { return false; }
    a.setAttribute(‘target’, ‘_self’);
    a.setAttribute(‘href’, ‘javascript:onclick();’);
    a.style.cursor = “hand”;
    span.appendChild(a);


    var img = document.createElement(“img”);
    img.setAttribute(‘id’, fieldName + ‘Img’);
    img.setAttribute(‘className’, ‘ms-crm-Menu-ButtonFirst’);
    img.setAttribute(‘src’, ‘/_imgs/ico/’ + iconName);
    img.style.cursor = “hand”;


    var span2 = document.createElement(“span”);
    span2.setAttribute(‘id’, fieldName + ‘Span2’);
    span2.setAttribute(‘className’, ‘ms-crm-MenuItem-TextRTL’);
    span2.innerText = buttonText;
    span2.style.cursor = “hand”;
    a.appendChild(img);
    a.appendChild(span2);


    removeChildNodes(crmForm.all.item(fieldName + “_d”));
    crmForm.all.item(fieldName + “_d”).appendChild(li);
}


function push_custom_button() {
    window.event.srcElement.style.marginLeft = “1px”;
    window.event.srcElement.style.marginTop = “1px”;
}


function release_custom_button() {
    window.event.srcElement.style.marginLeft = “0px”;
    window.event.srcElement.style.marginTop = “0px”;
}


// Add a button to the CRM Form – End

Set CRM Date Field to Empty

I have a check box and a date field on the form. The requirement is to display the System Date in the date field when the check box is checked by the user and to clear the date field when the check box is unchecked by the user. Although the user has unchecked the check box and saved the form it displays the previously saved date by the user. I used the following code to save it.

Following code will solve the problem and will set it to Empty when it saved.

      //get the value of Reviewed by Manager checkbox
    var ReviewedManager = crmForm.all.cpms_reviewedbymanager.DataValue;

    //if Reviewed by Manager chekbox is checked
    if (ReviewedManager == true)
    {
        //set the Date Reviewed by  to system date
        crmForm.all.cpms_datereviewedbymanager.Disabled = false;
        crmForm.all.cpms_datereviewedbymanager.DataValue = new Date();
    }
    else
    {
        crmForm.all.cpms_datereviewedbymanager.DataValue = null;
        crmForm.all.cpms_datereviewedbymanager.ForceSubmit = true;
        crmForm.all.cpms_datereviewedbymanager.Disabled = true;
    }