CRM Retrieve entity - Error: Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'CRMEntities.List'
Asked Answered
R

1

7

I generated entities from CRM like this:

CrmSvcUtil.exe /url:http://<servername>/<organizationname>/XRMServices/2011/Organization.svc
    /out:<outputfilename>.cs /username:<username> /password:<password> /domain:<domainname>
    /namespace:CRMEntities /serviceContextName:XrmServiceContext

For serviceContextName I set XrmServiceContext. I want to retrieve some entity from CRM using next code:

var context = new XrmServiceContext(myorgserv);
var marketingList = context.ListSet.Where(item => item.Id == new Guid("SOME GUID"));

And I'm getting error:

Message "Unable to cast object of type 'Microsoft.Xrm.Sdk.Entity' to type 'CRMEntities.List'."

After 'add to watch' I saw that every set of entities in context have the same message. What have I missed?

Retired answered 1/3, 2012 at 15:32 Comment(0)
R
17

Problem solved. After initializing OrganizationServiceProxy, I have to call EnableProxyTypes method.

OrganizationServiceProxy orgserv;
ClientCredentials clientCreds = new ClientCredentials();

    clientCreds.Windows.ClientCredential.UserName = username;
    clientCreds.Windows.ClientCredential.Password = password;
    clientCreds.Windows.ClientCredential.Domain = domain;
    IServiceConfiguration<IOrganizationService> orgConfigInfo = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(orgServiceUri);

    orgserv = new OrganizationServiceProxy(orgConfigInfo, clientCreds);
    orgserv.EnableProxyTypes();

Key thing is: orgserv.EnableProxyTypes();

Retired answered 2/3, 2012 at 12:16 Comment(1)
Thank you! Was banging my head against the wall, works perfectly!Narton

© 2022 - 2024 — McMap. All rights reserved.