Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CreateManyToMany Message (MetadataService)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Works for all deployment typesWorks online only

Creates a many-to-many relationship between two entities.

The relevant classes are specified in the following table.

TypeClass
RequestCreateManyToManyRequest
ResponseCreateManyToManyResponse

Remarks

To perform this action, the caller must be a user in the organization for which metadata is requested and must have Create Relationship and Read Relationship privileges.

The relationship created will have a property value of true for the RelationshipMetadata.IsCustomRelationship property.

You must publish the changes to the metadata before this change will be visible in the application. For more information see Publishing the Metadata.

Example

The following example shows how to use the CreateManyToMany message. For more information on the helper methods in the Microsoft.Crm.Sdk.Utility.CrmServiceUtility namespace, see Utility Sample Code.

[C#]
// Create an authentication token.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.OrganizationName = "AdventureWorksCycle";

// You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0; 

// Create the metadata Web service.
MetadataService metadataService = new MetadataService();
metadataService.Url = "http://<servername>:<port>/MSCRMServices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
metadataService.PreAuthenticate = true;

// Create a ManyToManyMetadata.
ManyToManyMetadata manyToManyRelationship = new ManyToManyMetadata();

// Set the metadata properties.
manyToManyRelationship.SchemaName = "new_accounts_campaigns";

// The account entity will arbitrarily be the first entity in this relationship.
manyToManyRelationship.Entity1LogicalName = EntityName.account.ToString();
manyToManyRelationship.Entity1AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();
manyToManyRelationship.Entity1AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;
manyToManyRelationship.Entity1AssociatedMenuGroup = new CrmAssociatedMenuGroup();
manyToManyRelationship.Entity1AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;
manyToManyRelationship.Entity1AssociatedMenuLabel = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Account", 1033);
manyToManyRelationship.Entity1AssociatedMenuOrder = new MetadataServiceSdk.CrmNumber();
manyToManyRelationship.Entity1AssociatedMenuOrder.Value = 10000;

// Add the campaign entity to the relationship.
manyToManyRelationship.Entity2LogicalName = EntityName.campaign.ToString();
manyToManyRelationship.Entity2AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();
manyToManyRelationship.Entity2AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;
manyToManyRelationship.Entity2AssociatedMenuGroup = new CrmAssociatedMenuGroup();
manyToManyRelationship.Entity2AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;
manyToManyRelationship.Entity2AssociatedMenuLabel = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Campaign", 1033);
manyToManyRelationship.Entity2AssociatedMenuOrder = new MetadataServiceSdk.CrmNumber();
manyToManyRelationship.Entity2AssociatedMenuOrder.Value = 10000;

// Create the request.
CreateManyToManyRequest createRelationship = new CreateManyToManyRequest();

// Microsoft Dynamics CRM 4.0 uses a linker behind the scenes to create a many-to-many relationship.
// This is the name given to that linker entity.
createRelationship.IntersectEntitySchemaName = "new_accounts_campaigns";
createRelationship.ManyToManyRelationship = manyToManyRelationship;

// Execute the request.
CreateManyToManyResponse oneToManyResponse = (CreateManyToManyResponse)metadataService.Execute(createRelationship);

// Publish the new custom entity.
// Note: All customizations must be published before they can be used.
PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();
PublishAllXmlResponse publishAllResponse = (PublishAllXmlResponse)metadataService.Execute(publishAllRequest);
[Visual Basic .NET]
' Set up the CRM service.
Dim token As New CrmAuthenticationToken()
' You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
token.AuthenticationType = 0
token.OrganizationName = "AdventureWorksCycle";

// Create the metadata Web service.
Dim metadataService As New MetadataService ()
metadataService.Url = "http://<servername>:<port>/mscrmservices/2007/MetadataService.asmx";
metadataService.CrmAuthenticationTokenValue = token;
metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials
metadataService.PreAuthenticate = true;

// Create a ManyToManyMetadata object.
ManyToManyMetadata manyToManyRelationship = new ManyToManyMetadata();

// Set the metadata properties.
manyToManyRelationship.SchemaName = "new_accounts_campaigns";

// The account entity will arbitrarily be the first entity in this relationship.
manyToManyRelationship.Entity1LogicalName = EntityName.account.ToString();
manyToManyRelationship.Entity1AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();
manyToManyRelationship.Entity1AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;
manyToManyRelationship.Entity1AssociatedMenuGroup = new CrmAssociatedMenuGroup();
manyToManyRelationship.Entity1AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;
manyToManyRelationship.Entity1AssociatedMenuLabel = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Account", 1033);
manyToManyRelationship.Entity1AssociatedMenuOrder = new MetadataServiceSdk.CrmNumber();
manyToManyRelationship.Entity1AssociatedMenuOrder.Value = 10000;

// Add the campaign entity to the relationship.
manyToManyRelationship.Entity2LogicalName = EntityName.campaign.ToString();
manyToManyRelationship.Entity2AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();
manyToManyRelationship.Entity2AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;
manyToManyRelationship.Entity2AssociatedMenuGroup = new CrmAssociatedMenuGroup();
manyToManyRelationship.Entity2AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;
manyToManyRelationship.Entity2AssociatedMenuLabel = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Campaign", 1033);
manyToManyRelationship.Entity2AssociatedMenuOrder = new MetadataServiceSdk.CrmNumber();
manyToManyRelationship.Entity2AssociatedMenuOrder.Value = 10000;

// Create the request.
CreateManyToManyRequest createRelationship = new CreateManyToManyRequest();

// Microsoft CRM 4.0 uses an intersect table behind the scenes to create a many-to-many relationship.
// This is the name given to that intersect table.
createRelationship.IntersectEntitySchemaName = "new_accounts_campaigns";
createRelationship.ManyToManyRelationship = manyToManyRelationship;

// Execute the request.
CreateManyToManyResponse oneToManyResponse = (CreateManyToManyResponse)metadataService.Execute(createRelationship);

// Publish the new customizations.
// Note:  All customizations must be published before they can be used.
PublishAllXmlRequest publishAllRequest = new PublishAllXmlRequest();
PublishAllXmlResponse publishAllResponse = (PublishAllXmlResponse)metadataService.Execute(publishAllRequest);

See Also

Concepts

Reference


© 2010 Microsoft Corporation. All rights reserved.


Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.