Skip to content

Instantly share code, notes, and snippets.

@dkudelko
Created November 30, 2015 12:29
Show Gist options
  • Save dkudelko/f9773f3e401381abfcbb to your computer and use it in GitHub Desktop.
Save dkudelko/f9773f3e401381abfcbb to your computer and use it in GitHub Desktop.
CRM Plugin sample code get/check user in specific role
private static bool UserInFrozenRole(IPluginExecutionContext context, IOrganizationService organizationService, IEnumerable<string> settingRoles)
{
var jjQuery = new QueryExpression()
{
EntityName = "role",
ColumnSet = new ColumnSet("name"),
LinkEntities =
{
new LinkEntity
{
LinkFromEntityName = Role.EntityLogicalName,
LinkFromAttributeName = "roleid",
LinkToEntityName = SystemUserRoles.EntityLogicalName,
LinkToAttributeName = "roleid",
LinkCriteria = new FilterExpression
{
FilterOperator = LogicalOperator.And,
Conditions =
{
new ConditionExpression
{
AttributeName = "systemuserid",
Operator = ConditionOperator.Equal,
Values = {context.InitiatingUserId}
}
}
}
}
}
};
var userRoles = organizationService.RetrieveMultiple(jjQuery);
return userRoles.Entities.ToList().ToList().Find(r => settingRoles.Contains((string)r.Attributes["name"])) != null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment