Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

CreateOneToMany Message (MetadataService)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Works for all deployment typesWorks online only

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

The relevant classes are specified in the following table.

TypeClass
RequestCreateOneToManyRequest
ResponseCreateOneToManyResponse

Remarks

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

When you create a one to many relationship, you also create a lookup attribute in the referencing (many) entity that stores the ID of the related entity instance.

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 CreateOneToMany 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 OneToManyMetadata.
OneToManyMetadata oneToManyRelationship = new OneToManyMetadata();

// The account entity will be the 'one' in this one-to-many relationship.
oneToManyRelationship.ReferencedEntity = EntityName.account.ToString();

// The campaign entity will be the 'many' in this one-to-many relationship.
oneToManyRelationship.ReferencingEntity = EntityName.campaign.ToString();

// Set the metadata properties.
oneToManyRelationship.SchemaName = "new_account_campaign";
oneToManyRelationship.AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();
oneToManyRelationship.AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;
oneToManyRelationship.AssociatedMenuGroup = new CrmAssociatedMenuGroup();
oneToManyRelationship.AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;
oneToManyRelationship.AssociatedMenuLabel = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Account", 1033);
oneToManyRelationship.AssociatedMenuOrder = new MetadataServiceSdk.CrmNumber();
oneToManyRelationship.AssociatedMenuOrder.Value = 10000;

// Make the relationship behavior 'parental' by setting all cascade properties to 'Cascade'.
oneToManyRelationship.CascadeAssign = new CrmCascadeType();
oneToManyRelationship.CascadeAssign.Value = CascadeType.Cascade;
oneToManyRelationship.CascadeDelete = new CrmCascadeType();
oneToManyRelationship.CascadeDelete.Value = CascadeType.Cascade;
oneToManyRelationship.CascadeReparent = new CrmCascadeType();
oneToManyRelationship.CascadeReparent.Value = CascadeType.Cascade;
oneToManyRelationship.CascadeShare = new CrmCascadeType();
oneToManyRelationship.CascadeShare.Value = CascadeType.Cascade;
oneToManyRelationship.CascadeUnshare = new CrmCascadeType();
oneToManyRelationship.CascadeUnshare.Value = CascadeType.Cascade;

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

// The 'many' campaign entities will need to store the ID to the 'one' account they are related to.
// So we will now create a lookup to store that ID.
createRelationship.Lookup = new LookupAttributeMetadata();

// Set the lookup properties.
createRelationship.Lookup.SchemaName = "new_parent_accountid";
createRelationship.Lookup.AttributeType = new CrmAttributeType();
createRelationship.Lookup.AttributeType.Value = AttributeType.Lookup;
createRelationship.Lookup.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Account Lookup", 1033);
createRelationship.Lookup.RequiredLevel = new CrmAttributeRequiredLevel();
createRelationship.Lookup.RequiredLevel.Value = AttributeRequiredLevel.None;
createRelationship.Lookup.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Lookup", 1033);
createRelationship.OneToManyRelationship = oneToManyRelationship;
         
// Execute the request.
CreateOneToManyResponse oneToManyResponse = (CreateOneToManyResponse)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 OneToManyMetadata object.
OneToManyMetadata oneToManyRelationship = new OneToManyMetadata();

// The account entity will be the 'one' in this one-to-many relationship.
oneToManyRelationship.ReferencedEntity = EntityName.account.ToString();

// The campaign entity will be the 'many' in this one-to-many relationship.
oneToManyRelationship.ReferencingEntity = EntityName.campaign.ToString();

// Set the metadata properties.
oneToManyRelationship.SchemaName = "new_account_campaign";
oneToManyRelationship.AssociatedMenuBehavior = new CrmAssociatedMenuBehavior();
oneToManyRelationship.AssociatedMenuBehavior.Value = AssociatedMenuBehavior.UseLabel;
oneToManyRelationship.AssociatedMenuGroup = new CrmAssociatedMenuGroup();
oneToManyRelationship.AssociatedMenuGroup.Value = AssociatedMenuGroup.Details;
oneToManyRelationship.AssociatedMenuLabel = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Account", 1033);
oneToManyRelationship.AssociatedMenuOrder = new MetadataServiceSdk.CrmNumber();
oneToManyRelationship.AssociatedMenuOrder.Value = 10000;

// Make the relationship behavior 'parental' by setting all cascade properties to 'Cascade'.
oneToManyRelationship.CascadeAssign = new CrmCascadeType();
oneToManyRelationship.CascadeAssign.Value = CascadeType.Cascade;
oneToManyRelationship.CascadeDelete = new CrmCascadeType();
oneToManyRelationship.CascadeDelete.Value = CascadeType.Cascade;
oneToManyRelationship.CascadeReparent = new CrmCascadeType();
oneToManyRelationship.CascadeReparent.Value = CascadeType.Cascade;
oneToManyRelationship.CascadeShare = new CrmCascadeType();
oneToManyRelationship.CascadeShare.Value = CascadeType.Cascade;
oneToManyRelationship.CascadeUnshare = new CrmCascadeType();
oneToManyRelationship.CascadeUnshare.Value = CascadeType.Cascade;

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

// The 'many' campaign entities will need to store the id to the 'one' account they are related to.
// So we will now create a lookup to store that id.
createRelationship.Lookup = new LookupAttributeMetadata();

// Set the lookup properties.
createRelationship.Lookup.SchemaName = "new_parent_accountid";
createRelationship.Lookup.AttributeType = new CrmAttributeType();
createRelationship.Lookup.AttributeType.Value = AttributeType.Lookup;
createRelationship.Lookup.DisplayName = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Account Lookup", 1033);
createRelationship.Lookup.RequiredLevel = new CrmAttributeRequiredLevel();
createRelationship.Lookup.RequiredLevel.Value = AttributeRequiredLevel.None;
createRelationship.Lookup.Description = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.CreateSingleLabel("Sample Lookup", 1033);
createRelationship.OneToManyRelationship = oneToManyRelationship;
         
// Execute the request.
CreateOneToManyResponse oneToManyResponse = (CreateOneToManyResponse)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);

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.