web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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

I have the same question (0)
  • Suggested answer
    Adrian Begovich Profile Picture
    1,027 Moderator on at

    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
      
  • DivyaBharathi Profile Picture
    245 on at

    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,027 Moderator on at

    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

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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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 .

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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();
    
    
            }


  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    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();
            }


  • Parashuram  Profile Picture
    381 on at

    Thanks Gautam ...

  • Shahzeb Khan Profile Picture
    167 on at

    Hi Sir,

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

    kindly guide.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans