Retrieve Personal Setting Data of LoggedIn User in Dynamics 365 CRM Using Js.


There can be scenarios where we need to get the logged-in User personal Settings data from CRM and use it in some requirements. Microsoft has exposed a UserSetting Entity WebAPI which we can use for this purpose.

Below is the msdyn link for more info.

https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/entities/usersettings

Here is a small example of script code which I have used to get the personal setting data with few values using javascript. It can be helpful.

Capture 3

function checkUserDefaultHomePage() {
    var currentUser = Xrm.Utility.getGlobalContext().userSettings.userId;

    Xrm.WebApi.retrieveRecord("usersettingscollection", currentUser, "?$select=homepagearea,homepagesubarea").then(
        function success(result) {
            //homepagesubarea = Default Tab.
            //homepagearea= Default Pane.
            console.log("Retrieved values: Name: " + result.homepagesubarea + ", Revenue: " + result.homepagearea);
            if (result.homepagesubarea == "nav_conts")//contacts
            {
                //your line of code
            }
            else
                return false;
        },
        function (error) {
            console.log(error.message);
            return false;
            // handle error conditions
        }
    );
}

Get Logged-In User Security Roles

//This returns the collection of GUID (GUID of security roles assigned to this user)
 
var userRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;

for (var i = 0; i < userRoles.length; i++) {
//you can parse all the GUID in the collection and write your code as per requirement
}
return false;

Thanks. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s