When building custom logic in a Model-Driven App, you may need to retrieve the GUID or name of the currently logged-in user. This is especially useful for implementing user-specific behavior or validations.
The Model-Driven Apps Client API offers a convenient method to access user-specific details like ID and name.
Get Current User ID
To fetch the current user’s ID (GUID), use the following line of JavaScript –
const userId = Xrm.Utility.getGlobalContext().userSettings.userId
// {17B913D4-B13F-F011-8779-0022485A263C}Note
The GUID is returned with curly braces. You may need to strip them for certain operations. Use slice function in JavaScript to remove the curly braces.
Get Current User Name
Similarly, you can retrieve the display name of the current user using the following code –
const userName = Xrm.Utility.getGlobalContext().userSettings.userName // John SmithFor more user and context-related methods, check out the official documentation.