locked
how to create a Record in CRM 2011 using Web Service??

    Question

  • i want to create a record in contact entity of CRM 2011 using the web service (i have added a service Reference - Organization.svc) i can create a simple record with specifying first name & last name  but when it comes to lokup or pick list or any other then normal string it is giving me error that

    There was an error while trying to serialize parameter http://schemas.microsoft.com/xrm/2011/Contracts/Services:entity. The InnerException message was 'Type '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 have only Added a service reference in VS 2010 project

    URL :  http://servername:port/OrganizationName/xrmServices/2011/organization.svc?wsdl

     OrganizationServiceClient orgClient = new OrganizationServiceClient();
                orgClient.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("user name", "password", "servername");
               

                Entity myContact = new Entity();
                myContact.LogicalName = "contact";
                createentity2011.ServiceReference1.AttributeCollection myAttColl = new createentity2011.ServiceReference1.AttributeCollection();

                  EntityReference t = new EntityReference();
                    t.LogicalName = "account";
                    t.Id =new Guid(guid);            

    myAttColl.Add(new KeyValuePair<string, object>("lastname", "Rana"));
    myAttColl.Add(new KeyValuePair<string, object>("parentcustomerid", t));

    myContact.Attributes = myAttColl;
               
                  try
                {
                    orgClient.Create(myContact);
                }
                catch (Exception ex){
                MessageBox.Show(ex.Message);
                }

    please help its urgent

    Wednesday, February 29, 2012 8:52 AM

Answers

  • Hi Krunal

    I got now where you are getting the problem. Actually pure WCF model , for some reason, does not have support for OptionSetValue and EntityReference. Hence, when you do the CRUD operation it throws the exception for Serialization.

    Here is the solution for this:

    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))]

    [System.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 { }

    4. 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}));

    5. Build the solution

    Hope this helps :)

    • Marked as answer by krunal89 Wednesday, February 29, 2012 2:31 PM
    Wednesday, February 29, 2012 2:04 PM

All replies

  • You will need to add KnownType attributes to the OrganizationRequest class. Probably the easiest way is to follow the steps under 'Add Supporting Classes and Edit Files' in http://msdn.microsoft.com/en-us/library/gg594452.aspx

    Although the example is for Silverlight, this should still be application to different project types that use a Service Reference


    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Wednesday, February 29, 2012 9:13 AM
    Moderator
  • Try

    CrmService service = new CrmService();
    service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
    service.CrmAuthenticationTokenValue = token;
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;

    get some Dynamic Entity if nececery 

    TargetRetrieveDynamic...
    RetrieveRequest...
    RetrieveResponse...
    
    DynamicEntity targetObject = (DynamicEntity)retrieveResponse.BusinessEntity;

    update properties of it and send back service.Update(dEntity);

    or create own dEntity and send it

    Wednesday, February 29, 2012 9:34 AM
  • still no luck any one have any good solution or code snippets that worked??

    Wednesday, February 29, 2012 10:07 AM
  • Hi Krunal

    Try with following modifications:

    1. Add your service reference with following uri: 

    string strURI = @”http://CRMServerName:defaultPort/” + strOrg +“/XRMServices/2011/Organization.svc”

    OrganizationServiceClient orgProxy = new OrganizationServiceClient();
    orgProxy.ClientCredentials.Windows.ClientCredential =new System.Net.NetworkCredential(“UserId”, “Password”, “DomainName”);


    System.ServiceModel.EndpointAddress myEndpointAdd = new System.ServiceModel.EndpointAddress(newUri strURI), System.ServiceModel.EndpointIdentity.CreateDnsIdentity(“”));
    orgProxy.Endpoint.Address= myEndpointAdd;

    (Replace CRMServerName, defaultPort and strOrg with your organization details)

    2. Now for creating entity record use it like this:

    Entity objAcc = newEntity();

    objAcc.LogicalName =“account”;
    AttributeCollection myAttColl = new AttributeCollection();
    myAttColl.Add(new KeyValuePair<string,object>(“name”, “ABC company”));
    myAttColl.Add(new KeyValuePair<string,object>(“emailaddress1″, abc@def.com));
    myAttColl.Add(new KeyValuePair<string,object>(“telephone1″, “+1234567″));
    objAcc.Attributes = myAttColl; 
    orgProxy.Create(objAcc);

    Hope this helps

    Wednesday, February 29, 2012 10:38 AM
  • I know how to create a string type.

    But I want to create lookup record so how can I create this.

    Thanks in Advance

    Wednesday, February 29, 2012 11:16 AM
  • Hi Krunal

    I got now where you are getting the problem. Actually pure WCF model , for some reason, does not have support for OptionSetValue and EntityReference. Hence, when you do the CRUD operation it throws the exception for Serialization.

    Here is the solution for this:

    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))]

    [System.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 { }

    4. 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}));

    5. Build the solution

    Hope this helps :)

    • Marked as answer by krunal89 Wednesday, February 29, 2012 2:31 PM
    Wednesday, February 29, 2012 2:04 PM
  • Thanks It worked....

    Thanks once again CRMDevlpr

     

    Wednesday, February 29, 2012 2:31 PM
  • Thanks it worked.... CRMDevlpr
    Wednesday, February 29, 2012 2:32 PM
  • Krunal,

    Can you please send me Code (remove all your username/passwords or URL's). If you send me Solution file will really helpful.

    Thanks in advance

    SK

    Monday, July 02, 2012 8:44 PM
  •  OrganizationServiceClient orgClient = new OrganizationServiceClient();
                orgClient.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("user name", "password", "servername");
               

                Entity myContact = new Entity();
                myContact.LogicalName = "contact";
                createentity2011.ServiceReference1.AttributeCollection myAttColl = new createentity2011.ServiceReference1.AttributeCollection();

                  EntityReference t = new EntityReference();
                    t.LogicalName = "account";
                    t.Id =new Guid(guid);            

    myAttColl.Add(new KeyValuePair<string, object>("lastname", "Rana"));
    myAttColl.Add(new KeyValuePair<string, object>("parentcustomerid", t));

    myContact.Attributes = myAttColl;
               
                  try
                {
                    orgClient.Create(myContact);
                }
                catch (Exception ex){
                MessageBox.Show(ex.Message);
                }

    this is the only code which i have mentioned in my first post is working
    Wednesday, July 04, 2012 5:01 AM
  • Thanks Krunal for your quick respond
    Friday, July 06, 2012 4:16 AM
  • CRMDevlpr What smart you are! :) Thanks a lot, you're Guru for me, you've helped me a lot! :)
    • Edited by gmaly Sunday, July 22, 2012 7:40 AM
    Friday, July 20, 2012 2:19 PM
  • Hi CRMDevlpr,

    i have a similar problem: i'm trying to create a campaign response which is linked with a lead and a campaign.

    In CRM 4.0 I did this like that:

    Dim lookup As New Lookup()
    lookup.Value = New Guid(linkCampaignGuid)
    lookup.type = "campaign"

    Dim activity As New activityparty()
    activity.partyid = New Lookup()
    activity.partyid.type = "lead"
    ' the CreateLead method creates a lead and returns its guid
    ' linkCustomer is an object with members like firstname, lastname, email etc.
    activity.partyid.Value = New Guid(CreateLead(linkCustomer))

    Dim cr As New campaignresponse()
    cr.regardingobjectid = lookup
    cr.customer = New activityparty() {activity}

    ' my CrmService object
    _service.Create(cr)


    In CRM 2011 im trying to do it with the web service:

    Dim lookup As New EntityReference()
    lookup.LogicalName = "campaign"
    lookup.Id = New Guid(linkCampaignGuid)

    Dim lead As New EntityReference()
    lead.LogicalName = "lead"
    lead.Id = New Guid(CreateLead(linkCustomer))

    Dim activity As New Entity
    activity.LogicalName = "activityparty"
    activity.Id = lead.Id
    activity.Attributes = New AttributeCollection
    activity.Attributes.Add(New KeyValuePair(Of String, Object)("partyid", lead))

    Dim campaignResponse As New Entity
    campaignResponse.LogicalName = "campaignresponse"
    campaignResponse.Attributes = New AttributeCollection
    campaignResponse.Attributes.Add(New KeyValuePair(Of String, Object)("regardingobjectid", lookup))
    campaignResponse.Attributes.Add(New KeyValuePair(Of String, Object)("customer", activity))

    _service.Create(campaignResponse)
    The creation of campaign response and lead is working. Also the link between campaign response and campaign works. But the link between lead and campaign response doesn't work.

    Any kind of help would be nice :)


    • Edited by Maxlanger Wednesday, February 26, 2014 3:16 PM
    Wednesday, February 26, 2014 3:12 PM
  • Hi

    'In the following statement :
    campaignResponse.Attributes.Add(New KeyValuePair(Of String, Object)("customer", activity))
    
    'Shouldn't this be like :
    
    campaignResponse.Attributes.Add(New KeyValuePair(Of String, Object)("customer", lead))
    
    

    Why are you trying to assign an object of type lead to customer type field ?

    Do you want to assign same lead to a newly created activity as well ?

    Wednesday, February 26, 2014 9:14 PM