Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 general forum

How to assign Web Roles to a portal contact programmatically?

Posted on by 40

Hi,

In my current ADX portals project, on click of SigIn link the page is redirected to an external authentication provider where the users get authenticated and redirects  back to the Portal SignIn page. Now in page load event the system checks for an external cookie in the response and creates a new portal user by calling UserManager.Create method.

After creating the portal user, the system determines a web role based on some conditions and assign the web role to the user. I tried using the OOB method UserManager.AddToRole but this is not supported in ASP.Net Identity Model. Can some one suggest how to assign a web role to a portal user programmatically using OrganizationService.Associate or any other approaches.

Thanks,

Manas

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: How to assign Web Roles to a portal contact programmatically?

    Hi ManasK,

    The Run this workflow in the background (recommended) setting determines whether the workflow is a real-time or background workflow. Real-time workflows run immediately (synchronously) and background workflows run asynchronously. The configuration options available depend on your choice for this setting. Background workflows allow for wait conditions that are not available for real-time workflows. As long as you do not use those wait conditions, later you can convert background workflows to real-time workflows and real-time workflows to background workflows.

    This article describes monitoring and management of processes, including background workflows, real-time workflows and actions. You can use this information to monitor successful and failed workflow jobs.

  • ManasK Profile Picture
    ManasK 40 on at
    RE: How to assign Web Roles to a portal contact programmatically?

    Does these workflows run synchronously and how would I know whether the workflow is succeeded?

    As of now I have coded this method and it works as expected

    private void AssignWebRole(ApplicationUser user, string webRole)
            {
                using (var xrmService = new OrganizationService("Xrm"))
                {
                    QueryExpression query = new QueryExpression
                    {
                        EntityName = "adx_webrole",
                        ColumnSet = new ColumnSet("adx_name"),
                        Criteria = new FilterExpression()
                        {
                            Conditions =
                            {
                                new ConditionExpression("adx_name", ConditionOperator.Equal, webRole),
                            }
                        }
                    };
                    var role = xrmService.RetrieveMultiple(query).Entities;
    
                    xrmService.Associate("contact",
                        new Guid(user.Id),
                        new Relationship("adx_webrole_contact"),
                        new EntityReferenceCollection() { new EntityReference("adx_webrole", role[0].Id) });
                }
            }


    I understand assigning web roles through workflow is a better approach, but my only worry is what if the workflow fails and how would I get the result in the UI and will the workflow work synchronously? 

  • Suggested answer
    Adrian Begovich Profile Picture
    Adrian Begovich 21,009 Super User 2024 Season 2 on at
    RE: How to assign Web Roles to a portal contact programmatically?

    Hi ManasK,

    This is a method of automatically assigning web roles to portal users using Workflows.

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... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans