12.04.09

CRM 4 vb.net example of checking to see if a user has a specific role

Posted in Uncategorized at 4:21 pm by mmwebesa

‘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”

qe.ColumnSet =New AllColumns
   
Dim le As New LinkEntity
 le.LinkFromEntityName =“role”
le.LinkFromAttributeName =“roleid” 
le.LinkToEntityName =“systemuserroles”
le.LinkToAttributeName =“roleid”

Dim le2 As New LinkEntity

le2.LinkFromEntityName =“systemuserroles”

le2.LinkFromAttributeName =“systemuserid”

le2.LinkToEntityName =“systemuser”

le2.LinkToAttributeName =“systemuserid”

 

Dim ce As New ConditionExpression
ce.AttributeName = “systemuserid”
ce.[Operator] = ConditionOperator.Equal
ce.Values =New Object() {userid} 

le2.LinkCriteria =New FilterExpression

le2.LinkCriteria.Conditions.AddRange(New ConditionExpression() {ce})

le.LinkEntities.AddRange(New LinkEntity() {le2})
qe.LinkEntities.AddRange(New LinkEntity() {le})

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



Leave a Comment