Monday, September 26, 2016

How to hide a section in MS CRM 2016 based on user logged in ?

Today I am blogging about how can we hide a section in MS CRM 2016 using JavaScript.

Where might this functionality be required ?

·         Based on user logged-in, we may not want user to see all details.
·         Based on some selection of check box or some field value, we may require to hide a section altogether.


Understanding MS CRM Page different controls and Hierarchy


The MS CRM Page object is available in forms by referencing Xrm.Page
The Page Object is having two important part.

  • Xrm.Page.data provides methods to work with the form. You can refresh the data in the form and save the form asynchronously.
  • Xrm.Page.ui contains properties and methods to retrieve information about the user interface as well as collections for several subcomponents of the form.


Few of the ui components are :

Xrm.Page.ui.quickForms
A quick view control is a quick view form added to a main form in Microsoft Dynamics CRM that enables you to view information about a related entity record within the main form.

Xrm.Page.ui.tabs         
A tab is a group of sections on a page.

Xrm.Page.ui.process
It provides methods to interact with the business process flow control in a form.

Xrm.Page.ui.navigation
It have items represents one of the available options available in  the navigation bar of an entity.

Xrm.Page.ui.tabs.get("tab_name").sections
A section partition various items of the form within a specific tab. It can be called as a container of all  fields or an attribute of an entity.

For more details about ui component of Microsoft Dynamic CRM, you can refer to MSDN article here
Code to hide a section for a given bunch of CRM users ?


// Obtain the User Id from context of the page.
var userId = window.parent.Xrm.Page.context.getUserId();

// Check for user Id match
if (userId == "{3220927E-0205-DD11-93BA-0018FE304530}"     // User – Vipin Jaiswal
   || userId == "{C6928F30-8B5A-E611-80EE-00155DCFC14C}"   // User – Heena Gupta
   || userId == "{74EFA877-0205-DD11-93BA-0018FE304530}"   // User – Pradnya Apte
   || userId == "{5744B501-74F2-E311-BFDF-000C295A41AA}"   // User – Mohan Sony
   )
{           
// Use setVisible(true) to DISPLAY section
Xrm.Page.ui.tabs.get("tab_name").sections.get("section_name").setVisible(true);
}
else
{
// Use setVisible(false) to HIDE section
Xrm.Page.ui.tabs.get("tab_name").sections.get("section_name").setVisible(false);
}


Note :
How to obtain Guid of User

Just open a CRM User record in new tab of browser and from the url take out the guid.

An Example

etc=8&extraqs=&histKey=68878144&id=%7b3220927E-0205-DD11-93BA-0018FE304530%7d
&newWindow=true&pagetype=entityrecord&sitemappath=Settings%7cSystem_Setting%7cnav_security

Within %7b ---  and %7d in url, unique guid is referred internally for CRM reference.


How to obtain section and tab name used in code above

Open the form in edit mode and select any tab or section and click on its properties.























No comments: