CRM 2011 – N:M Beziehung via C# setzen

CRM 2011 beherrscht nun endlich eine vernünftige N:M Beziehung.
Folgendes Code-Snippet zeigt, wie man via C# und dem SDK-Webservice zwei Entitäten verbindet.

[sourcecode language=“csharp“]
/// <summary>
/// Clones the entity.
/// </summary>
/// <param name="organization">The organization.</param>
/// <param name="entity1">The entity1.</param>
/// <param name="entity2">The entity2.</param>
/// <param name="relationShipName">Name of the relation ship.</param>
public static void AssociateEntities(contracts.IOrganization organization, Entity entity1, Entity entity2, string relationShipName)
{
// Create an AssociateEntities request
AssociateEntitiesRequest request = new AssociateEntitiesRequest();

// Set the ID of Moniker1 to the ID of the lead.
request.Moniker1 = new EntityReference { Id = entity1.Id, LogicalName = entity1.LogicalName};

// Set the ID of Moniker2 to the ID of the contact.
request.Moniker2 = new EntityReference { Id = entity2.Id, LogicalName = entity2.LogicalName};

// Set the relationship name to associate on.
request.RelationshipName = relationShipName;

// Execute the request.
Helper.OrgService(organization).Execute(request);
}
[/sourcecode]

Viel Spaß beim Ausprobieren!

Leave a reply