Suppose you need to show the current user name in your Mendix-made application. How can you achieve this goal? There are some methods to get this result, and I’ll show you six of them in this article.
Method 1
[M1-1] Open the Domain Model, create a Non-persistent Entity called CurrentUsername, and define one String attribute called Name. Set the access rules as shown below:
Press enter or click to view image in full size
[M1-2] Create a Microflow called DS_MF_CurrentUsername_Create and add to this microflow a Create activity, defining the Name attribute as $currentUser/Name. Set the entity created as the microflow’s result:
Press enter or click to view image in full size
[M1-3] In your page, create a Dataview using the DS_MF_CurrentUsername_Create as context and add a Text combining a fixed text and a parameter pointing to the Name attribute:
Press enter or click to view image in full size
[M1–4] The final result will be like below:
Press enter or click to view image in full size
Method 2
[M2–1] Create a Nanoflow called DS_NF_CurrentUser and define a Retrieve activity to get the current User using the XPath[id='[%CurrentUser%]'] as XPath constraint. Also, get only the First result in Options/Range. Define User as nanoflow’s result:
Press enter or click to view image in full size
[M2–2] In your page, create a Dataview using the DS_NF_CurrentUser as context and add a Text combining a fixed text and a parameter pointing to the Name attribute:
Press enter or click to view image in full size
[M2–3] The final result will be like below:
Press enter or click to view image in full size
Method 3
[M3–1] Create a Javascript action called JSA_CurrentUser with no parameters that return a System.User object, as shown below:
import "mx-global"; import { Big } from "big.js";
/** * @returns {Promise.<MxObject>} */ export async function JSA_CurrentUser() { // BEGIN USER CODE return new Promise((resolve, reject) => { mx.data.get({ guid: mx.session.getUserId(), callback: resolve, error: reject }); }); // END USER CODE }
[M3–2] Create a Nanoflow called DS_NF_CurrentUser_2. Call the Javascript action created earlier and return the object retrieved as the result:
JSAction to retrieve the current user: credits to Andrej Gajduk
[M3–3] In your page, create a Dataview using the DS_NF_CurrentUser_2 as context and add a Text combining a fixed text and a parameter pointing to the Name attribute:
Press enter or click to view image in full size
[M3–4] The final result will be like below:
Press enter or click to view image in full size
Method 4
[M4–1] Install the HTML Snippet widget from the Market Place.
[M4–2] In your page, drop a Text that you want to show the user name and also the HTML Snippet, as shown below:
[M4–3] Open the HTML Snippet properties and select Javascript in Content type.
[M4–4] Then, use the javascript snippet below in Contents:
var elemUser = document.getElementsByClassName('mx-name-text15')[0]; elemUser.innerHTML = mx.session.getUserName();
[M4–5] In addition, define the additional properties as shown below:
Observe that text15 used as a parameter in the method getElementsByClassName is the Text widget’s name in which you want to show the current user name, with the prefix 'mx-name-'.
[M4–6] The final result will be like below:
Press enter or click to view image in full size
Method 5
This is a creative variation of the Method 1, in which we use the $currentUser variable to get the Current User. Why not return the current user by itself?
[M5–1] Create a Microflow called DS_MF_CurrentUser and define the variable $currentUseras return value:
[M5–2] In your page, create a Dataview using the DS_MF_CurrentUser as context and add a Text combining a fixed text and a parameter pointing to the Name attribute:
Press enter or click to view image in full size
[M5–3] The final result will be like below:
Press enter or click to view image in full size
Method 6
[M6–1] Install the Events widget from the Market Place.
[M6–2] In your page, drop a Text that you want to show the user name and also the Events widget, as shown below:
[M6–3] Create a JavaScript Action that requires a Name(String) as input. Use the code shown below:
/** * @param {string} name * @returns {Promise.<void>} */ export async function JSA_ChangeTextToCurrentUsername(name) { // BEGIN USER CODE var elemUser = document.getElementsByClassName(`mx-name-${name}`)[0]; elemUser.innerHTML = mx.session.getUserName(); // END USER CODE }
[M6–4] Create a Nanoflow called IVK_OnPageLoad that calls the JavaScript Action created earlier (with the Name parameter pointing to the name of the Text widget you want to replace with the current user name):
Press enter or click to view image in full size
[M6–5] Double-click on the Events widget to edit the properties. Change the Component load action to Call a Nanoflow, pointing to the Nanoflow we've created in step M6–4.
[M6–6] The final result, showing all methods is shown below:
Press enter or click to view image in full size
Comments & Conclusions
These are very simple methods to get the current user name. They can be easily integrated with your business logic.
I personally prefer the 4th method or the 6th method (mostly this one), as they don’t require a DataView or Object/Entity.
You can encapsulate your preferred method in a Snippet associated with a Nanoflow/Microflow and organize them in a Layout inside a style module called YourCompany_Theme.
Do you have another method to get the user name? Could you spot any flaws in the above methods? Please share in the comments!