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.

Answers:

Answer

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.

Answer

To complement Arun's answer;

This type of stuff is unsupported; document.querySelector('#crmRibbonManager')

Microsoft Dynamics 365 and the importance of staying supported

All JavaScript interactions within the application pages must only be performed using functions defined in Xrm.Page & Xrm.Utility namespaces, i.e. don’t directly interact with the page DOM.

If you want to control ribbon behaviours you should Customize commands and the ribbon, you will need to add an enable/display rule. Ribbon Workbench is a great tool for this.

Finally, it's worth nothing that this only controls client side validation. If you want server side rules the user can't avoid, you should look into implementing a plugin.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.