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

Notifications

Announcements

Community site session details

Community site session details

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

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
    8,338 Super User 2025 Season 2 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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard