Sample code (C#) for Multi-Table Lookup / Polymorphic Lookup attribute in Dataverse / Dynamics 365


For creating multi-table lookup we already have an XrmToolBox plugin Polymorphic Lookup Creator which we should be using ideally, but in case somebody wants to try it out, the below shared sample code can be referred.

Below is our custom table named mycustomtable to which we’d add a multi-table lookup which references case, contact and account entity.

We need to call CreatePolymorphicLookupAttribute Action, which expects 3 parameters

Below is the sample code to create the polymorphic lookup.

string ConnectionString = "AuthType = OAuth; " +
                  "AppId=51f81489-12ee-4a9e-aaae-a2591f45987d; " +
                  "Username=user@domain.onmicrosoft.com; " +
                  "Password=pwd; " +
                  "RedirectUri=app://58145B91-0C36-4500-8554-080854F2AC97;" +
                  "Url = https://orgname.crm4.dynamics.com/;";


 CrmServiceClient svc = new CrmServiceClient(ConnectionString);

            if (svc.IsReady)
            {
                // Create PolymorphicLookupAttribute 
                // with mycustomtable custom entity / table
                // referencing case, contact and account entity
                var varOrgRequest = new OrganizationRequest();

                // specify the request name
                varOrgRequest.RequestName = "CreatePolymorphicLookupAttribute";

                // specify lookup attribute details
                varOrgRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
                {
                    SchemaName = "crf82_mypolymorphiclookup",
                    DisplayName = new Label("My Polymorphic Lookup", 1033)
                };

                // referencing entity is our custom entity named my custom table
                // referenced entity is incident
                var oneToManyRelation1 = new OneToManyRelationshipMetadata();
                oneToManyRelation1.ReferencingEntity = "crf82_mycustomtable";
                oneToManyRelation1.ReferencedEntity = "incident";
                oneToManyRelation1.SchemaName = "crf82_mycustomtable_crf82_incident";

                // referencing entity is our custom entity named my custom table
                // referenced entity is contact
                var oneToManyRelation2 = new OneToManyRelationshipMetadata();
                oneToManyRelation2.ReferencingEntity = "crf82_mycustomtable";
                oneToManyRelation2.ReferencedEntity = "contact";
                oneToManyRelation2.SchemaName = "crf82_mycustomtable_crf82_contact";


                // referencing entity is our custom entity named my custom table
                // referenced entity is account
                var oneToManyRelation3 = new OneToManyRelationshipMetadata();
                oneToManyRelation3.ReferencingEntity = "crf82_mycustomtable";
                oneToManyRelation3.ReferencedEntity = "account";
                oneToManyRelation3.SchemaName = "crf82_mycustomtable_crf82_account";

                // populate OneToManyRelationships parameter of CreatePolymorphicLookupAttribute request
                varOrgRequest.Parameters["OneToManyRelationships"] = new OneToManyRelationshipMetadata[]
                {
                    oneToManyRelation1, oneToManyRelation2, oneToManyRelation3
                };

                // specify the existing solution name 
                varOrgRequest.Parameters["SolutionUniqueName"] = "MySolution";

                var response = svc.Execute(varOrgRequest);
            }

The lookup à

Sample code to add a new relationship to an existing lookup.

                var createOneToManyRelationshipRequest = new CreateOneToManyRequest();

                // referencing entity is our custom entity named mycustomtable
                // referenced entity is contact - add the entity to be added
                var oneToManyRelationAdd = new OneToManyRelationshipMetadata();
                oneToManyRelationAdd.ReferencingEntity = "crf82_mycustomtable";
                oneToManyRelationAdd.ReferencedEntity = "contact";
                oneToManyRelationAdd.SchemaName = "crf82_mycustomtable_crf82_contact";

                createOneToManyRelationshipRequest.OneToManyRelationship = oneToManyRelationAdd;


                // specify lookup attribute details to which new relationship is to be added
                createOneToManyRelationshipRequest.Parameters["Lookup"] = new LookupAttributeMetadata()
                {
                    SchemaName = "crf82_mypolymorphiclookup",
                    DisplayName = new Label("My Polymorphic Lookup", 1033)
                };


                var createOneToManyRelationshipResponse = svc.Execute(createOneToManyRelationshipRequest);

Sample code to remove relationship from an existing lookup.

 var deleteRelationShip = new DeleteRelationshipRequest();
                // specify schema name of the relationship 
                deleteRelationShip.Name = "crf82_mycustomtable_crf82_contact1";
                svc.Execute(deleteRelationShip);

Sample code to delete the lookup

  // User delete attribute request 
                DeleteAttributeRequest varDelRequest = new DeleteAttributeRequest();

                // specify the entity name
                varDelRequest.EntityLogicalName = "crf82_mycustomtable";

                // specify the schema name of the entity
                varDelRequest.LogicalName = "crf82_mymultitablelookup";
                svc.Execute(varDelRequest);

Get all the details here –

https://docs.microsoft.com/en-us/powerapps/developer/data-platform/webapi/multitable-lookup?branch=pr-en-us-4448

Hope it helps..

Advertisements

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

3 thoughts on “Sample code (C#) for Multi-Table Lookup / Polymorphic Lookup attribute in Dataverse / Dynamics 365”

  1. Hi,
    I have added one polymorphic lookup “serviceid” using polymorphic lookup manager tool from xrm toolbox then i tried to import solution in different environment but i’m getting error of missing dependency with “serviceidtype” field.
    Can you help me with that?

    Liked by 1 person

Leave a Reply to Priya Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

RelatoCorto

Blog Fiction

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

Virendra Agrawal's Blog

Blogs, News and Insights of Dynamics 365 world

Jukka Niiranen

Thinking forward about Microsoft Power Platform

Amar Singh

Share your knowledge. It’s a way to achieve immortality.

Lokin Shah

Dynamics 365 - Sharing edge cases and ideas

%d bloggers like this: