Using RetrievePrincipalAccessRequest in CRM

Recently had a requirement to show\hide a ribbon button based on the condition i.e. if a user has Write access to that record show it else hide it.

Here either the user can have Write access to the record through the one of the security roles assigned to him or through the team which has that rights.

In this case we can use RetrievePrincipalAccessRequest which returns as response all the right user had on that particular record either through his own security roles or teams he is part of.

For e.g. we give following rights on a particular record to a team (which the user is part of)

If we check the rights user has on the record using RetrievePrincipalAccessRequest we get the following response (combination of all the rights either through sharing or his own security roles)

[

RetrievePrincipalAccessRequest request = new RetrievePrincipalAccessRequest();
// system user or team
request.Principal = new EntityReference("systemuser", new Guid("EBEECB5F-2D60-E511-80F8-3863BB357FC0"));
// record for which we want to check the access
request.Target = new EntityReference("is_productionsite", new Guid("15734DF9-31DD-E511-810E-3863BB353ED0"));
RetrievePrincipalAccessResponse resp = (RetrievePrincipalAccessResponse)orgService.Execute(request);