Display Current User Information on Menu Bar

I recently posted about how to add Information to Menu Bar . I have been asked on LinkedIn - "How to display Current Logged in Username here ?"
I will not say it is difficult, but involves a little more number of steps, because we have to use some extra steps to get current user details as well.
So, Lets Start !
  1. Create 2 Document Properties time and UserName . We will use time to trigger a IronPython Script to get current user details.
  2. Add script to Document Property time .
    You can use any script to get user information. I used approach from my post on LinkedIn-
    from Spotfire.Dxp.Framework.ApplicationModel import UserContext
    r=Application.GetService[UserContext]()
    Document.Properties["UserName"] = r.Username
  3. Now, create a Data Function with Simple script.
    output=input
  4. Define Output Parameter to Document Property time to trigger IronPython code from step 2.
  5. Define Input Parameter to expression below-
    String(DateTimeNow())
  6. In HTML Text Area, create a Div with Property User enclosed -
    <span id='userDet' style="visibility:hidden;">
    <SpotfireControl id="52ea70f998bb46c985e49a1d0ca03ada" />
    </span>
  7. Add JavaScript for final touch-
    if(!$('#AlienBox').length){
     $('body').append($(`<style id="AlienBox">
     #Brand {
        position: absolute;
        top: 0px;
        right: 40%;
        height: 30px;
        width: 100px;
        z-index: 99;
        border: 1px solid chocolate;
        border-radius :5px;
        color:chocolate;
    }
     </style>`, {
            id: 'AlienBox' 
       })); 
    }
    innerHTML=`<div id="Brand">
     <center> Current User is <br><b>`;
    innerHTML+=$("#userDet").text();
    innerHTML+=`</b> </center>
     </div>`;

    if(!$('#Brand').length){
     $('body').append($(innerHTML, {
            id: 'Brand' 
       })); 
    }
  8. Save the Analysis and Close.
  9. Open Analysis again to see the desired output.
I am not including any screenshot this time, Just give a try and see the results yourself. 

Share: