5

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..");

    }
}
James Skemp
  • 8,018
  • 9
  • 64
  • 107
Brian Var
  • 6,029
  • 25
  • 114
  • 212

2 Answers2

13

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();
Henk van Boeijen
  • 7,357
  • 6
  • 32
  • 42
  • I tried adapting that code to mine but there is no definition for RetrieveMultiple. This is what I tried: `var userList = svcClient.RetrieveMultiple(DisabledMailBoxUsersFetchXML).Entities.Select(e => e.ToEntity());` Any ideas what the issue is there? – Brian Var Feb 09 '16 at 20:17
  • I overlooked the fact you are using the `Microsoft.Xrm.Tooling.Connector` namespace. Extended my answer. – Henk van Boeijen Feb 09 '16 at 21:10
  • Okay is the SystemUser type a model class I need to add? – Brian Var Feb 09 '16 at 22:53
  • In your code sample you are using `CRMSDKTypeProxy.SystemUser`. It is a so called "early bound" entity class that can be generated with the CrmSvcUtil.exe tool. This tool is part of the Dynamics CRM SDK. – Henk van Boeijen Feb 10 '16 at 06:10
  • Do you perhaps know if you can return an anonymous list? Something like `.Select(e => e.ToEntity()).ToList();` – VDWWD Nov 11 '20 at 16:17
  • @VDWWD, yes, you can use generics here as well. – Henk van Boeijen Nov 12 '20 at 07:36
2

@Henk van Boeijen is partially correct. Rather than use the service.Retrieve in his example replace it with your svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML) call.

var userList = svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML).Entities.Select(e => e.ToEntity<SystemUser>());

If your'e not using the early bound entities then you can simply call .ToList() on the Entity collection.

var userList = svcClient.GetEntityDataByFetchSearchEC(DisabledMailBoxUsersFetchXML).Entities.ToList();

The Entities property of the EntityCollection is a DataCollection which extends the Collection object and you can call the usual methods.

Clayton Page
  • 95
  • 1
  • 13