views:

4866

answers:

4

Hey there

I am trying to create a new contact using Dynamic Entity. The sample i found in CRM SDK had this code.

// Set the properties of the contact using property objects.
        StringProperty firstname = new StringProperty();
        firstname.Name = "firstname";
        firstname.Value = "Jesper";
        StringProperty lastname = new StringProperty();
        lastname.Name = "lastname";
        lastname.Value = "Aaberg";

        // Create the DynamicEntity object.
        DynamicEntity contactEntity = new DynamicEntity();

        // Set the name of the entity type.
        contactEntity.Name = EntityName.contact.ToString();

        // Set the properties of the contact.
        contactEntity.Properties = new Property[] {firstname, lastname};

In my code i have the following implementation.

        StringProperty sp_Field1 = new StringProperty("Field1","Value1");
        StringProperty sp_Field2 = new StringProperty("Field2","Value1");

        CrmService service = new CrmService();
        service.Credentials = System.Net.CredentialCache.DefaultCredentials;
        // Create the DynamicEntity object.
        DynamicEntity contactEntity = new DynamicEntity();
        // Set the name of the entity type.
        contactEntity.Name = EntityName.contact.ToString();
        // Set the properties of the contact.
        contactEntity.Properties = new Property[] {sp_Field1,sp_Field2};

I don't see much differences in the code. In the examples i found in the internet i have the same implementation as i found in SDK. But if i run the same i get the following error

CS0029: Cannot implicitly convert type 'Microsoft.Crm.Sdk.StringProperty' to 'Microsoft.Crm.Sdk.PropertyCollection'

I tried created a new variable of type PropertyCollection(one that belongs in mscrm namespace) and added the stringpropertys into that and passed it to the entity.

Microsoft.Crm.Sdk.PropertyCollection propTest = new Microsoft.Crm.Sdk.PropertyCollection();
        propTest.Add(sp_SSNNo);
        propTest.Add(sp_FirstName);
        contactEntity.Properties = new Property[] {propTest};

This gave me the following error

CS0029: Cannot implicitly convert type 'Microsoft.Crm.Sdk.PropertyCollection' to 'Microsoft.Crm.Sdk.Property'

I am sure its a minor typecasting error but i am not able to figure out where the error is. And moreover, even if it was a typecasting error why is it working for all the samples given in the internet and not for me. I tried getting the code sample to run but i am encountering the same conversion error. Please let me know if you need more info on this, any help on this would be appreciated.

A: 

I believe the issue is that you are referencing the dynamic entity class in the Microsoft.Crm.Sdk assembly. The sample in the SDK is using a reference to the CRM web service. This can get confusing as both assemblies contain many of the same types, however they are different.

SaaS Developer
A: 

So you are saying the assemblies i am referring are not bugfree ?. Is there a resolution for this ?

vikramjb
+3  A: 

Here is an article from Microsoft that makes an attempt to discuss this topic:

http://community.dynamics.com/blogs/cscrmblog/archive/2008/06/23/web-services-amp-dlls-or-what-s-up-with-all-the-duplicate-classes.aspx

This is not a bug that you are running into but more of a difference in design between the way the two assemblies work and what they are designed to do.

If you want to continue to use the Microsoft.Crm.Sdk.dll you should be able to accomplish your goal with the following...

    StringProperty sp_Field1 = new StringProperty("Field1","Value1");
    StringProperty sp_Field2 = new StringProperty("Field2","Value1");

    CrmService service = new CrmService();
    service.Credentials = System.Net.CredentialCache.DefaultCredentials;
    // Create the DynamicEntity object.
    DynamicEntity contactEntity = new DynamicEntity();
    // Set the name of the entity type.
    contactEntity.Name = EntityName.contact.ToString();

    // Set the properties of the contact.
    PropertyCollection properties = new PropertyCollection();
    properties.Add(sp_Field1);
    contactEntity.Properties = properties;
SaaS Developer
+1  A: 

Thanks SaaS Developer, that code is working fine now. One more way of doing it would be to directly add the StringProperty to the entity property collection.

contactEntity.Properties.Add(sp_SSNNo);

Thanks again for replying :)

vikramjb