Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast an Xrm.EntityCollection to a List?

Overview:

I'm coding a FetchXML query to return users with disabled mailboxes in a Dynamics 2015 online CRM instance. Now I've come to a stage where the query results need to be bound to a ListView. (The project is using the Dynamics SDK 2015 libs.)

In order to do this I've tried to cast the returned result which is an EntityCollection -> to a list. But the CRMSDKTypeProxy can't be found in my code for the cast.

I was following this example's second answer in order to do the casting:

Convert Entity Collection to Ilist where Entity Collection does not implement IEnumerable

Question:

Does anyone know how to reference the CRMSDKTypeProxy? Or any alternative way to cast my collection to a list?

Code: (short example)

if (ctrl.CrmConnectionMgr != null && ctrl.CrmConnectionMgr.CrmSvc != null && ctrl.CrmConnectionMgr.CrmSvc.IsReady)
{
    CrmServiceClient svcClient = ctrl.CrmConnectionMgr.CrmSvc;
    if (svcClient.IsReady)
    {
        // Get data from CRM . 
        string DisabledMailBoxUsersFetchXML =
            @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                <entity name='systemuser'>
                <attribute name='fullname' />
                <attribute name='businessunitid' />
                <attribute name='title' />
                <attribute name='address1_telephone1' />
                <attribute name='positionid' />
                <attribute name='systemuserid' />
                <order attribute='fullname' descending='false' />
                <link-entity name='mailbox' from='mailboxid' to='defaultmailbox' alias='aa'>
                    <filter type='and'>
                    <condition attribute='statecode' operator='eq' value='1' />
                    </filter>
                </link-entity>
                </entity>
            </fetch>";

        var DisabledMailBoxUsersResult = svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML);

        if (DisabledMailBoxUsersResult != null)
        {
            //perform the cast here --->
            var disabledMailBoxUsersList = (from t in DisabledMailBoxUsersResult.Entities select t as CRMSDKTypeProxy.SystemUser).ToList();
            disabledMailboxUserLBx.ItemsSource = disabledMailBoxUsersList;
        }
        else
            MessageBox.Show("All user's mailboxes are approved..");

    }
}
like image 667
Brian Var Avatar asked Dec 08 '22 23:12

Brian Var


1 Answers

You can use the ToEntity<T> method to do the conversion to a strong typed entity like this: (In this snippet service is an object implementing the IOrganizationService interface and query is a QueryExpression object.)

// RetrieveMultiple will never return null, so this one-liner is safe to use.
var userList = service.RetrieveMultiple(query)
    .Entities
    .Select(e => e.ToEntity<SystemUser>())
    .ToList();

I noticed you are using the CrmServiceClient in the Microsoft.Xrm.Tooling.Connector namespace. This was introduced in Dynamics CRM 2013.

Your code could look like this:

var userList = svcClient.OrganizationServiceProxy
    .RetrieveMultiple(new FetchExpression(fetchXml))
    .Entities
    .Select(e => e.ToEntity<SystemUser>())
    .ToList();

and alternatively this should work too:

var userList = svcClient.GetEntityDataByFetchSearchEC(fetchXml)
    .Entities
    .Select(e => e.ToEntity<SystemUser>())
    .ToList();
like image 130
Henk van Boeijen Avatar answered Dec 27 '22 12:12

Henk van Boeijen