UpdateRelationship Message (MetadataService)

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

Find the latest SDK documentation: CRM 2015 SDK

Works for all deployment types Works online only

Updates the metadata for a relationship.

The relevant classes are specified in the following table.

Type Class
Request UpdateRelationshipRequest
Response UpdateRelationshipResponse


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

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


The following example shows how to use the UpdateRelationship message.

// 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 the request
RetrieveRelationshipRequest relationshipRequest = new RetrieveRelationshipRequest();

// This is a built-in relationship
relationshipRequest.Name = "Contact_Tasks";
// Retrieve only the currently published changes, ignoring the changes that have
// not been published.
relationshipRequest.RetrieveAsIfPublished = false;

// Execute the request
RetrieveRelationshipResponse relationshipResponse = (RetrieveRelationshipResponse)metadataService.Execute(relationshipRequest);

// Access the retrieved relationship
OneToManyMetadata retrievedRelationship = (OneToManyMetadata)relationshipResponse.RelationshipMetadata;

// Create a new associated menu label
MetadataServiceSdk.CrmNumber langCode = new MetadataServiceSdk.CrmNumber();
MetadataServiceSdk.LocLabel englishLabel = new MetadataServiceSdk.LocLabel();
MetadataServiceSdk.CrmLabel newMenuLabel = new MetadataServiceSdk.CrmLabel();
langCode.Value = 1033;
englishLabel.LanguageCode = langCode;
englishLabel.Label = "My New Label";
newMenuLabel.LocLabels = new MetadataServiceSdk.LocLabel[] { englishLabel };

// Modify a relationship retrieved via a RetrieveRelationshipRequest call
retrievedRelationship.AssociatedMenuLabel = newMenuLabel;

// Update the relationship
UpdateRelationshipRequest updateRequest = new UpdateRelationshipRequest();
updateRequest.Relationship = retrievedRelationship;
updateRequest.MergeLabels = false;

// Execute the request
UpdateRelationshipResponse updateResponse = (UpdateRelationshipResponse)metadataService.Execute(updateRequest);

// Publish the customization
// 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 the request.
Dim relationshipRequest As New RetrieveRelationshipRequest()

' This is a built-in relationship.
relationshipRequest.Name = "Contact_Tasks"
relationshipRequest.RetrieveAsIfPublished = True

' Execute the request.
Dim relationshipResponse As RetrieveRelationshipResponse = CType(metadataService.Execute(relationshipRequest), RetrieveRelationshipResponse)

' Access the retrieved relationship.
Dim retrievedRelationship As OneToManyMetadata = CType(relationshipResponse.RelationshipMetadata, OneToManyMetadata)

See Also



© 2010 Microsoft Corporation. All rights reserved.