Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Javascript code to check Current users Team

(0) ShareShare
ReportReport
Posted on by 245

Hi i have a requirement to check the logged in users Team on click of custom button and then change the status based on Team .

Kindly let me know if any pointers.

*This post is locked for comments

  • Shahzeb Khan Profile Picture
    167 on at
    RE: Javascript code to check Current users Team

    Hi Sir,

    There is error in response when i am using this code.

    kindly guide.

  • Parashuram  Profile Picture
    381 on at
    RE: Javascript code to check Current users Team

    Thanks Gautam ...

  • Verified answer
    gdas Profile Picture
    50,089 Moderator on at
    RE: Javascript code to check Current users Team

    You can also try with single request -

            function getLoginUserTeam()
            {
              
                var loggedInUserId = Xrm.Page.context.getUserId();
                loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');
                var req = new XMLHttpRequest();
                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/teams$select=name&$expand=teammembership_association($filter=systemuserid eq "+loggedInUserId+")",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.onreadystatechange = function() {
                    if (this.readyState === 4) {
                        req.onreadystatechange = null;
                        if (this.status === 200) {
                            var results = JSON.parse(this.response);
                            // I have not tried you can debug results part 
                            for (var i = 0; i < results.value.length; i++) {
                                var teamname = results.value[i]["name"];
                                                        
                            }
                        } else {
                            Xrm.Utility.alertDialog(this.statusText);
                        }
                    }
                };
                req.send();
            }


  • Suggested answer
    gdas Profile Picture
    50,089 Moderator on at
    RE: Javascript code to check Current users Team

    Try with this -

            function getLoginUserTeam()
            {
                var UserTeams = null;
                var loggedInUserId = Xrm.Page.context.getUserId();
                loggedInUserId = loggedInUserId.replace('{', '').replace('}', '');
                var req = new XMLHttpRequest();
                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/teammemberships?$select=teamid,teammembershipid&$filter=systemuserid eq "+loggedInUserId+"", 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.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 teamid = results.value[i]["teamid"];
                                var teammembershipid = results.value[i]["teammembershipid"];
    
                                var teamid = teamid.replace("{","").replace("}","");
                                var req = new XMLHttpRequest();
                                req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/teams("+teamid+")?$select=name,teamid", 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 result = JSON.parse(this.response);
    
                                            // You will get the team id and team name here
                                            var name = result["name"]; // Team Name
                                            var teamid = result["teamid"];
    
                                            if(UserTeams == null)
                                                UserTeams  =  name;
                                            else
                                                UserTeams = UserTeams + "," +  name;
    
    
                                        } else {
                                            Xrm.Utility.alertDialog(this.statusText);
                                        }
                                    }
                                };
                                req.send();
                            }
                        } else {
                            Xrm.Utility.alertDialog(this.statusText);
                        }
                    }
                };
                req.send();
    
    
            }


  • Suggested answer
    gdas Profile Picture
    50,089 Moderator on at
    RE: Javascript code to check Current users Team

    Hi Divya ,

    Check below old post here .

    community.dynamics.com/.../218804

    In addition I would suggest to download CRMRESTBuilder and prepare same code with web api instead of using organization service .

  • DivyaBharathi Profile Picture
    245 on at
    RE: Javascript code to check Current users Team

    I haven't tried this before .it would be great if you post some sample code for the same

  • Suggested answer
    Adrian Begovich Profile Picture
    1,023 Super User 2025 Season 1 on at
    RE: Javascript code to check Current users Team

    Hi DivyaBharathi,

    It is actually not meant to be a link. I intended for it to be an example pseudocode for your JavaScript retrieve.

  • DivyaBharathi Profile Picture
    245 on at
    RE: Javascript code to check Current users Team

    Hi Adrian thank you for your quick response

    //retrieve team

    customer.crm.dynamics.com/.../teams$select=name&$expand=teammembership_association($filter=systemuserid eq id);

    this link is not accessible

  • Suggested answer
    Adrian Begovich Profile Picture
    1,023 Super User 2025 Season 1 on at
    RE: Javascript code to check Current users Team

    Hi DivyaBharathi,

    You can use JavaScript and the Ribbon Workbench for Dynamics 365 & Dynamics CRM to achieve this. This is a rough method for implementing this.

    1. Create a custom button with the Ribbon Workbench that executes custom JavaScript when clicked.
    2. In the custom JavaScript, retrieve the logged in users ID, and use this value to retrieve the users teams. The code will look something like this pseudocode.
      var id = Xrm.Page.context.getUserId(); //get ID
      
      //retrieve team
      customer.crm.dynamics.com/.../teams$select=name&$expand=teammembership_association($filter=systemuserid eq id);
      
      //change status based on team
      

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,095 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,866 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans