Notifications
Announcements
I am working on a Model-Driven App in Power Apps, and I need to retrieve the following information when a user logs in:
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:
Any code samples or guidance would be greatly appreciated!
In a Model-Driven App, you can access the current user’s details using the Xrm API:
Xrm
// 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); });
Now, create a record in the Users table using Xrm.WebApi.createRecord:
Users
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); } ); }
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.
In our never-ending quest to improve we are simplifying the forum hierarchy…
We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…
These are the community rock stars!
Stay up to date on forum activity by subscribing.
WarrenBelz 796 Most Valuable Professional
Michael E. Gernaey 327 Super User 2025 Season 2
Power Platform 1919 268