Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

How to retrieve user roles programatically?

(0) ShareShare
ReportReport
Posted on by 310

Hi all,

I am trying to retrieve user roles programatically i have seen some example but i am not able to understand, My requirement is to retrieve all security roles programatically provided sytemuser as criteria. There is N:N relation between roles and systemuser and i am trying using QueryExpression but i am not able to make the query using QueryExpression(Not FetchXML). below is my code. Anyone know please share the code. 

QueryExpression query = new QueryExpression("roles");
query.ColumnSet = new ColumnSet("fullname","internalemailaddress");
LinkEntity link = new LinkEntity();
link.LinkFromEntityName = "systemuserroles";
link.LinkToEntityName = "roles";
link.LinkFromAttributeName = "systemuserid";
link.LinkToAttributeName = "systemuserid";

Thanks in Advance!
Ishan Sharma

  • Ishan Sharma Profile Picture
    Ishan Sharma 310 on at
    RE: How to retrieve user roles programatically?

    Thanks. Your  code  is  working. I  have  marked  it  verified

  • Verified answer
    gdas Profile Picture
    gdas 50,089 on at
    RE: How to retrieve user roles programatically?

    Hi Ishan,

    Try with this -

                    Guid userid = new Guid("55A0BCC3-6DC2-4B75-B00F-200B2C7A8EF6");
                    QueryExpression qe = new QueryExpression("systemuserroles");
                    qe.ColumnSet.AddColumns("systemuserid");
                    qe.Criteria.AddCondition("systemuserid", ConditionOperator.Equal, userid);
    
                    LinkEntity link1 = qe.AddLink("systemuser", "systemuserid", "systemuserid", JoinOperator.Inner);
                    link1.Columns.AddColumns("fullname","internalemailaddress");
                    LinkEntity link = qe.AddLink("role", "roleid", "roleid", JoinOperator.Inner);
                    link.Columns.AddColumns("roleid","name");
                    EntityCollection results = organizationService.RetrieveMultiple(qe); 
                    foreach (Entity Userrole in results.Entities)
                    {
             
                        Console.WriteLine("User Name : - " + (Userrole.Attributes["systemuser1.fullname"] as AliasedValue).Value.ToString());
                        Console.WriteLine("Email : - " + (Userrole.Attributes["systemuser1.internalemailaddress"] as AliasedValue).Value.ToString());
                        Console.WriteLine("RoleId : - " + (Userrole.Attributes["role2.roleid"] as AliasedValue).Value.ToString());
                        Console.WriteLine("Role Name : - " + (Userrole.Attributes["role2.name"] as AliasedValue).Value.ToString());                    
    
                    }

    8741.role.PNG

    Refer my blogs for similar requirement -

    https://goutamdascrm.wordpress.com/2019/01/26/retrieve-user-assigned-roles-with-user-information-using-query-expression-c-in-dynamics-365/

    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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,494 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,305 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans