Occasionally we need to conditionally take an action programmatically on a form such as hiding fields, etc. based on a user’s team instead of their role. There are plenty of JavaScript snippets floating around that allow you to check a user’s team membership if you already know the Team ID, but to my surprise there didn’t seem to be any that would both locate the team id with just the team name, or one that could handle multiple teams with the same name (don’t ask). Not complicated, but I thought I would throw one together and make it available. I’ll enhance it to add support for multiple teams, “contains”, etc. in the near future as I have time. I hope this makes CRM life a little easier for you!
// In your new function, call the “UserHasTeam("<the team name exactly")” function // sample usage, this is not part of the function: function TestUserHasTeam() { if (UserHasTeam('TestTeam1')) { alert("I'm a member if the team!"); } else { alert("I'm NOT a member of the team!"); } }
// function starts here.
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>
if (teamName != null && teamName != "") { // build endpoint URL var serverUrl = Xrm.Page.context.getServerUrl(); var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/"; // query to get the teams that match the name oDataEndpointUrl += "TeamSet?$select=Name,TeamId&$filter=Name eq '" + teamName + "'"; var service = GetRequestObject();
if (service != null) { // execute the request service.open("GET", oDataEndpointUrl, false); service.setRequestHeader("X-Requested-Width", "XMLHttpRequest"); service.setRequestHeader("Accept", "application/json,text/javascript, */*"); service.send(null); // parse the results var requestResults = eval('(' + 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)) { return true; } } } else { return false; } } } else { alert("Team with name '" + teamName + "' not found"); return false; } return false; } } else { alert("No team name passed"); return false; } }
function getUserTeams(teamToCheckId) { // gets the current users team membership var userId = Xrm.Page.context.getUserId().substr(1, 36); var serverUrl = Xrm.Page.context.getServerUrl(); var oDataEndpointUrl = serverUrl + "/XRMServices/2011/OrganizationData.svc/"; oDataEndpointUrl += "TeamMembershipSet?$filter=SystemUserId eq guid' " + userId + " ' and TeamId eq guid' " + teamToCheckId + " '";
var service = GetRequestObject();
if (service != null) { service.open("GET", oDataEndpointUrl, false); service.setRequestHeader("X-Requested-Width", "XMLHttpRequest"); service.setRequestHeader("Accept", "application/json,text/javascript, */*"); service.send(null);
var requestResults = eval('(' + service.responseText + ')').d;
if (requestResults != null && requestResults.results.length > 0) { return requestResults.results; } } }
function GetRequestObject() {
if (window.XMLHttpRequest) { return new window.XMLHttpRequest; } else { try { return new ActiveXObject("MSXML2.XMLHTTP.3.0"); } catch (ex) { return null; } } } 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; } By: John Voorhis - New Jersey Microsoft Dynamics CRM partner