Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

getUserPrivilege at the record level?

(0) ShareShare
ReportReport
Posted on by 162

We have getUserPrivilege at the attribute level as documented formContext.getAttribute(arg).getUserPrivilege()

But we need one at the record level to see whether user can read or write or delete to the record, is there a function for that?

Many Thanks,

Ning

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: getUserPrivilege at the record level?

    Hi Ning Zhu,

    It seems you want to achieve this by JS. Please try the following code:

    function onload(executionContext){
    var formContext = executionContext.getFormContext();
    var globalContext = Xrm.Utility.getGlobalContext();
    //Get client url
    var clientUrl = globalContext.getClientUrl();
    //Get current user id
    var currentUserId = Xrm.Utility.getGlobalContext().userSettings.userId.replace(/{|}/g,'');
    //Get current record reference
    var currentRecordReference = formContext.data.entity.getEntityReference();

    //plural of logical name(Sometime you need add 'es' or remove 'y' then add 'ies')
    var logicalName = currentRecordReference.entityType + 's';
    var recordId = currentRecordReference.id.replace(/{|}/g,'');

    var target = "{'@odata.id':'" + logicalName + "(" + recordId + ")'}";

    var url = clientUrl + "/api/data/v9.2/systemusers(" + currentUserId + ")/Microsoft.Dynamics.CRM.RetrievePrincipalAccess(Target=@tid)?@tid=" + target;
    var request = new XMLHttpRequest();
    request.open("get", url , true);
    request.setRequestHeader("OData-MaxVersion", "4.0");
    request.setRequestHeader("OData-Version", "4.0");
    request.setRequestHeader("Accept", "application/json");
    request.onreadystatechange = function () {
    if (request.readyState === 4) {
    console.log(JSON.parse(request.responseText));
    }
    }
    request.send();
    }

    Result:

    Snipaste_5F00_2022_2D00_03_2D00_24_5F00_13_2D00_14_2D00_43.png

  • Amit Katariya007 Profile Picture
    Amit Katariya007 8,596 Super User 2024 Season 1 on at
    RE: getUserPrivilege at the record level?

    Hello Ning Zhu,

    Please refer below article for your requirement.

    xrmdynamicscrm.wordpress.com/.../

    Thank you,

    Amit Katariya

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

News and Announcements

Announcing Category Subscriptions!

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Verified Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,359 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,370 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans