SBX - Search With Button

SBX - Forum Post Title

Security Roles

Microsoft Dynamics CRM Forum

Shakti Singh Rajput asked a question on 14 Jun 2018 1:59 AM
My Badges

Question Status

Verified

How can i assign the security roles of any fields, section, tab and form of any entity according to specific users?

Reply
Mick_4711 responded on 14 Jun 2018 3:28 AM
My Badges

You can Enable Security Roles for Dashboards and Forms and make it visible for certain users with the given roles.

Concerning Field Security Profiles you could read these articles: https://crmbook.powerobjects.com/system-administration/business-administration/field-security-profiles/ or https://docs.microsoft.com/en-us/dynamics365/customer-engagement/admin/set-up-security-permissions-field 

Reply
Sachith Vidanage responded on 14 Jun 2018 3:38 AM
My Badges
Suggested Answer

You can assign security roles to forms; for fields you can use field security profiles; but if you want to show and hide tabs or section on forms then you will have to use JavaScript. You will have to get the current logged in user's security roles and based on that you can show/hide sections.

Reply
Mahender responded on 14 Jun 2018 4:20 AM
My Badges
Suggested Answer

Hello,

Referring SDK should be helpful :technet.microsoft.com/.../hh699698.aspx

Reply
Goutam Das responded on 14 Jun 2018 5:21 AM
Suggested Answer

Hi Shakti,

Please find my answers-

Field Security : -

We can secured field only  using field security profile .There is no relation between security role and Field Security profile  both are  having different concept , only similarity is that we can assign user and team both security role and field security profile. When you create any field you will get options called Enable Field Security , if you enable this that means by default when you create any field security profile you can see the field over there and provide specific permission (Read,Write ...)to the field security profile. To make work field security profile you should have entity level access to the user assigned security role.

Tab/Section Security: -

Coming to your second questions , there is no OOB functionality to enable security on tab , section ,however you can write custom java-script code to get user security role and make then show/hide.

Form Security :-

Yes it is possible you can implement Role Based Forms  using assigning  specific security role to the forms .

Hope this helps.

Reply
Shakti Singh Rajput responded on 14 Jun 2018 6:11 AM
My Badges

Thanks Das,

Could you please provide me any sample code to hide or show the tab/section of any entity?

Reply
Shahbaaz Ansari responded on 14 Jun 2018 7:09 AM
Suggested Answer

Hi Shakti,

I have requirement where i wanted to hide tab on security role of users, so i used javascript code for this,

Below is the full code.

function CheckUserRole() {
	var currentUserId = Xrm.Page.context.getUserId();
	var currentUserRoles = getCurrentUserRoles(currentUserId);
}

// Get Roles of User only
function getCurrentUserRoles(currentUserId){
	var userId = currentUserId.slice(1, -1);
	var req = new XMLHttpRequest();
	req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemuserrolescollection?$select=roleid&$filter=systemuserid eq "+userId+"", true);
	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=\"*\"");
	req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
			debugger;
            var results = JSON.parse(this.response);
			for (var i = 0; i < results.value.length; i++) {
                var userRoleId = results.value[i].roleid
				if(userRoleId=="3e4d417d-3a43-xxxx-xxxx-3863bb353c20")
				{				
					Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true);
					Xrm.Page.ui.tabs.get("tab_4").setVisible(true);	
					Xrm.Page.ui.tabs.get("urstab").setVisible(true);
					return false;					
				}
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}

Hope this helps!!!

Best regards,

Shahbaaz

Reply
Shakti Singh Rajput responded on 14 Jun 2018 7:34 AM
My Badges

Thanks Shahbaaz,

Could you please tell me the implementation process of this above code?

Reply
Shahbaaz Ansari responded on 14 Jun 2018 7:40 AM
Suggested Answer

Please copy the above code and add it to java script web resource, add that web resource on your entity, add CheckUserRole function on page load, and check.

Also change the tab name in below code

if(userRoleId=="3e4d417d-3a43-xxxx-xxxx-3863bb353c20")// check for which Role Id you want to hide tab(Role id you will get from the setting-->security role)
				{				
					Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true);// provide the tab name, that you want to hide
					Xrm.Page.ui.tabs.get("tab_4").setVisible(true);	// provide the tab name, that you want to hide
					Xrm.Page.ui.tabs.get("urstab").setVisible(true);// provide the tab name, that you want to hide
					return false;					
				}
Reply
Shakti Singh Rajput responded on 15 Jun 2018 1:40 AM
My Badges

Thanks Shahbaaz,

And if i want to show the tab or section again then what can i do?

Reply
Shahbaaz Ansari responded on 15 Jun 2018 1:52 AM
Suggested Answer

if(userRoleId=="3e4d417d-3a43-xxxx-xxxx-3863bb353c20")// check for which Role Id you want to hide tab(Role id you will get from the setting-->security role)

{

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true);// provide the tab name, that you want to show

Xrm.Page.ui.tabs.get("tab_4").setVisible(true); // provide the tab name, that you want to show

Xrm.Page.ui.tabs.get("urstab").setVisible(true);// provide the tab name, that you want to show

return false;

}

else{

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(false);// provide the tab name, that you want to hide

}

Reply
Shahbaaz Ansari responded on 15 Jun 2018 1:54 AM
Verified Answer

False to hide and true to show the tab

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(false)// to hide

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true)// to show

Reply
Shakti Singh Rajput responded on 15 Jun 2018 1:58 AM
My Badges

Thanks Shahbaaz,

Would you please tell me that when i make a trial account in D365 then default role is system administrator or else?

Reply
Shahbaaz Ansari responded on 15 Jun 2018 2:01 AM
Suggested Answer

It will be global admin + system admin who create the trial account.

Also you can create new users and assign them roles you want from setting

Regards,

Shahbaaz

Reply
Shakti Singh Rajput responded on 15 Jun 2018 3:22 AM
My Badges

When i apply the code it gives an script error and screenshot is attached above then now how can i remove this error? 

Reply
Shakti Singh Rajput responded on 15 Jun 2018 3:30 AM
My Badges

What is the use of slice function in userId var?

Reply
Shahbaaz Ansari responded on 15 Jun 2018 1:54 AM
Verified Answer

False to hide and true to show the tab

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(false)// to hide

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true)// to show

Reply
Sachith Vidanage responded on 14 Jun 2018 3:38 AM
My Badges
Suggested Answer

You can assign security roles to forms; for fields you can use field security profiles; but if you want to show and hide tabs or section on forms then you will have to use JavaScript. You will have to get the current logged in user's security roles and based on that you can show/hide sections.

Reply
Mahender responded on 14 Jun 2018 4:20 AM
My Badges
Suggested Answer

Hello,

Referring SDK should be helpful :technet.microsoft.com/.../hh699698.aspx

Reply
Goutam Das responded on 14 Jun 2018 5:21 AM
Suggested Answer

Hi Shakti,

Please find my answers-

Field Security : -

We can secured field only  using field security profile .There is no relation between security role and Field Security profile  both are  having different concept , only similarity is that we can assign user and team both security role and field security profile. When you create any field you will get options called Enable Field Security , if you enable this that means by default when you create any field security profile you can see the field over there and provide specific permission (Read,Write ...)to the field security profile. To make work field security profile you should have entity level access to the user assigned security role.

Tab/Section Security: -

Coming to your second questions , there is no OOB functionality to enable security on tab , section ,however you can write custom java-script code to get user security role and make then show/hide.

Form Security :-

Yes it is possible you can implement Role Based Forms  using assigning  specific security role to the forms .

Hope this helps.

Reply
Shahbaaz Ansari responded on 14 Jun 2018 7:09 AM
Suggested Answer

Hi Shakti,

