Bhoopathi

"Be Somebody Nobody Thought You could Be"

Tuesday, March 21

Disable lookup hyperlinks on form using JavaScript in Dynamics CRM 2011

Use following JavaScript functions to disable lookup fields hyperlinks on Header, Body, Footer sections on any CRM form
Example method to disable Owner and Primary Contact fields on Header, PrimaryContact field on Body and CreatedBy,ModifiedBy fields on Footer on Account form
Call DisableLinks() in the account form load and include other methods in Account entity JavaScript file.
 
Before applying Script
Before
After applying Script
After

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//disable Owner field on Header, Account field on Body and CreatedBy field on Footer
function DisableLinks() {
    DisableHeaderLinks("ownerid");
    DisableHeaderLinks("primarycontactid");
    DisableLookupLinks("primarycontactid");
    DisableFooterLinks("createdby");
    DisableFooterLinks("modifiedby");
}
//to disable Lookup hyderlinks
function DisableLookupLinks(lookupFieldName) {
    var lookupParentNode = document.getElementById(lookupFieldName + "_d");
    var lookupSpanNodes = lookupParentNode.getElementsByTagName("SPAN");
 
    for (var spanIndex = 0; spanIndex < lookupSpanNodes.length; spanIndex++) {
        var currentSpan = lookupSpanNodes[spanIndex];
 
        // Hide the hyperlink formatting
        currentSpan.style.textDecoration = "none";
        currentSpan.style.color = "#000000";
 
        // Revoke click functionality
        currentSpan.onclick = function () { };
    }
}
//to disable Footer hyperlinks
function DisableFooterLinks(lookupFieldName) {
    var lookupParentNode = document.getElementById("footer_" + lookupFieldName + "_d");
    var lookupSpanNodes = lookupParentNode.getElementsByTagName("SPAN");
 
    for (var spanIndex = 0; spanIndex < lookupSpanNodes.length; spanIndex++) {
        var currentSpan = lookupSpanNodes[spanIndex];
 
        // Hide the hyperlink formatting
        currentSpan.style.textDecoration = "none";
        currentSpan.style.color = "#000000";
 
        // Revoke click functionality
        currentSpan.onclick = function () { };
    }
}
 
//to disable Header hyperlinks
function DisableHeaderLinks(lookupFieldName) {
    var lookupParentNode = document.getElementById("header_" + lookupFieldName + "_d");
    var lookupSpanNodes = lookupParentNode.getElementsByTagName("SPAN");
 
    for (var spanIndex = 0; spanIndex < lookupSpanNodes.length; spanIndex++) {
        var currentSpan = lookupSpanNodes[spanIndex];
 
        // Hide the hyperlink formatting
        currentSpan.style.textDecoration = "none";
        currentSpan.style.color = "#000000";
 
        // Revoke click functionality
        currentSpan.onclick = function () { };
    }
}

Get Login User Role name(s) in JavaScript using OData in Dynamics CRM 2011

Here is the sample JavaScript code to get login user role.
Make sure you have added jquery-1.9.1.min.js and JSON2.js in Entity form libraries.If not you can download these from following URL’s
http://jquery.com/download/
http://www.json.org/js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//Check login User has 'System Administrator' role
function CheckUserRole() {
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < currentUserRoles.length; i++) {
         var userRoleId = currentUserRoles[i];
    var userRoleName = GetRoleName(userRoleId);
        if (userRoleName == "System Administrator") {
            return true;
        }
    }
    return false;
}
 
//Get Rolename based on RoleId
function GetRoleName(roleId) {
    //var serverUrl = Xrm.Page.context.getServerUrl();
    var serverUrl = location.protocol + "//" + location.host + "/" + Xrm.Page.context.getOrgUniqueName();
    var odataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$filter=RoleId eq guid'" + roleId + "'";
    var roleName = null;
    $.ajax(
        {
            type: "GET",
            async: false,
            contentType: "application/json; charset=utf-8",
            datatype: "json",
            url: odataSelect,
            beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
            success: function (data, textStatus, XmlHttpRequest) {
                roleName = data.d.results[0].Name;
            },
            error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
        }
    );
    return roleName;
}