javascriptdynamics-crmmicrosoft-dynamicsdynamics-crm-365xrm

Set form read only using Xrm Object on Dynamics 365 CRM


I am reading about Xrm object on Microsoft Documentation but I can't find something concrete.

I need to restrict some roles of creating or editing a portal comment (type of activity). Therefore I can't just simply modify security roles.

I see when I edit the portal comment form that there is a JavaScript script which executes on Page load:

Page load script

When editing this it uses a lot this Xrm object.

In pseudocode I should do

if(ActiveUser.hasRoles(["Some role", "Some other role"]) {
    Page.setReadOnly(true);
}

I already have a script (as a web resource) to check the roles like this but I don't know how to make the form "read only" or make this with Xrm only. Any clues how to achieve this here?

Edit 1:

I was able to do this by using the following code:

// Ribbon "Save" button
document.querySelector('#crmRibbonManager').style.display = "none";
// Status "Save" button
document.querySelector('#savefooter_statuscontrol').style.display = "none";
Xrm.Page.ui.controls.get().forEach(function (control) {          
    if(!control.getDisabled()) {
        control.setDisabled(true);
    }
});

Of course I get Page Load error since the document is not loaded yet. But I am pretty sure I could use some Xrm technique to disable these attributes, I have to study that.


Solution

  • You are on right track. Like you said, create/modify your js library web resource & register on form load rather than editing the existing OOB adx form scripting js file. In your js code - check the current user's security roles.

    Xrm.Page.context.getUserRoles();
    

    Then disable the fields in bulk.

    Xrm.Page.getControl("myfield").setDisabled(true);
    

    Dynamics 365 CRM platform has two forms namely Read-only & Disabled, former is loaded when user has only Read privilege for that particular entity & latter rendered for Inactive records. Unfortunately we cannot force to load of any of these forms.