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 :
Customer experience | Sales, Customer Insights,...
Answered

D365 UCI version - How to obtain team name in C#

(0) ShareShare
ReportReport
Posted on by 32

I need some help, please. I am writing a C# code that should ascertain whether the logged user  is a member of a particular team, called it, Corporate Desk Team, and if yes, grant certain privileges.  I have been able to handle all aspects of the code apart from the Query Expression or FetchXML that returns the name of the team. Could someone help me with a  Query Expression or FetchXML query that can search whether a logged in user belongs to Corporate Desk Team?  Thank you in advance.

I have the same question (0)
  • Verified answer
    Charan Raju C R Profile Picture
    7 Moderator on at

    Hi,

    Please check if the below fetch XML query is helpful for your requirement.

    string fetchXML = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='true'>
     <entity name='systemuser'>
       <attribute name='fullname' />
       <attribute name='systemuserid' />
       <order attribute='fullname' descending='false' />
       <filter type='and'>
         <condition attribute='systemuserid' operator='eq-userid' />
       </filter>
       <link-entity name='teammembership' from='systemuserid' to='systemuserid' visible='false' intersect='true'>
         <link-entity name='team' from='teamid' to='teamid' alias='af'>
           <filter type='and'>
             <condition attribute='name' operator='eq' value='Corporate Desk Team' />
           </filter>
         </link-entity>
       </link-entity>
     </entity>
    </fetch>";
    EntityCollection records = crmService.RetrieveMultiple(new FetchExpression(fetchXML));

    if(records.Entities.Count > 0)
       //  Logged in user belongs to 'Corporate Desk Team'
    else
       // Logged in user doesn't belongs to 'Corporate Desk Team'

  • Verified answer
    Mauro De Biasio Profile Picture
    on at

    Hi D365 Geek,

    This is the query expression that retrieves all the users belonging to a team that you know the Guid of: 

    //Define the Entity you want to retrieve
    QueryExpression retrieveUsersBelongingToTeam = new QueryExpression() { EntityName = "systemuser" };
    //Define the columns you want to retrieve (true means all) you just need systemuser id and fullname
    retrieveUsersBelongingToTeam.ColumnSet = new ColumnSet(true);
    
    //define the entities and the field to join
    var linkedEntity = new LinkEntity();
    linkedEntity.LinkFromEntityName = "systemuser";
    linkedEntity.LinkToEntityName = "teammembership";
    linkedEntity.LinkFromAttributeName = "systemuserid";
    linkedEntity.LinkToAttributeName = "systemuserid";
    
    //define a filter to limit the number of teams
    var filterExpression  = new FilterExpression();
    filterExpression.FilterOperator = LogicalOperator.And;
    
    //define the team Guid to filter
    var condition = new ConditionExpression();
    condition.AttributeName = "teamid";
    condition.Operator = ConditionOperator.Equal;
    condition.Values.Add("Your Team guid here");
    
    //add the condition to the filter
    filterExpression.Conditions.Add(condition);
    
    //add the filter to the linked entity
    linkedEntity.LinkCriteria = filterExpression;
    
    //add the linked entity
    retrieveUsersBelongingToTeam.LinkEntities.Add(linkedEntity);
    
    EntityCollection retrieved = svc.RetrieveMultiple(retrieveUsersBelongingToTeam);
    
    //print the username that belong to the particular team
    foreach (var c in retrieved.Entities)
    {
        System.Console.WriteLine("Name: "   c.Attributes["fullname"]);
    }

    With that information you should be able to accomplish what you were after. Remember that User and Team are connected via "teammembership".

    Please consider to change your code style and switch to "Early Bound" the code will be more manageable and in general linq queries are easier to write.

    Best Regards,

    Mauro De Biasio

  • Mauro De Biasio Profile Picture
    on at

    D365 Geekcan you please mark the previous post as the answer if the code worked for you? 

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 > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 43 Most Valuable Professional

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans