Creating records using CRM 2011 programming reference RRS feed

  • Question

  • Hi everyone,

    What is the best way to programmatically create records in CRM for entities with custom attributes in a console app? I succeeded in doing this once using the Entity class. I am attempting to create records in CRM by reading data from a mySql database. The code below works until I start trying to enter data into custom attribute fields.

            public static void Main(string[] args)
                Uri organizationUri = new Uri("http://servername/org/XRMServices/2011/Organization.svc"); 
                Uri homeRealmUri = null; 
                ClientCredentials credentials = new ClientCredentials();
                // set default credentials for OrganizationService            
                credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials; 

                OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null); 
                IOrganizationService _service = (IOrganizationService)orgProxy;

                DataTable dtRecords = GetDataTable();

                foreach(DataRow row in dtRecords.Rows){

                        Entity currentLead = new Entity("lead");
                        currentLead["subject"] = (string)row["Topic"];
                        currentLead["firstname"] = (string)row["First Name"];
                        currentLead["lastname"] = (string)row["Last Name"];
                        currentLead["companyname"] = (string)row["Company Name"];
                        currentLead["emailaddress1"] = (string)row["Email"];
                        currentLead["telephone1"] = (string)row["Phone"];




    I also tried doing it by using a reference to an Xrm.cs class generated by the crmsvcutil.exe tool but the code fails too and I get the error below. Thanks for your help in advance.


    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:entity. The InnerException message was 'Error in line 1 position 442. Element 'http://schemas.microsoft.com/xrm/2011/Contracts/Services:entity' contains data from a type that maps to the name 'Xrm:Lead'. The deserializer has no knowledge of any type that maps to this name. Consider changing the implementation of the ResolveName method on your DataContractResolver to return a non-null value for name 'Lead' and namespace 'Xrm'.'.  Please see InnerException for more details.

    Thursday, May 26, 2011 8:26 PM


All replies