Java Script to get Teams of currently logged in user
This post is to show how you can get the Teams of currently logged in User in Dynamics CRM
Step 1: Upload Jquery.min.js file as JScript web resource on the entity form.Step 2: Upload Json2.js file as JScript web resource on the entity form.
Step 3: Create a new web resource of type JScript.
Step 4: Paste the below code in it.
function getTeamNames()
{
//ajax call to get data from CRM
$.ajax
(
{
type: "GET", //Get method is used to fetch data from Dynamics CRM
contentType: "application/json; charset=utf-8", //Type of content
datatype: "json", //Data type
async: false, //To run Script synchronously, set it false
url: "https://j44.crm5.dynamics.com/XRMServices/2011/OrganizationData.svc/TeamSet?$select=Name,TeamId", //Write odata query here
beforeSend: function (XMLHttpRequest)
{
//Specifying this header ensures that the results will be returned as JSON
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest)
{
for(var i=0;i<data.d.results.length;i++) //Traversing each team in CRM
{
alert(data.d.results[i].Name); //Showing each team name
if(Check(data.d.results[i].TeamId)) //Calling function Check to check if the user is associated with this team
{
alert("You are associated with team: " + data.d.results[i].Name);
}
}
},
error: function (XmlHttpRequest, textStatus, errorThrown)
{
if (XmlHttpRequest && XmlHttpRequest.responseText)
{
alert("Error while updating " + odataSetName + " ; Error – " + XmlHttpRequest.responseText);
}
}
}
);
}
function Check(teamid)
{
var userId=Xrm.Page.context.getUserId(); //Fetching user Id of current logged in user
$.ajax
(
{
type: "GET", //Get method is used to fetch data from Dynamics CRM
contentType: "application/json; charset=utf-8", //Type of content
datatype: "json", //Data type
async: false, //To run Script synchronously, set it false
url: "https://j44.crm5.dynamics.com/XRMServices/2011/OrganizationData.svc/TeamMembershipSet?$filter=SystemUserId eq guid'"+userId+"' and TeamId eq guid'"+teamid+"'",//Write odata query here
beforeSend: function (XMLHttpRequest)
{
//Specifying this header ensures that the results will be returned as JSON
XMLHttpRequest.setRequestHeader("Accept", "application/json");
},
success: function (data, textStatus, XmlHttpRequest)
{
return true; //Returning true if user is associated with the team
},
error: function (XmlHttpRequest, textStatus, errorThrown)
{
if (XmlHttpRequest && XmlHttpRequest.responseText)
{
alert("Error while updating " + odataSetName + " ; Error – " + XmlHttpRequest.responseText);
}
}
}
);
return false; //Returing false if the user is not associated with the team
}
Step 5: Attach the web resource wherever you want, either on the page or on any button.
Comments
Post a Comment