If you need to get current/logged in or of any other user’s all teams. You have to look for the N:N relationship schema name and use that association in WebApi request:
So below is the code you can use to achieve this requirement. I have just retrieving the name of the teams associated and putting an alert on Onload of a form.
I have generated the code using Rest Builder Tool . And added the filter ($filter=systemuserid eq IdOfTheuser) manually.
function GetUserTeams()
{
var userSettings = Xrm.Utility.getGlobalContext().userSettings; // userSettings is an object with user information.
var current_User_Id = userSettings.userId; // The user's unique id
var newid = current_User_Id.slice(1, -1);
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.1/teams?$select=name&$expand=teammembership_association($filter=systemuserid eq "+newid+")", true);
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++) {
var name = results.value[i]["name"];
alert(name);
}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
I am calling the function on Onload event of lead record. My user has two teams associated to it:
Important – Make sure you format the code fully before you use it. i.e use the latest method in the script i.e not using Xrm.Page but rather the formContext for v9.
I hope this helps! Cheers!
Hi Rawish, thank you for this.
Using your code I find that the alert names EVERY team, not just the teams that the current user is a member of.
Is there something I am not doing correctly with the filter?
Thanks
LikeLike
Hi
since we are providing current logged in user id it should give only teams associated with user only .
req.open(“GET”, Xrm.Page.context.getClientUrl() + “/api/data/v9.1/teams?$select=name&$expand=teammembership_association($filter=systemuserid eq “+newid+”)”, true);
did you try to debug and see, you can also try executing a fetch xml. like below:
(pass the user id.)
If you are getting all the results, I suspect the user id is not being passed property. I would recommend debugging script.
LikeLike
Hi Rawish, thank you for this.
Using your code I find that the alert names EVERY team, not just the teams that the current user is a member of.
Is there something I am not doing correctly with the filter?
Thanks
LikeLike
Hi
since we are providing current logged in user id it should give only teams associated with user only .
req.open(“GET”, Xrm.Page.context.getClientUrl() + “/api/data/v9.1/teams?$select=name&$expand=teammembership_association($filter=systemuserid eq “+newid+”)”, true);
did you try to debug and see, you can also try executing a fetch xml. like below:
(pass the user id.)
If you are getting all the results, I suspect the user id is not being passed property. I would recommend debugging script.
LikeLike
Hi
since we are providing current logged in user id it should give only teams associated with user only .
req.open(“GET”, Xrm.Page.context.getClientUrl() + “/api/data/v9.1/teams?$select=name&$expand=teammembership_association($filter=systemuserid eq “+newid+”)”, true);
did you try to debug and see, you can also try executing a fetch xml. like below:
(pass the user id.)
If you are getting all the results, I suspect the user id is not being passed property. I would recommend debugging script.
LikeLike
Hi ravish by using your code i am getting all teams but not current logged in user team please help me
LikeLike
Hi ravish kumar i am also getting all the teams i want only logged in user teams please help me
LikeLike