Archives
Trending
Support
Login
clear text
XML
Django
JavaScript
MATLAB
C
C++
C#
Python
SQL
Shell
Bash
Markdown
YAML
JSON
HTML
CSS
PHP
Java
Ruby
Go
Rust
Swift
Kotlin
Arduino
TypeScript
Perl
Autohotkey
Lua
SQF
R
Scala
Haskell
Groovy
Dart
Clojure
VB.NET
Objective-C
PowerShell
Bash
CoffeeScript
Verilog
/** * Retrieves and displays all security roles directly assigned to a user. * Works with Xrm.WebApi inside Dynamics CE. * @param {string} userId - The systemuserid (GUID) of the user. */ async function exportUserRoles(userId) { if (!userId) { console.error("❌ Please provide a userId (systemuserid)."); return; } userId = userId.replace(/[{}]/g, ""); console.log(`🔍 Fetching direct roles for userId: ${userId}`); try { // Step 1: Get all systemuserroles records for this user const linkQuery = `?$filter=systemuserid eq ${userId}`; const userRoles = await Xrm.WebApi.retrieveMultipleRecords("systemuserroles", linkQuery); if (!userRoles.entities.length) { console.warn("⚠️ No direct roles found for this user."); return []; } // Step 2: Extract role IDs const roleIds = userRoles.entities.map(r => r.roleid); // Step 3: Fetch each role's details const roles = []; for (const id of roleIds) { try { const role = await Xrm.WebApi.retrieveRecord("role", id, "?$select=name,roleid"); roles.push({ RoleName: role.name, RoleId: role.roleid }); } catch (innerError) { console.warn(`⚠️ Could not retrieve role ${id}:`, innerError.message); } } // 🧮 Step 4: Sort roles alphabetically by RoleName roles.sort((a, b) => a.RoleName.localeCompare(b.RoleName)); // Step 5: Display results console.table(roles); console.log(`✅ Total Direct Roles: ${roles.length}`); return roles; } catch (error) { console.error("❌ Error retrieving roles:", error.message || error); } }
Mark as private (unlisted)
for 30 minutes
for 6 hours
for 1 day
for 1 week
for 1 month
for 1 year
everlasting (like CCCP)