在MS CRM 4.0中创建动态实体

时间:2022-08-01 12:54:00

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

我正在尝试使用动态实体创建新联系人。我在CRM SDK中找到的示例有此代码。

// 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

我没有看到代码中的太多差异。在我在互联网上找到的例子中,我有与SDK中发现的相同的实现。但如果我运行相同我得到以下错误

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

CS0029:无法将类型'Microsoft.Crm.Sdk.StringProperty'隐式转换为'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.

我尝试创建一个PropertyCollection类型的新变量(属于mscrm名称空间的变量),并将stringpropertys添加到该变量并将其传递给实体。

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'

CS0029:无法将类型'Microsoft.Crm.Sdk.PropertyCollection'隐式转换为'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.

我确定它是一个小的类型转换错误,但我无法弄清楚错误在哪里。而且,即使它是一个类型转换错误,为什么它适用于互联网上提供的所有样本而不是我。我尝试运行代码示例但我遇到相同的转换错误。如果您需要更多信息,请告知我们,对此有任何帮助将不胜感激。

3 个解决方案

#1


3  

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

以下是Microsoft的一篇文章,试图讨论此主题:

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...

如果您想继续使用Microsoft.Crm.Sdk.dll,您应该能够通过以下方式实现目标......

    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;

#2


1  

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.

感谢SaaS Developer,该代码现在运行良好。另一种方法是直接将StringProperty添加到实体属性集合中。

contactEntity.Properties.Add(sp_SSNNo);

Thanks again for replying :)

再次感谢回复:)

#3


0  

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.

我相信问题是您正在引用Microsoft.Crm.Sdk程序集中的动态实体类。 SDK中的示例使用对CRM Web服务的引用。这可能会让人感到困惑,因为两个程序集都包含许多相同的类型,但它们是不同的。

#1


3  

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

以下是Microsoft的一篇文章,试图讨论此主题:

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...

如果您想继续使用Microsoft.Crm.Sdk.dll,您应该能够通过以下方式实现目标......

    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;

#2


1  

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.

感谢SaaS Developer,该代码现在运行良好。另一种方法是直接将StringProperty添加到实体属性集合中。

contactEntity.Properties.Add(sp_SSNNo);

Thanks again for replying :)

再次感谢回复:)

#3


0  

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.

我相信问题是您正在引用Microsoft.Crm.Sdk程序集中的动态实体类。 SDK中的示例使用对CRM Web服务的引用。这可能会让人感到困惑,因为两个程序集都包含许多相同的类型,但它们是不同的。