Posted in Code Snippets, CRM, CRM 2016, JavaScripts, OData Call

Checking User is In a Particular Team.

Checking User is In a Particular Team :

For checking this, its must need teamid and userid .

  1. Query the Team entity to get team id by passing team name.
  2. Query the Team Membership Entity by Passing Userid and Team Name. If response results greater than ‘0’ means, user is a member in that team.
function teamcheck() {
 debugger;
 var currentuserid = Xrm.Page.context.getUserId();
 var underwritingteamname = "Underwriting";
 var teamid = getTeamId(underwritingteamname);
 if (teamid != null)
 {
 var flag = checkuserinateam(teamid, currentuserid)
 if (flag == true)
 alert("User is in a Team.");
 else
 alert("User is not a Team .");
 }
}

//Getting TeamId
function getTeamId( underwritingteamname)
{
 var teamid = null;
 var req = new XMLHttpRequest();
 req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/teams?$select=name,teamid&$filter=name eq '" + underwritingteamname+"'", false);
 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) {
 var results = JSON.parse(this.response);
 for (var i = 0; i < results.value.length; i++) {
 teamid = results.value[i]["teamid"];
 }
 } else {
 Xrm.Utility.alertDialog(this.statusText);
 }
 }
 };
 req.send();
 return teamid;
}

//Check user is in a team 
function checkuserinateam(teamid, currentuserid)
{
 if (teamid != null && teamid != undefined)
 {
 var req = new XMLHttpRequest();
 req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/teammemberships?$select=systemuserid,teammembershipid&$filter=systemuserid eq '" + currentuserid + "' and teamid eq '" + teamid+"'", false);
 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)
 {
 var results = JSON.parse(this.response);
 if ( results.value.length > 0) 
 return true; 
 else 
 return false; 
 }
 else
 {
 Xrm.Utility.alertDialog(this.statusText);
 }
 }
 };
 req.send(); 
 }
}

 

That’s It. Enjoy 🙂

Advertisements