How can I set the Navigation Property name for a Lookup Field while creating it programmatically?

By | July 11, 2023

We were recently working on a requirement to create a polymorphic lookup attribute. We needed to use its navigation property further while working on the UI part. However, we were having issues using the navigating property of the field for some of the OOB entities. While further investigating, we found that the relationship was being created in the background in the format of “prefix_fieldSchemaName_OOBEntitylogicalName_OOBEntityLogicalName”.

Since we wanted it to be in the format of “prefix_fieldSchemaName_OOBEntityLogicalName”, we started looking at it at the code level. Since we were creating the relationship through C# code, we started looking through two metadata requests –

  • LookupAttributeMetadata
  • OneToManyRelationshipMetadata

After a few trials and errors, we created the relationship with the name in the format we wanted, and below are the steps to achieve it.

Firstly, we need to add the IsCustomizable property to our LookupAttributeMetadata with the value a new BooleanManagedProperty with the value as true.

// Create a new instance of BooleanManagedProperty
BooleanManagedProperty boolManagedProperty = new Microsoft.Xrm.Sdk.BooleanManagedProperty();

boolManagedProperty.Value = true;

createMultilookupField.Parameters["Lookup"] = new LookupAttributeMetadata()

{

SchemaName = fieldSchemaName,

DisplayName = new Label(fieldDisplayName, languageCode),

IsCustomizable = boolManagedProperty,

Description = new Label(description, languageCode)

};

After the change in LookupAttributeMetadata request, we need to add the ReferencingEntityNavigationPropertyName property to our OneToManyRelationshipMetadata request, as shown below –

//create variable for onetomanyrelationship metadata.
var lookupFieldcreated = new OneToManyRelationshipMetadata

{

//entity on which this field should be created

ReferencingEntity = referencingEntityName,

//entity with which relationship should be created

ReferencedEntity = logicalNameofEntity,

SchemaName = relationshipSchemaName,

ReferencingEntityNavigationPropertyName = fieldSchemaName + "_" + logicalNameofEntity,

};

So, after adding these two properties, the whole code to create the polymorphic attribute with a custom Referencing Entity Navigation Property Name becomes as below –

OrganizationRequest createMultilookupField = new OrganizationRequest
{

RequestName = "CreatePolymorphicLookupAttribute"

};
 

// Create a new instance of BooleanManagedProperty

BooleanManagedProperty boolManagedProperty = new Microsoft.Xrm.Sdk.BooleanManagedProperty();


boolManagedProperty.Value = true;


createMultilookupField.Parameters["Lookup"] = new LookupAttributeMetadata()

{

SchemaName = fieldSchemaName,

DisplayName = new Label(fieldDisplayName, languageCode),

IsCustomizable = boolManagedProperty,

Description = new Label(description, languageCode)

};

//create variable for onetomanyrelationship metadata.

var lookupFieldcreated = new OneToManyRelationshipMetadata

{

//entity on which this field should be created

ReferencingEntity = referencingEntityName,

//entity with which relationship should be created

ReferencedEntity = logicalNameofEntity,

SchemaName = relationshipSchemaName,

ReferencingEntityNavigationPropertyName = fieldSchemaName + "_" + logicalNameofEntity,


};


createMultilookupField.Parameters["OneToManyRelationships"] = new OneToManyRelationshipMetadata[] { lookupFieldcreated };

config.Service.Execute(createMultilookupField);

Conclusion

This is how we can create a custom referencing entity navigation property name by setting the IsCustomizable & LookupAttributeMetadata properties.