Get User’s Teams Using WebApi JavaScript In Dynamics 365

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:

1

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:

1


2


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!

 

Advertisement

7 thoughts on “Get User’s Teams Using WebApi JavaScript In Dynamics 365

  1. 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

    Like

    • 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.

      Like

  2. 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

    Like

    • 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.

      Like

  3. 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.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.