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 { }
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}));
- Build the solution