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

CRM 4 vb.net example of status description retrieval when one has just the integer id

Posted in Uncategorized at 3:40 pm by mmwebesa

‘This was used in a plugin hence the “IMetadataService” as a parameter in the function.
‘Otherwise, “MetadataService” would have been used instead.   
 Private Function GetCfstatusPicklistLabels(ByVal cfStatus As Integer, ByVal mdService As IMetadataService) As String

Dim cfStatusDesc As String = “” 

Dim cfStatusValue As Integer = 0
 Dim amd As AttributeMetadata

Dim attributeRequest As New RetrieveAttributeRequest

attributeRequest.EntityLogicalName = EntityName.appointment.ToString

attributeRequest.LogicalName = “cf_status” ‘attribute name

 Dim attributeResponse As RetrieveAttributeResponse

attributeResponse = CType(mdService.Execute(attributeRequest), RetrieveAttributeResponse)

amd = attributeResponse.AttributeMetadata
Dim pam As PicklistAttributeMetadata = CType(amd, PicklistAttributeMetadata)

 For Each pamOption As [Option] In pam.Options

cfStatusValue = cfStatus

If cfStatusValue = pamOption.Value.Value Then

cfStatusDesc = pamOption.Label.LocLabels(0).Label.ToString 
 Exit For
 End If
 Next
 Return cfStatusDesc

End Function

CRM 4 vb.net example of specific business entity retrieval

Posted in Uncategorized at 3:13 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 GetAppointmentBusinessEntity(ByVal entityID As Guid, ByVal service As ICrmService, ByVal colsArray() As String) As BusinessEntity

‘ Create the column set object that indicates the fields to be retrieved. 

Dim cols As New ColumnSet()

‘ Set the properties of the column set with the column array passed in.cols.Attributes.AddRange(colsArray)

 

‘ Create the request object. 

Dim retrieve As New RetrieveRequest()

Dim target As New TargetRetrieveAppointment()

‘ Set the properties of the target object. 

‘ EntityId is the GUID of the record being retrieved.target.EntityId = entityID

 

‘ Set the properties of the request object.retrieve.Target = target

retrieve.ColumnSet = cols

‘*** retrieves only the attributes specified in the array passed in for the target (entity). 

‘retrieve.ColumnSet = New AllColumns ‘*** retrieves all attribues for the target (entity). 

‘ Execute the request. 

Dim retrieved As RetrieveResponse = CType(service.Execute(retrieve), RetrieveResponse)

Return retrieved.BusinessEntity

‘This can also be cast to the specific entity (appointment) as follows: 

‘Dim beAppointment As Microsoft.Crm.SdkTypeProxy.appointment = DirectCast(retrieved.BusinessEntity, Microsoft.Crm.SdkTypeProxy.appointment) 

End Function

CRM 4 vb.net example of dynamic entity retrieval

Posted in Uncategorized at 2:51 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 GetDynamicEntity(ByVal entityName As String, ByVal entityID As Guid, ByVal service As ICrmService, ByVal colsArray() As String) As DynamicEntity

‘ Create the column set object that indicates the fields to be retrieved. 

Dim cols As New ColumnSet()

‘ Set the properties of the column set with the column array passed in.

cols.Attributes.AddRange(colsArray)

‘ Create the request object. 

Dim retrieve As New RetrieveRequest()

Dim target As New TargetRetrieveDynamic()

‘ Set the properties of the target object. 

‘ EntityId is the GUID of the record being retrieved.

target.EntityId = entityID

target.EntityName = entityName

‘ Set the properties of the request object.

retrieve.Target = target

retrieve.ColumnSet = cols ‘*** retrieves only the attributes specified in the array passed in for the target (entity). 

‘retrieve.ColumnSet = New AllColumns ‘*** retrieves all attribues for the target (entity).

retrieve.ReturnDynamicEntities =True

 ‘ Execute the request. 

Dim retrieved As RetrieveResponse = CType(service.Execute(retrieve), RetrieveResponse)

Return CType(retrieved.BusinessEntity, DynamicEntity)

End Function

 

 

 

 

 

 

 

 

 

 

 

 

target.EntityName = entityName

 

 

retrieve.ColumnSet = cols