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

本文关键字:编程 方式 创建 LookupAttributeMetadata Dynamics CRM 2015 | 更新日期: 2023-09-27 17:52:40

我准备将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不存在。这可能是有意义的,因为我必须在前面的Attribute创建期间跳过它(因为它不能通过CreateAttributeRequest创建)。

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

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

MSDN上有一个示例。

示例比上面的代码有更多的参数,这可能是问题的原因。

示例:创建和检索实体关系
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的类。

无论如何,这里是为子孙后代。

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