cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
paulsnolan
Helper IV
Helper IV

Display different navigation menu depending on type of user logged in

Hi All

 

We have a requirement whereby we want to display a different navigation menu depending upon the type of user that is logged into our portal. Looking at the Header web template there is a statement near the beginning of the template

 

    <div id="navbar" class="navbar-collapse collapse">

      {% assign primary_nav = weblinks["Default"] %}

 

Is this where I would put the logic, something like:

 

                {% if someusertype = "xyz" %}

                    {% assign primary_nav = weblinks["xyz navigation"] %}

                {% endif %}

 

                {% if someusertype = "abc" %}

                    {% assign primary_nav = weblinks["abc navigation"] %}

                {% endif %}

 

Is anything else required?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
justinburch
Microsoft
Microsoft

Hi @paulsnolan,

Without digging into the code, that looks about right. If the "user type" doesn't change during a session it should work just fine, otherwise you'll need to consider adding {% substitute %} and {% endsubstitute %} tags to bypass the header cache. Signing in and out should also reset the cache, as far as my previous experience has shown. 

View solution in original post

7 REPLIES 7
justinburch
Microsoft
Microsoft

Hi @paulsnolan,

Without digging into the code, that looks about right. If the "user type" doesn't change during a session it should work just fine, otherwise you'll need to consider adding {% substitute %} and {% endsubstitute %} tags to bypass the header cache. Signing in and out should also reset the cache, as far as my previous experience has shown. 

View solution in original post

OliverRodrigues
Super User
Super User

@paulsnolan another thing to add here is that you can access any attribute from the user (even custom attributes) via liquid, i.e.: {% if user.new_type == 1 %}

if you are using web roles to define the type, you can also filter by that: 

{% if user | has_role: "Administrators" %}




If you like this post, give a Thumbs up. Where it solved your request, Mark it as a Solution to enable other users find it.

Power Apps Portals Super User


Oliver Rodrigues


 

Hi Oliver

 

Thanks for the reply

Hi Justin

 

Thanks for the confirmation that we have the right approach

Hi Justin

 

We have got around to doing this development but have hit an issue. I have created two different weblink sets for our required navigation based on a user type.

 

To ensure they both work as expected I have hardcoded each assign primary_nav = weblinks[... in turn in the header web template with the assignment before the start of the OOB 'if primary_nav' statement

 

e.g.

 

      {% assign primary_nav = weblinks["xyz navigation"] %}

 

      {% if primary_nav %}

        <div class="navbar-right menu-bar {% if primary_nav.editable %}xrm-entity xrm-editable-adx_weblinkset{% endif %}" data-weblinks-maxdepth="2" id="navbarright">

          <ul class="nav navbar-nav weblinks" role="menubar">

            {% for link in primary_nav.weblinks %}

              {% unless forloop.first %}

 

This works fine

 

 

However, if I then place the assign inside an if statement, as in my original question, and this if is immediately before the 'if primary_nav' the navigation menu isn't displayed.

 

e.g.

 

      {% if someusertype = "xyz" %}

        {% assign primary_nav = weblinks["xyz navigation"] %}

      {% endif %}

 

      {% if someusertype = "abc" %}

        {% assign primary_nav = weblinks["abc navigation"] %}

      {% endif %}

 

      {% if primary_nav %}

        <div class="navbar-right menu-bar {% if primary_nav.editable %}xrm-entity xrm-editable-adx_weblinkset{% endif %}" data-weblinks-maxdepth="2" id="navbarright">

          <ul class="nav navbar-nav weblinks" role="menubar">

            {% for link in primary_nav.weblinks %}

              {% unless forloop.first %}

 

 

Is there a scope issue with the liquid assign within the if statement?

Oh man, wrote a full reply before realizing I wasn't signed in.. forgive any shortness here in rewriting.

I don't think it'll be a scope issue, and would likely be the statements themselves. Definitely recommend having a default style just in case, though

var primary_nav = defaultnav;
if (user['new_typecode'].value == 1)
   primary_nav = nav1;
if (user['new_typecode'].value == 2)
  primary_nav = nav2;
nav = primary_nav;

I think it's likely the issue is in the if statements. Some progressive things I'd try:

  1. Validate your field is being retrieved as you expect
    • Login to the Portal on a test page, have Liquid somewhere to wrap the value you're comparing in a <p></p> to make sure it looks like what you'd expect
    • Make sure it's a valid type; e.g., multi-choice isn't available in the Portal
  2. Validate your if statements are being hit successfully, assuming the value is correct - e.g., seeing a datetime object in <p> doesn't mean the conditions in logic will work the same against it, so put some hidden inputs or <p> statements in your if assignment blocks to see which (if any) are being covered by a given user
  3. Wrap the assignment of the variable in a substitution tag
  4. Wrap the assignment of the variable and the nav logic in the substitution tag or just turn off header/footer cache
  5. Use a workflow/process to assign/unassign appropriate web roles based on the type field, then see if the hasrole condition is more successful.. hopefully this would be a last resort

I hope this helps!

Justin

Hi Justin

 

Got this working, my bad. I had some substitution tags in the if statements plus I'd turned the header caching off. I removed the tags and turned the caching on and it worked.

 

Regards

Paul

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Users online (1,306)