简体   繁体   English

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

[英]Programmatically creating LookupAttributeMetadata in Dynamics CRM 2015

I am about to migrate a Dynamics CRM 2011 On Premise instance to Dynamics CRM 2015 Online.我即将将 Dynamics CRM 2011 On Premise 实例迁移到 Dynamics CRM 2015 Online。

I'm using the current Dynamics CRM SDK (current Version 7.1) and have managed to migrate the custom attributes, except the Virtual and Lookup ones, which can't be created via CreateAttributeRequest .我正在使用当前的 Dynamics CRM SDK(当前版本 7.1)并成功迁移了自定义属性,但无法通过CreateAttributeRequest创建的VirtualLookup属性除外。

Now next, I need to migrate all the relationships.接下来,我需要迁移所有关系。 So far I've been able to get the necessary OneToManyRelationshipMetadata and ManyToManyRelationshipMetadata .到目前为止,我已经能够获得必要的OneToManyRelationshipMetadataManyToManyRelationshipMetadata However, for OneToManyRelationshipMetadata I need to pass a LookupAttributeMetadata to the CreateAttributeRequest .但是,对于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"
    }
}

However, I get the exception that attribute new_topicid doesn't exist.但是,我得到属性new_topicid不存在的异常。 That may make sense, since I had to skip it during Attribute creation earlier (since it can't be created through CreateAttributeRequest ).这可能是有道理的,因为我在之前的 Attribute 创建过程中不得不跳过它(因为它不能通过CreateAttributeRequest创建)。

Is there any other way how I can migrate the LookupAttributeMetadata or OneToManyRelationshipMetadata / ManyToManyRelationshipMetadata to Dynamics CRM online?有没有其他方法可以将LookupAttributeMetadataOneToManyRelationshipMetadata / ManyToManyRelationshipMetadata在线迁移到 Dynamics CRM?

There is a sample on the MSDN for this. MSDN 上有一个示例。

The sample has significantly more parameters than your code above which is probably the cause of the problem.该示例的参数比上面的代码多得多,这可能是问题的原因。

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

Why is the Dynamics community so quick to tell people they should not be doing something?为什么 Dynamics 社区如此迅速地告诉人们他们不应该做某事? There may be a good reason right?可能有一个很好的理由吧? The answer is in the CRM 2015 SDK.答案在 CRM 2015 SDK 中。 Look for a class called WorkWithRelationships.cs.查找名为 WorkWithRelationships.cs 的类。

Here it is anyway for posterity.无论如何,这是为了后代。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM