I want to check, if the user is member of a special team. If he is member of the team, I want to show some tabs. If the user is not a member of this team, I want to hide the tabs.

Use following Javascript to check, weather the current user is member of the A-Team:

function CFTabs() {

var teamName = 'A-Team';

var ClientUrl = Xrm.Page.context.getClientUrl();

 checkTeam('A-Team');

function checkTeam(teamName) {

var clientURL = Xrm.Page.context.getClientUrl();

var req = new XMLHttpRequest();

var userId = Xrm.Page.context.getUserId();

 userId = userId.replace("{", "");

 userId = userId.replace("}", "");

var query = "/api/data/v8.2/systemusers(" + userId + ")?$expand=teammembership_association($select=name;$filter=name eq '" + teamName + "')";

 req.open("GET", encodeURI(clientURL + query), false);

 req.setRequestHeader("Accept", "application/json");

 req.setRequestHeader("Content-Type", "application/json;charset=utf-8");

 req.setRequestHeader("OData-MaxVersion", "4.0");

 req.setRequestHeader("OData-Version", "4.0");

req.send();

if (req.status == 200) {

var data = JSON.parse(req.response).teammembership_association;

if (data.length > 0) {

 Xrm.Page.ui.tabs.get("TAB1").setVisible(true);

 Xrm.Page.ui.tabs.get("TAB2").setVisible(true);

 Xrm.Page.ui.tabs.get("TAB3").setVisible(true);

}

else {

 Xrm.Page.ui.tabs.get("TAB1").setVisible(false);

 Xrm.Page.ui.tabs.get("TAB2").setVisible(false);

 Xrm.Page.ui.tabs.get("TAB3").setVisible(false);

}

}

}

}

You can also use array of teams:

function CFTabs() {

var teamName = ['A-Team', 'B-Team', 'C-Team', 'D-Team'];

for (i = 0; i <= teamName.length; i++) {

var ClientUrl = Xrm.Page.context.getClientUrl();

var clientURL = Xrm.Page.context.getClientUrl();

var req = new XMLHttpRequest();

var userId = Xrm.Page.context.getUserId();

 userId = userId.replace("{", "");

 userId = userId.replace("}", "");

var query = "/api/data/v8.2/systemusers(" + userId + ")?$expand=teammembership_association($select=name;$filter=name eq '" + teamName[i] + "')";

 req.open("GET", encodeURI(clientURL + query), false);

 req.setRequestHeader("Accept", "application/json");

 req.setRequestHeader("Content-Type", "application/json;charset=utf-8");

 req.setRequestHeader("OData-MaxVersion", "4.0");

 req.setRequestHeader("OData-Version", "4.0");

req.send();

var data = JSON.parse(req.response).teammembership_association;

if (data.length > 0) {

 Xrm.Page.ui.tabs.get("TAB1").setVisible(true);

 Xrm.Page.ui.tabs.get("TAB2").setVisible(true);

 Xrm.Page.ui.tabs.get("TAB3").setVisible(true);

return;

}

else {

 Xrm.Page.ui.tabs.get("TAB1").setVisible(false);

 Xrm.Page.ui.tabs.get("TAB2").setVisible(false);

 Xrm.Page.ui.tabs.get("TAB3").setVisible(false);

}

}

}

 

Join the conversation! 2 Comments

  1. Thank you for this post. Had a requirement just like this one and the code worked perfectly! 🙂

    Like

    Reply

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 )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About Miss Ob·so·let

Imperfection is beauty, madness is genious. it is better to be absolutely ridiculous than absolutely boring.

Category

nice to know

Tags

, , , , , , , , ,