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)

How to check if Logged-in User is in a Team or not.?

(0) ShareShare
ReportReport
Posted on by

I have a button APPROVE in Deal Entity. And a subgrid named TEAMMEMBERS in Deal Entity.

On Load of Form i need to check if the logged in user is in Team or not and then show/hide that button. How can i achieve this through Webapi..

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    There is N:N relationship between team and users. Below link may help you to achieve your task.

    community.dynamics.com/.../checking-if-a-user-is-a-member-of-a-team-using-javascript

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

    Hi,

    Try with this -

    function checkTeam() {

    var result = true;

    var teamName = 'System Administrator';

    var clientURL = Xrm.Page.context.getClientUrl();

    var req = new XMLHttpRequest();

    var userId = Xrm.Page.context.getUserId().substr(1, 36);

    var query = "/api/data/v8.2/systemusers(" + userId + ")?$expand=teammembership_association($select=name;$filter=name eq '" + teamName + "')";

    req.open("GET", encodeURI(clientURL + query), false);  

    req.setRequestHeader("Accept", "application/json");

    req.setRequestHeader("Content-Type", "application/json;charset=utf-8");

    req.setRequestHeader("OData-MaxVersion", "4.0");

    req.setRequestHeader("OData-Version", "4.0");

    req.send();

    if (req.status == 200) {

    var data = JSON.parse(req.response).teammembership_association;

    if (data.length > 0)

    {

    result = true;

    return result;

    } else {

    result = false;

    return result;

    }

    };

    return result;

    }

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    Please use CRM Rest Builder to build the WEB API request. This is the easiest way to build the query as well as execute it and see the results.

    carldesouza.com/dynamics-crm-rest-builder

    You can download the tool here- github.com/.../releases

    Hope this helps.

  • Suggested answer
    Nithya Gopinath Profile Picture
    17,078 on at

    Hi,

    You can use the JS code below with the $expand in Web API and register this function in Custom Javascript enable rule of the button.

    function IsTeamMember() {
        var currentLoggedInUserId = Xrm.Page.context.getUserId().replace("{", "").replace("}","");
        var teamId = "06f352df-1db9-e811-a968-000d3af27cc7"; //Replace with the your Team ID
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v9.0/teams(" + teamId + ")?$expand=teammembership_association($filter=systemuserid eq " + currentLoggedInUserId + ")", false);
        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=\"OData.Community.Display.V1.FormattedValue\"");
        req.setRequestHeader("Prefer", "odata.maxpagesize=10");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    debugger;
                    var results = JSON.parse(this.response);
                    if (results.teammembership_association.length > 0)
                        return true;
                    else
                        return false;
                }
                else {
                    alert(this.statusText);
                }
            }
        };
        req.send();
    }

    Hope this helps.

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