DYNAMICS 365 HOW TO CREATE AND MANAGE POLYMORPHIC LOOKUP ATTRIBUTE IN SDK C#
In other posts, we saw
how to create a Polymorphic Lookup Attribute using the Polymorphic Lookup Manager plugin
and
how to create a Polymorphic Lookup Attribute using WEB API
As per Microsoft docs, the creation of the Polymorphic Lookup Attribute can be currently done through SDK or WebAPI, therefore,
in this post, we will see how to create a Polymorphic Lookup Attribute using SDK.
We will see as well, what are the different actions that can be done against a polymorphic lookup attribute.
-
In order to test these actions, I've created a console application that connects to a trial environment using OAUTH authentication.
For more details on how to connect to a Dynamics 365 environment using OAUTH authentication, you can check the following post. -
The console application will call the below method in order to create the Polymorphic Lookup Attribute
with four relationships under the account table
public void CreatePolymorphicLookupAttribute()
{
OrganizationRequest createPolymorphicAttributeRequest = new OrganizationRequest
{
RequestName = "CreatePolymorphicLookupAttribute"
};
// Polymorphic lookup attribute details
createPolymorphicAttributeRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
{
SchemaName = "cak_MainSectorId",
DisplayName = new Label("Main Sector", 1033)
};
// N:1 Relationship details with table 1
var polymorphicRelation1 = new OneToManyRelationshipMetadata
{
ReferencingEntity = "account",
ReferencedEntity = "cak_contractor",
SchemaName = "account_cak_contractor"
};
// N:1 Relationship details with table 2
var polymorphicRelation2 = new OneToManyRelationshipMetadata
{
ReferencingEntity = "account",
ReferencedEntity = "cak_educational",
SchemaName = "account_cak_educational"
};
// N:1 Relationship details with table 3
var polymorphicRelation3 = new OneToManyRelationshipMetadata
{
ReferencingEntity = "account",
ReferencedEntity = "cak_health",
SchemaName = "account_cak_health"
};
// N:1 Relationship details with table 4
var polymorphicRelation4 = new OneToManyRelationshipMetadata
{
ReferencingEntity = "account",
ReferencedEntity = "cak_university",
SchemaName = "account_cak_university"
};
// Add the N:1 relationships to the parameters
createPolymorphicAttributeRequest.Parameters["OneToManyRelationships"] = new OneToManyRelationshipMetadata[]
{
polymorphicRelation1,
polymorphicRelation2,
polymorphicRelation3,
polymorphicRelation4
};
// specify existing solution name to add the N:1 relationships components to it
createPolymorphicAttributeRequest.Parameters["SolutionUniqueName"] = "CAKSOL";
AdminService.Execute(createPolymorphicAttributeRequest);
}
-
Once the Polymorphic Lookup Attribute is created, you can achieve the following actions against it using the SDK
-
Add new relationship to an existing Polymorphic Lookup Attribute using the following method
public void AddRelationshipToExistingPolymorphic() {
var createRelationshipRequest = new CreateOneToManyRequest();
// define the new relationship details
var oneToManyRelationship = new OneToManyRelationshipMetadata
{
ReferencingEntity = "account",
ReferencedEntity = "contact",
SchemaName = "account_contact"
};
createRelationshipRequest.OneToManyRelationship = oneToManyRelationship;
// define the existing polymorphic lookup attribute details
createRelationshipRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
{
SchemaName = "cak_MainSectorId",
DisplayName = new Label("Main Sector", 1033)
};
AdminService.Execute(createRelationshipRequest);
}
-
Delete a relationship from an existing Polymorphic Lookup Attribute using the following method
public void DeleteRelationshipFromExistingPolymorphic() {
var deleteRelationshipRequest = new DeleteRelationshipRequest
{
// name of the relationship to be deleted
Name = "account_contact"
};
AdminService.Execute(deleteRelationshipRequest);
}
-
Delete an existing Polymorphic Lookup Attribute using the following method
public void DeletePolymorphicLookupAttribute() {
DeleteAttributeRequest deletePolymorphicAttributeRequest = new DeleteAttributeRequest();
// define the entity name where the polymorphic lookup attribute exists
deletePolymorphicAttributeRequest.EntityLogicalName = "account";
// define the existing polymorphic lookup attribute
deletePolymorphicAttributeRequest.LogicalName = "cr53c_mainsectorid";
AdminService.Execute(deletePolymorphicAttributeRequest);
}
-
Add new relationship to an existing Polymorphic Lookup Attribute using the following method
Hope This Helps!
Comments
Post a Comment