Question

I'm retrieving some data from CRM 2011. I have a windows form application with a service reference to my organization.

            query = new QueryByAttribute();
            query.EntityName = "myentityname";
            cset = new ColumnSet();
            cset.AllColumns = true;
            cset.Columns = new string[] { "" };
            query.ColumnSet = cset;
            query.Attributes = new string[] { "col1", "col2" };
            query.Values = new object[] { col1.value, col2.value };
            result = (EntityCollection)_service.RetrieveMultiple(query);

The "retrieveMultiple" threw the exception below :

The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter 
 http://schemas.microsoft.com/xrm/2011/Contracts/Services:RetrieveMultipleResult. The InnerException message was 'Error in line 1 position 833. 
 Element 'http://schemas.datacontract.org/2004/07/System.Collections.Generic:value' contains data from a type that maps to the name 'http://schemas.microsoft.com/xrm/2011/Contracts:OptionSetValue'. 
The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding 
to 'OptionSetValue' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'.
 Please see InnerException for more details.

I did some search online and ended up removed "cset.AllColumns=true; and replace the "" in the query.columns with one of the column names for this entity. This worked fine but then the rest of my code causes an error.

           if (result.Entities.Length > 0)
            {
                Entity myentity= new Entity();
                myentity.LogicalName = "EntityName";
                 myentity.Attributes = new AttributeCollection();
                //record exists, update
                myentity.Id = (result.Entities[0]).Id;
                myentity.Attributes.Add(new KeyValuePair<string, object>   ("myentityid", ((Entity)result.Entities[0]).Id));
                _service.Update(myentity);

            }

This time "update" method throws this exception:

     There was an error while trying to serialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:entity.
     The InnerException message was 'Type 'CRM_Populi_Integration.CRMSer.EntityReference' with data contract name 'EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts' is not expected. 
      Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. 
      Please see InnerException for more details.

I don't understand the reason behind this serielization error and I don't know how to fix it.

Note: This application was working fine on another server, I just copied and pasted the application in another server and updated the service reference and it's when these exceptions started to appear.

Can you help me with it ?

Thanks

Was it helpful?

Solution

I found these solutions on another forum and it worked for me:

Solution 1: In your reference.cs , search "class Entity", "class EntityReference"

Above these two partial classes, add these two lines [System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] [System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]

Now search for "class OrganizationRequest".

Add these lines above it:

 [System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
 [Syst em.Runtime.Serialization.KnownTypeAttribute(typeof(PrincipalAccess))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] 

Now, go to your actual code and for lookup field use like this:

  myAttColl.Add(new KeyValuePair<string,object>("parentcustomerid", new EntityReference() {Id = t.Id, LogicalName= t.LogicalName}));

Build the solution and it should work now.

Solution 2: As you might have expected, updating reference.cs is not a very good approach. 1. Create a new class with name you prefer. 2. Keep the namespace of this class similar to your Reference.cs (This is important so do not forget it) 3. Now create partial classes as below

 [System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))]
 [System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
 public partial class Entity { }


[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
public partial class EntityCollection { }


[System.Runtime.Serialization.KnownTypeAttribute(typeof(EntityReference))]
[System.Runtime.Serialization.KnownTypeAttribute(typeof(PrincipalAccess))] 
[System.Runtime.Serialization.KnownTypeAttribute(typeof(OptionSetValue))] 
public partial class OrganizationRequest { }
  1. Now go to your actual code, and for lookup field use like this:

    myAttColl.Add(new KeyValuePair("parentcustomerid", new EntityReference() {Id = t.Id, LogicalName= t.LogicalName}));

  2. Build the solution

Reference: http://www.datazx.cn/Forums/en-US/48d57f16-6bd2-4470-8059-b75a0d8cb16c/action?threadDisplayName=how-to-create-a-record-in-crm-2011-using-web-service&forum=crmdevelopment

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top