12.04.09
CRM 4 vb.net example of checking to see if a user has a specific role
‘This was used in a plugin hence the “ICrmService” as a parameter in the function.
‘Otherwise, “CrmService” would have been used instead.
Private Function HasTheRole(ByVal userid As Guid, ByVal service As ICrmService) As Boolean ‘CrmService) As Boolean
‘**** Obtaining the current user’s role to check if they have a specific user role
Dim hasRole = False
Dim qe As New QueryExpression
qe.EntityName =“role”
Dim le2 As New LinkEntity
le2.LinkFromEntityName =“systemuserroles”
le2.LinkFromAttributeName =“systemuserid”
le2.LinkToEntityName =“systemuser”
le2.LinkToAttributeName =“systemuserid”
le2.LinkCriteria =New FilterExpression
le2.LinkCriteria.Conditions.AddRange(New ConditionExpression() {ce})
Dim bec As BusinessEntityCollection = service.RetrieveMultiple(qe)
Dim m As Integer
For m = 0 To bec.BusinessEntities.Count – 1
Dim role As New role
role =CType(bec.BusinessEntities(m), role)
If role.name = “…theRole…” Then
hasRole =True
Exit For
End If
Next
Return hasRole
End Function