开发者

Create a Many to Many relationship in CRM 4 and 5 in C#

开发者 https://www.devze.com 2023-03-09 15:12 出处:网络
I need to create/define a Many to Many relationship between a Lead and a custom entity as well as between a Contact and a custom entity. I can\'t seem to find any code examples of 开发者_开发知识库wha

I need to create/define a Many to Many relationship between a Lead and a custom entity as well as between a Contact and a custom entity. I can't seem to find any code examples of 开发者_开发知识库what I want to do.

This needs to work in both CRM 4 and CRM 5.

Are there any disadvantages to doing two N:1 relationships instead of the N:N relationship?


You can do N:N relationships via the UI by going to an entities N:N relationships. There are advantages to doing 2 N:1 relationships with an intermediate entity over doing a single N:N such as you can store relationship attributes (a role for the relationship), and also you can chain workflows through a double N:1 relationship.

EDIT:

To create N:N relationships through code you can refer to the CRM 2011 SDK help page "Create and Retrieve Entity Relationships", excerpted below. You will be able to do something similiar via the Metadata service in 4.0 -

Create an N:N Entity Relationship

The following sample uses a EligibleCreateManyToManyRelationship method to verify that the Account and Campaign entities can participate in a N:N entity relationship and then creates the entity relationship by using CreateManyToManyRequest.

bool accountEligibleParticipate =
    EligibleCreateManyToManyRelationship("account");
bool campaignEligibleParticipate =
    EligibleCreateManyToManyRelationship("campaign");

if (accountEligibleParticipate && campaignEligibleParticipate)
{

    CreateManyToManyRequest createManyToManyRelationshipRequest =
        new CreateManyToManyRequest
    {
        IntersectEntitySchemaName = "new_accounts_campaigns",
        ManyToManyRelationship = new ManyToManyRelationshipMetadata
        {
            SchemaName = "new_accounts_campaigns",
            Entity1LogicalName = "account",
            Entity1AssociatedMenuConfiguration =
            new AssociatedMenuConfiguration
            {
                Behavior = AssociatedMenuBehavior.UseLabel,
                Group = AssociatedMenuGroup.Details,
                Label = new Label("Account", 1033),
                Order = 10000
            },
            Entity2LogicalName = "campaign",
            Entity2AssociatedMenuConfiguration =
            new AssociatedMenuConfiguration
            {
                Behavior = AssociatedMenuBehavior.UseLabel,
                Group = AssociatedMenuGroup.Details,
                Label = new Label("Campaign", 1033),
                Order = 10000
            }
        }
    };

    CreateManyToManyResponse createManytoManyRelationshipResponse =
        (CreateManyToManyResponse)_serviceProxy.Execute(
        createManyToManyRelationshipRequest);


    _manyToManyRelationshipId =
        createManytoManyRelationshipResponse.ManyToManyRelationshipId;
    _manyToManyRelationshipName =
        createManyToManyRelationshipRequest.ManyToManyRelationship.SchemaName;

    Console.WriteLine(
        "The many-to-many relationship has been created between {0} and {1}.",
        "account", "campaign");
}

EligibleCreateManyToManyRelationship

The following sample creates a EligibleCreateManyToManyRelationship method that uses CanManyToManyRequest to verify whether an entity can participate in a N:N entity relationship.

/// <summary>
/// Determines whether the entity can participate in a many-to-many relationship.
/// </summary>
/// <param name="entity">Entity</param>
/// <returns></returns>
public bool EligibleCreateManyToManyRelationship(string entity)
{
    CanManyToManyRequest canManyToManyRequest = new CanManyToManyRequest
    {
        EntityName = entity
    };

    CanManyToManyResponse canManyToManyResponse =
        (CanManyToManyResponse)_serviceProxy.Execute(canManyToManyRequest);

    if (!canManyToManyResponse.CanManyToMany)
    {
        Console.WriteLine(
            "Entity {0} can't participate in a many-to-many relationship.", 
            entity);
    }

    return canManyToManyResponse.CanManyToMany;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号