Method to check if the user belongs to X team, looking by name:
You can call it this way:
if(UserhasTeam("Sales Managers")){
// Do something
}else{}
function UserHasTeam(teamName) {
///<summary>
/// Checks to see if the current user is a member of a team with the passed in name.
///</summary>
///<param name="teamName" type="String">
/// A String representing the name of the team to check if the user is a member of.
///</param>
// build endpoint URL
var serverUrl = Xrm.Page.context.getClientUrl();
var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
var result = false;
// query to get the teams that match the name
oDataEndpointUrl += "TeamSet?$select=Name,TeamId&$filter=Name eq '" + teamName + "'";
//var service = GetRequestObject();
var service = new XMLHttpRequest();
service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("Accept", "application/json");
service.setRequestHeader("Content-Type", "application/json;charset=utf-8");
service.onreadystatechange = function() {
if (service.readyState == 4) {
if (service.status == 200) {
debugger;
var requestResults = JSON.parse(service.responseText).d;
if (requestResults != null && requestResults.results.length > 0) {
var teamCounter;
// iterate through all of the matching teams, checking to see if the current user has a membership
for (teamCounter = 0; teamCounter < requestResults.results.length; teamCounter++) {
var team = requestResults.results[teamCounter];
var teamId = team.TeamId;
// get current user teams
var currentUserTeams = getUserTeams(teamId);
// Check whether current user teams matches the target team
if (currentUserTeams != null) {
for (var i = 0; i < currentUserTeams.length; i++) {
var userTeam = currentUserTeams[i];
// check to see if the team guid matches the user team membership id
if (GuidsAreEqual(userTeam.TeamId, teamId)) {
result = true;
}
}
} else {
result = false;
}
}
} else {
alert("Team with name '" + teamName + "' not found");
result = false;
}
}
}
};
service.send();
return result;
}
function getUserTeams(teamToCheckId) {
// gets the current users team membership
var userId = Xrm.Page.context.getUserId().substr(1, 36);
var serverUrl = Xrm.Page.context.getClientUrl();
var result;
var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/";
oDataEndpointUrl += "TeamMembershipSet?$filter=SystemUserId eq guid' " + userId + " ' and TeamId eq guid' " + teamToCheckId + " '";
var service = new XMLHttpRequest();
service.open("GET", oDataEndpointUrl, false);
service.setRequestHeader("Accept", "application/json");
service.setRequestHeader("Content-Type", "application/json;charset=utf-8");
service.onreadystatechange = function() {
if (service.readyState == 4) {
if (service.status == 200) {
// get user teams
debugger;
var requestResults = JSON.parse(service.responseText).d;
if (requestResults != null && requestResults.results.length > 0) {
result = requestResults.results;
}
}
}
}
service.send();
return result;
}
function GuidsAreEqual(guid1, guid2) {
// compares two guids
var isEqual = false;
if (guid1 == null || guid2 == null) {
isEqual = false;
} else {
isEqual = (guid1.replace(/[{}]/g, "").toLowerCase() == guid2.replace(/[{}]/g, "").toLowerCase());
}
return isEqual;
}