I have requirement where i wanted to hide tab on security role of users, so i used javascript code for this,

Below is the full code.

function CheckUserRole() {
	var currentUserId = Xrm.Page.context.getUserId();
	var currentUserRoles = getCurrentUserRoles(currentUserId);
}

// Get Roles of User only
function getCurrentUserRoles(currentUserId){
	var userId = currentUserId.slice(1, -1);
	var req = new XMLHttpRequest();
	req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/systemuserrolescollection?$select=roleid&$filter=systemuserid eq "+userId+"", true);
	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=\"*\"");
	req.onreadystatechange = function() {
    if (this.readyState === 4) {
        req.onreadystatechange = null;
        if (this.status === 200) {
			debugger;
            var results = JSON.parse(this.response);
			for (var i = 0; i < results.value.length; i++) {
                var userRoleId = results.value[i].roleid
				if(userRoleId=="3e4d417d-3a43-xxxx-xxxx-3863bb353c20")
				{				
					Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true);
					Xrm.Page.ui.tabs.get("tab_4").setVisible(true);	
					Xrm.Page.ui.tabs.get("urstab").setVisible(true);
					return false;					
				}
            }
        } else {
            Xrm.Utility.alertDialog(this.statusText);
        }
    }
};
req.send();
}

Hope this helps!!!

Best regards,

Shahbaaz

Reply
Shahbaaz Ansari responded on 14 Jun 2018 7:40 AM
Suggested Answer

Please copy the above code and add it to java script web resource, add that web resource on your entity, add CheckUserRole function on page load, and check.

Also change the tab name in below code

if(userRoleId=="3e4d417d-3a43-xxxx-xxxx-3863bb353c20")// check for which Role Id you want to hide tab(Role id you will get from the setting-->security role)
				{				
					Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true);// provide the tab name, that you want to hide
					Xrm.Page.ui.tabs.get("tab_4").setVisible(true);	// provide the tab name, that you want to hide
					Xrm.Page.ui.tabs.get("urstab").setVisible(true);// provide the tab name, that you want to hide
					return false;					
				}
Reply
Shahbaaz Ansari responded on 15 Jun 2018 1:52 AM
Suggested Answer

if(userRoleId=="3e4d417d-3a43-xxxx-xxxx-3863bb353c20")// check for which Role Id you want to hide tab(Role id you will get from the setting-->security role)

{

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true);// provide the tab name, that you want to show

Xrm.Page.ui.tabs.get("tab_4").setVisible(true); // provide the tab name, that you want to show

Xrm.Page.ui.tabs.get("urstab").setVisible(true);// provide the tab name, that you want to show

return false;

}

else{

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(false);// provide the tab name, that you want to hide

}

Reply
Shahbaaz Ansari responded on 15 Jun 2018 2:01 AM
Suggested Answer

It will be global admin + system admin who create the trial account.

Also you can create new users and assign them roles you want from setting

Regards,

Shahbaaz

Reply
Shahbaaz Ansari responded on 15 Jun 2018 4:37 AM
Suggested Answer

Please check this link for remove {} from guide, it will solve your problem

community.dynamics.com/.../242420

Reply
Shahbaaz Ansari responded on 15 Jun 2018 5:44 AM
Suggested Answer

You have to use the same code just you need to check in if and else condition for the security role and hide show the tab accordingly

False to hide and true to show the tab

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(false)// to hide

Xrm.Page.ui.tabs.get("DETAILS_TAB").setVisible(true)// to show

Reply
Shahbaaz Ansari responded on 15 Jun 2018 5:58 AM
Suggested Answer

Try with the below code for hiding section

Show a section

Xrm.Page.ui.tabs.get(“tabname”).sections.get(“sectioname”).setVisible(true);

Hide a section

Xrm.Page.ui.tabs.get(“tabname”).sections.get(“sectioname”).setVisible(false);

Reply

SBX - Two Col Forum

SBX - Migrated JS