Create and retrieve Dynamics 365 Stakeholder Connections (C#, SDK)


We recently wrote a data loader for a client which needed to load multiple contacts against an opportunity. The most logical way to do this was to utilize the Stakeholder grid on the opportunity. The underlying structure behind this grid uses connections with a connection role category of ‘Stakeholder’. Out of the box this category allows for the following roles:

  • Champion
  • Decision
  • End User
  • Economic Buyer
  • Stakeholder
  • Influencer
  • Technical Buyer

We are using the ‘Stakeholder’ role for the example within this article.

Using connections enables us to use a many to many relationship (N:N) to satisfy the requirement without the need for modifications.

How to create a new connection via the SDK?

The variables we need to create a new opportunity stakeholder connection are:

  • Opportunity ID (guid)
  • Contact ID (guid)
  • Connection Role ID (guid)

The Connection Role ID is easy to find and in this case as we are specifically after the “Stakeholder” role.  Out of the box there is only one and this can be found by using the query below:

The other two (contact and opportunity ID) are fairly self explanatory.

I tend to store my IDs  as strings within code and convert them when I need to used them. It’s just a habit.

Use the following code to create the new stakeholder connection:

I do add a description of Stakeholder in the above code so that from a contact connection grid point of view, it’s clear what the role is. Alternatively you can create a new role or update the Stakeholder role to work in reverse and update the record2roleid field with this role by adding the following line to the code:

Gotchas with connections

Because a Dynamics 365 connection creates a many to many relationship with a “connection” entity in the middle (i.e. between the opportunity and contact) we can have multiple connections to and from the same opportunity and contact, and therefore create duplicate stakeholders on the one opportunity.

If this were a straight native N:N relationship (i.e. no connection entity in the middle) we would hit a key error trying to save an identical relationship.

To get around this, check to see if the connection between the two records already exists.

Checking to see if the connection already exists

Use the following Dynamics 365 SDK QueryExpression to check if the connection between the opportunity and contact record already exists. It uses both the Contact ID (guid) and Opportunity ID (guid).

The result will either return a connection ID (guid) or a blank string. If it’s blank it does not exist.

Feel free to comment if you have any issues or questions.

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts