如何将新实体添加到MS CRM

时间:2014-12-11 09:41:04

标签: dynamics-crm-2011 dynamics-crm crm dynamics-crm-2013

我想在crm中创建一个新实体

    OrganizationService_orgService ;
    var connection = CrmConnection.Parse(conn);
    _orgService = new OrganizationService(connection);

    Entity newEntity = new Entity("this_is_a_new_entity");
    Guid newEntityID = _orgService.Create(newEntity); 

我写了上面的代码,其中conn是连接字符串,格式正确(我检查过)

  string conn = "Url=https://damnidiot.crm5.dynamics.com; Username=XXXXXXXX@damnidiot.onmicrosoft.com; Password=XXXXXXXXX;";

但是当我运行代码时,我得到一个异常{"The entity with a name = 'this_is_a_new_entity' was not found in the MetadataCache."}

我觉得我得到了这个错误,因为我的crm对实体this_is_a_new_entity没有定义。

是否可以重新审核并更新MS CRM的元数据缓存?(我使用的是Microsoft Dynamics CRM 2013)

2 个答案:

答案 0 :(得分:4)

如果您使用new Entity("new_entity_name"),则告诉代码您要在名为new_entity_name现有实体中创建新的记录

要完全创建新实体,您必须发出CreateEntityRequest (link to MSDN)

// PART OF THE LINKED SAMPLE
CreateEntityRequest createrequest = new CreateEntityRequest
{
    //Define the entity
    Entity = new EntityMetadata
    {
        SchemaName = _customEntityName,
        DisplayName = new Label("Bank Account", 1033),
        DisplayCollectionName = new Label("Bank Accounts", 1033),
        Description = new Label("An entity to store information about customer bank accounts", 1033),
        OwnershipType = OwnershipTypes.UserOwned,
        IsActivity = false,

    },

    // Define the primary attribute for the entity
    PrimaryAttribute = new StringAttributeMetadata
    {
        SchemaName = "new_accountname",
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
        MaxLength = 100,
        Format = StringFormat.Text,
        DisplayName = new Label("Account Name", 1033),
        Description = new Label("The primary attribute for the Bank Account entity.", 1033)
    }

};

答案 1 :(得分:2)

要创建新实体,您应该使用Create Entity Request。您的代码会创建this_is_a_new_entity的记录