Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Check logged in user’s web role

(0) ShareShare
ReportReport
Posted on by
Hi there, please help me with this code. I am trying to create a plugin for Dynamics 365 portal to check the logged in user’s web roles (adx_webrole) and if the user has role named “administrator” then update to the field “permissions” should be allowed otherwise it should throw an error. I am trying following code. Even if i have the administrator web role, it throws error. So i believe my code is not fetching any web roles. Please help.
 
   private bool UserHasWebRole(Guid userId, string webRoleName, IOrganizationService service)
    {
        // Query to check if the user has the specified web role
        QueryExpression query = new QueryExpression(/contact/);
        query.ColumnSet = new ColumnSet(/contactid/);
        query.Criteria.AddCondition(/contactid/, ConditionOperator.Equal, userId);
        query.LinkEntities.Add(new LinkEntity(/adx_webrole/, /adx_webrole_contact/, /adx_webroleid/, /contactid/, JoinOperator.Inner));
        query.LinkEntities[0].LinkCriteria.AddCondition(/role/, ConditionOperator.Equal, GetWebRoleId(webRoleName, service));

        EntityCollection result = service.RetrieveMultiple(query);

        // If there are records, the user has the web role
        return result.Entities.Count > 0;
    }

    private Guid GetWebRoleId(string webRoleName, IOrganizationService service)
    {
        // Retrieve the web role ID based on its name
        QueryExpression query = new QueryExpression(/adx_webrole/);
        query.ColumnSet = new ColumnSet(/adx_webroleid/);
        query.Criteria.AddCondition(/adx_name/, ConditionOperator.Equal, webRoleName);

        EntityCollection result = service.RetrieveMultiple(query);

        // Check if the web role exists
        if (result.Entities.Count > 0)
        {
            return result.Entities[0].Id;             y
        }
        else
        {
            throw new InvalidPluginExecutionException($/Web role '{webRoleName}' not found./);
        }
    }
}
 

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,759 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,468 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans