Skip to main content

Notifications

Announcements

No record found.

Dynamics 365 Community / Forums / Sales forum / Code to redirect a Use...
Sales forum
Answered

Code to redirect a User To Specific Page After Login To Dynamics 365 Portal is not working

Posted on by 1,632
Categories:
  • bkenn003 Profile Picture
    bkenn003 10 on at
    RE: Code to redirect a User To Specific Page After Login To Dynamics 365 Portal is not working

    What if it is based on user role not the name on text?

  • cloflyMao Profile Picture
    cloflyMao 25,194 on at
    RE: Code to redirect a User To Specific Page After Login To Dynamics 365 Portal is not working

    Hi Partner,

    Try to assign user role to a temp liquid variable, then give the value to js variable.

    pastedimage1573728504338v2.png

    pastedimage1573728466791v1.png

    If above steps could work, then make modification.

    Regards,

    Clofly

  • Inkey Solutions Profile Picture
    Inkey Solutions 1,632 on at
    RE: Code to redirect a User To Specific Page After Login To Dynamics 365 Portal is not working
    Hello Clofly Mao,
    Thank you for the response. You saved my day. It worked like a charm. 
    One more question, can I check the web portal user role in the webpage using the custom script as I require to run this only for the Admin role?
  • Verified answer
    cloflyMao Profile Picture
    cloflyMao 25,194 on at
    RE: Code to redirect a User To Specific Page After Login To Dynamics 365 Portal is not working

    Hi Partner,

    Prerequisite: make sure a test code could run on Home page.

    You could try code below on your Home page.

    var username = $('.username').text();
    if (username != "") {
      if (username == "Alejandro Pomeroy12") {
        if (localStorage.getItem('status') == null) {
          localStorage.setItem('status', 'logged');
          window.location.href = "/test";
        }
      }
    } else {
      localStorage.removeItem('status');
    }

    That's because if a user sign in portal, then his/her name will display on top right corner,

    pastedimage1573634762083v1.png

    then we could grab text of the element as condition to check whether user has logged.

    Process is below:

    1.1 If guest -> do nothing(remove localstorage item)

    1.2 If user has logged out -> remove localstorage item(to make user could still be redirected if he sign in again)

    2.1 If user value not equals a specific value, do nothing.

    2.2 If user value equals a specific value, then go to next step

    3. The first time when user enters Home page, "status" item not exists, then we add the item and redirect user to page.

    4. When user navigates back to Home page, due to the item has been added, so we do nothing for him.

    5. When user logged out, then back to 1.2

    In addition, the code only works when you enter sign in page from home page because we only add it to Home web template;

    you could move it to header to test whether it could work for all pages.

    Regards,

    Clofly

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,420 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,672 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans