I'm working on MS CRM plugin, and it should be able to determine whether the current user has write access to the current entity. I don't know how to approach this task.
It seems that the most user-friendly way accomplish this task is currently unsupported.
Is there any alternative in MS CRM 2011 SDK, except composing a FetchXML query and parsing its output?
Here is what I have come up with — this code will check, does current user has given privilege on current record:
// Requesting user's access rights to current record
var principalAccessRequest = new RetrievePrincipalAccessRequest
{
Principal = new EntityReference("systemuser", localContext.PluginExecutionContext.UserId),
Target = new EntityReference(localContext.PluginExecutionContext.PrimaryEntityName, localContext.PluginExecutionContext.PrimaryEntityId)
};
// Response will contain AccessRights mask, like AccessRights.WriteAccess | AccessRights.ReadAccess | ...
var principalAccessResponse = (RetrievePrincipalAccessResponse)localContext.OrganizationService.Execute(principalAccessRequest);
if ((principalAccessResponse.AccessRights & AccessRights.WriteAccess) != AccessRights.None)
{
...
...
...
}
The code inside if
statement will be executed if user has WriteAccess
to current record.
According to Matt's Answer:
You have just have to perform the joins and add the where clause you care about. Here is the Equivalent SQL:
SELECT Privilege.*
FROM Privilege
INNER JOIN RolePrivilege ON Privilege.PrivilegeId = RolePrivilege.PrivilegeId
INNER JOIN SystemUserRole ON SystemUserRole.RoleId = RolePrivileges.RoleId AND SystemUserRole.SystemUserId = (user's GUID)
-- WHERE Add whatever constraints on the Privilege entity that you need
You can perform this using Fetch XML, or LINQ to CRM, or Query Expressions, or even OData.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With