以编程方式在 Dynamics CRM 2015 中创建 LookupAttributeMetadata

Programmatically creating LookupAttributeMetadata in Dynamics CRM 2015

我准备将 Dynamics CRM 2011 On Premise 实例迁移到 Dynamics CRM 2015 Online。

我正在使用当前的 Dynamics CRM SDK(当前版本 7.1)并已成功迁移自定义属性,但 VirtualLookup 属性除外,它们无法通过创建CreateAttributeRequest.

接下来,我需要迁移所有关系。到目前为止,我已经能够获得必要的 OneToManyRelationshipMetadataManyToManyRelationshipMetadata。但是,对于 OneToManyRelationshipMetadata,我需要将 LookupAttributeMetadata 传递给 CreateAttributeRequest

OneToManyRelationshipRequest request = new OneToManyRelationshipRequest() 
{
    Lookup = new LookupAttributeMetadata() 
    {
        SchemaName = "new_topicid",
        DisplayName = new Label("Subject", 1033),
        Description = new Label("Subject Description", 1033)
    },
    OneToManyRelationship = new OneToManyRelationshipMetadata() 
    {
        ReferencedEntity = "subject",
        ReferencedAttribute = "subjectid",
        ReferencingEntity = "customer",
        ReferencingAttribute = "new_topicid"
    }
}

但是,我得到的异常是 new_topicid 属性不存在。这可能是有道理的,因为我不得不在之前的属性创建过程中跳过它(因为它不能通过 CreateAttributeRequest 创建)。

有没有其他方法可以将 LookupAttributeMetadataOneToManyRelationshipMetadata/ManyToManyRelationshipMetadata 在线迁移到 Dynamics CRM?

MSDN 上有一个示例。

示例中的参数明显多于您上面的代码,这可能是问题的原因。

Sample: Create and retrieve entity relationships

CreateOneToManyRequest createOneToManyRelationshipRequest =
    new CreateOneToManyRequest
{
    OneToManyRelationship =
    new OneToManyRelationshipMetadata
    {
        ReferencedEntity = "account",
        ReferencingEntity = "campaign",
        SchemaName = "new_account_campaign",
        AssociatedMenuConfiguration = new AssociatedMenuConfiguration
        {
            Behavior = AssociatedMenuBehavior.UseLabel,
            Group = AssociatedMenuGroup.Details,
            Label = new Label("Account", 1033),
            Order = 10000
        },
        CascadeConfiguration = new CascadeConfiguration
        {
            Assign = CascadeType.NoCascade,
            Delete = CascadeType.RemoveLink,
            Merge = CascadeType.NoCascade,
            Reparent = CascadeType.NoCascade,
            Share = CascadeType.NoCascade,
            Unshare = CascadeType.NoCascade
        }
    },
    Lookup = new LookupAttributeMetadata
    {
        SchemaName = "new_parent_accountid",
        DisplayName = new Label("Account Lookup", 1033),
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
        Description = new Label("Sample Lookup", 1033)
    }
};

为什么 Dynamics 社区如此迅速地告诉人们他们不应该做某事?可能有一个很好的理由吧?答案就在 CRM 2015 SDK 中。寻找名为 WorkWithRelationships.cs 的 class。

为了子孙后代,它就在这里。

var createOneToManyRelationshipRequest = new CreateOneToManyRequest {
OneToManyRelationship = new OneToManyRelationshipMetadata
    {
        ReferencedEntity = "account",
        ReferencingEntity = "campaign",
        SchemaName = "new_account_campaign",
        AssociatedMenuConfiguration = new AssociatedMenuConfiguration
        {
            Behavior = AssociatedMenuBehavior.UseLabel,
            Group = AssociatedMenuGroup.Details,
            Label = new Label("Account", 1033),
            Order = 10000
        },
        CascadeConfiguration = new CascadeConfiguration
        {
            Assign = CascadeType.NoCascade,
            Delete = CascadeType.RemoveLink,
            Merge = CascadeType.NoCascade,
            Reparent = CascadeType.NoCascade,
            Share = CascadeType.NoCascade,
            Unshare = CascadeType.NoCascade
        }
    },
Lookup = new LookupAttributeMetadata
    {
        SchemaName = "new_parent_accountid",
        DisplayName = new Label("Account Lookup", 1033),
        RequiredLevel = new AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None),
        Description = new Label("Sample Lookup", 1033)
    }
};

var createOneToManyRelationshipResponse = (CreateOneToManyResponse)_serviceProxy.Execute(
createOneToManyRelationshipRequest);