web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Get User Details on Lo...
Power Apps
Suggested Answer

Get User Details on Login in Model-Driven App

(0) ShareShare
ReportReport
Posted on by 109

I am working on a Model-Driven App in Power Apps, and I need to retrieve the following information when a user logs in:

  • Username
  • User Email
  • User Role

Once I retrieve this information, I need to save it into a Dataverse table called Users.

Could anyone guide me on how to achieve this using JavaScript? Specifically, I need help with:

  • Retrieving the logged-in user's details (username, email, role).
  • Storing this information into the Users table in Dataverse.

Any code samples or guidance would be greatly appreciated!

I have the same question (0)
  • Suggested answer
    Ravindra Jadhav Profile Picture
    342 Moderator on at
    To retrieve the logged-in user's details (username, email, and role) in a Model-Driven App and save them
     

    Step 1: Retrieve User Information

    In a Model-Driven App, you can access the current user’s details using the Xrm API:

     

    // Get the logged-in user ID
    var userId = Xrm.Utility.getGlobalContext().userSettings.userId;
    
    // Get the username and email
    var userName = Xrm.Utility.getGlobalContext().userSettings.userName;
    var userEmail = Xrm.Utility.getGlobalContext().userSettings.email;
    
    // Fetch user role via Web API (assuming you want to retrieve the user's role name)
    Xrm.WebApi.retrieveMultipleRecords("systemuserroles", `?$filter=systemuserid eq ${userId}`)
        .then(function (result) {
            var userRoles = result.entities.map(role => role.name); // Array of role names
            saveUserData(userName, userEmail, userRoles);
        });
    

    Step 2: Save User Data to Dataverse

    Now, create a record in the Users table using Xrm.WebApi.createRecord:

     

    function saveUserData(userName, userEmail, userRoles) {
        var data = {
            "username": userName,
            "email": userEmail,
            "role": userRoles.join(", ") // Concatenate roles into a single string if multiple
        };
    
        Xrm.WebApi.createRecord("users", data).then(
            function success(result) {
                console.log("User record created with ID: " + result.id);
            },
            function error(error) {
                console.error("Error creating user record: " + error.message);
            }
        );
    }
    
     
     
  • naidu1811 Profile Picture
    109 on at
  • Suggested answer
    Fubar Profile Picture
    17 Super User 2026 Season 1 on at
    Why do you need to capture it and store it in a Dataverse table?
     
    If you want to see if a user logged in you would usually just turn on auditing. https://learn.microsoft.com/en-us/power-platform/admin/manage-dataverse-auditing#startstop-auditing-for-an-environment-and-set-retention-policy and then either check it in the Audit log or Purview.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 358 Most Valuable Professional

#2
11manish Profile Picture

11manish 214 Super User 2026 Season 2

#3
Mohsin Ali Profile Picture

Mohsin Ali 185

Last 30 days Overall leaderboard