Posted in CRM, JavaScripts, OData Call

Making the Form Fields Read Only for Non Admin User

Making the Form Fields Read Only for Non Admin UsersScreenshot (29).png

function CheckUserRole()
{ debugger;
var count=0;
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”) {
count= count +1 ;
}
}
if(count==0)
{
var controls = Xrm.Page.ui.controls.get();
for (var i in controls){
var ControlType = controls[i].getControlType();
if(ControlType != “iframe” && ControlType != “webresource” && ControlType != “subgrid”)
{
controls[i].setDisabled(true);
}
}
}
}

//Getting rolename_OData Call
function GetRoleName(roles)
{
var nam= “”;
var req = new XMLHttpRequest();
req.open(“GET”, Xrm.Page.context.getClientUrl() + “/api/data/v8.0/roles?$select=name&$filter=roleid eq “+roles, false);
req.setRequestHeader(“OData-MaxVersion”, “4.0”);
req.setRequestHeader(“OData-Version”, “4.0”);
req.setRequestHeader(“Accept”, “application/json”);
req.setRequestHeader(“Content-Type”, “application/json; charset=utf-8”);
req.setRequestHeader(“Prefer”, “odata.include-annotations=\”OData.Community.Display.V1.FormattedValue\””);
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var results = JSON.parse(this.response);
nam = results.value[0].name;

}
else {
alert(this.statusText);
}
}
};
req.send();
return nam;
}

 

Note: It can applicable for the create and update or all types. If we want only for updtae or create make condition as formtype

Get from type
var type = Xrm.Page.ui.getFormType();

getFromType() function returns integer value for different Form states
0 – undefined
1 – Create
2 – Update
3 – Read Only
4 – Disabled
5 – Quick Create (Deprecated)
6 – Bulk Edit

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