Skip to content

Commit 4df5ac0

Browse files
authored
Merge branch 'main' into mints-monitor-dami
2 parents a01729f + 19743fa commit 4df5ac0

File tree

2 files changed

+19
-79
lines changed

2 files changed

+19
-79
lines changed

powerapps-docs/developer/data-platform/org-service/entity-operations-associate-disassociate.md

Lines changed: 18 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "Associate and disassociate table rows using the SDK for .NET (Microsoft Dataverse) | Microsoft Docs" # Intent and product brand in a unique string of 43-59 chars including spaces
33
description: "Learn how to associate and disassociate table rows using the SDK for .NET" # 115-145 characters including spaces. This abstract displays in the search result.
4-
ms.date: 03/22/2022
4+
ms.date: 12/13/2024
55
ms.reviewer: "pehecke"
66
ms.topic: "article"
77
author: MicroSri
@@ -19,9 +19,9 @@ contributors:
1919

2020
Table rows are associated to each other using lookup columns on the related table row. The simplest way to associate two rows in a one-to-many relationship is to use an <xref:Microsoft.Xrm.Sdk.EntityReference> to set the value of a lookup column on the related row.
2121

22-
The simplest way to disassociate two rows in a one-to-many relationship is to set the value of the lookup column to null.
22+
The simplest way to disassociate two rows in a one-to-many relationship is to set the value of the lookup column to `null`.
2323

24-
Relationships using an many-to-many relationship also depend on lookup columns on the *intersect entity* that supports the many-to-many relationship. These relationship are defined by the existence of rows in that intersect entity. While you can interact with the intersect entity directly, it is much easier to use the API to do this for you.
24+
Relationships using a many-to-many relationship also depend on lookup columns on the *intersect entity* that supports the many-to-many relationship. Relationship are defined by the existence of rows in that intersect entity. While you can interact with the intersect entity directly, it's much easier to use the API to do this task for you.
2525

2626
## Use the Associate method or AssociateRequest
2727

@@ -40,109 +40,49 @@ Whether the relationship is a one-to-many or many-to-many relationship doesn't m
4040

4141
You can discover the names of the relationships by viewing the customization UI or in the metadata using the Metadata Browser.
4242

43-
More information:
43+
More information:
4444

4545
- [Create and edit 1:N (one-to-many) or N:1 (many-to-one) relationships](../../../maker/data-platform/create-edit-1n-relationships.md)
4646
- [Create and edit many-to-many (N:N) table row relationships](../../../maker/data-platform/create-edit-nn-relationships.md)
4747
- [Browse the metadata for your environment](../browse-your-metadata.md)
4848

49-
The following example will set a specific contact (`jimGlynn`) as the primary contact for all accounts that are in Redmond.
49+
The following example creates a relationship and associates a primary entity with a collection of related entities.
5050

51+
:::code language="csharp" source="~/../PowerApps-Samples/dataverse/orgsvc/CSharp-NETCore/Relationships/AssociateDisassociate/Program.cs" id="AssociateDisassociate":::
52+
Complete code sample: [AssociateDisassociate](https://github.com/microsoft/PowerApps-Samples/blob/master/dataverse/orgsvc/CSharp-NETCore/Relationships/AssociateDisassociate/Program.cs)
5153

52-
```csharp
53-
54-
// Retrieve the accounts
55-
var query = new QueryByAttribute("account")
56-
{
57-
ColumnSet = new ColumnSet("name")
58-
};
59-
query.AddAttributeValue("address1_city", "Redmond");
60-
61-
EntityCollection accounts = svc.RetrieveMultiple(query);
62-
63-
//Convert the EntityCollection to a EntityReferenceCollection
64-
var accountReferences = new EntityReferenceCollection();
65-
66-
accounts.Entities.ToList().ForEach(x => {
67-
accountReferences.Add(x.ToEntityReference());
68-
});
69-
70-
// The contact to associate to the accounts
71-
var jimGlynn = new EntityReference("contact",
72-
new Guid("cf76763a-ba1c-e811-a954-000d3af451d6"));
73-
74-
// The relationship to use
75-
var relationship = new Relationship("account_primary_contact");
76-
77-
// Use the Associate method
78-
svc.Associate(jimGlynn.LogicalName, jimGlynn.Id, relationship, accountReferences);
79-
```
80-
Although there is no particular advantage in doing so, if you wanted to use the <xref:Microsoft.Xrm.Sdk.Messages.AssociateRequest>, you can replace the last line with this:
81-
54+
If you wanted to use the <xref:Microsoft.Xrm.Sdk.Messages.AssociateRequest>, you would use the following code. A benefit of using the request instead of the service client method is that the request supports the [use of optional parameters](../optional-parameters.md).
8255

8356
```csharp
84-
// Use AssociateRequest
8557
AssociateRequest request = new AssociateRequest()
8658
{
87-
RelatedEntities = accountReferences,
88-
Relationship = relationship,
89-
Target = jimGlynn
59+
RelatedEntities = relatedEntities,
60+
Relationship = relation,
61+
Target = primaryEntity
9062
};
9163

92-
svc.Execute(request);
64+
service.Execute(request);
9365
```
9466

95-
This operation is the same as three separate update operations to the [Account](../reference/entities/account.md).[PrimaryContactId](../reference/entities/account.md#BKMK_PrimaryContactId) lookup column, but it is using the [account_primary_contact](../reference/entities/contact.md#BKMK_account_primary_contact) relationship, which is a many-to-one entity relationship on the account and a one-to-many entity relationship on the contact.
67+
Let's say we are associating a primary entity (a contact) with three related entities (accounts). This single association operation shown above is the same as three separate update operations where the [Account](../reference/entities/account.md).[PrimaryContactId](../reference/entities/account.md#BKMK_PrimaryContactId) lookup column is set. Instead, the simpler service client method or request call is using the [account_primary_contact](../reference/entities/contact.md#BKMK_account_primary_contact) relationship which establishes a many-to-one entity relationship on each related account and a one-to-many entity relationship on the contact.
9668

9769
If you examine the properties of the relationship columns, you can see that the `ReferencingEntity` value is `account` and the `ReferencingAttribute` value is `primarycontactid`.
9870

99-
10071
## Use the Disassociate method or DisassociateRequest
10172

10273
The <xref:Microsoft.Xrm.Sdk.IOrganizationService>.<xref:Microsoft.Xrm.Sdk.IOrganizationService.Disassociate*> method or the <xref:Microsoft.Xrm.Sdk.Messages.DisassociateRequest> with the <xref:Microsoft.Xrm.Sdk.IOrganizationService>.<xref:Microsoft.Xrm.Sdk.IOrganizationService.Execute*> method are just the reverse of the way that you associate table rows.
10374

104-
The following code reverses the associations made in the sample above.
105-
106-
107-
```csharp
108-
// Retrieve the accounts
109-
var query = new QueryByAttribute("account")
110-
{
111-
ColumnSet = new ColumnSet("name")
112-
};
113-
query.AddAttributeValue("address1_city", "Redmond");
114-
115-
EntityCollection accounts = svc.RetrieveMultiple(query);
116-
117-
//Convert the EntityCollection to a EntityReferenceCollection
118-
var accountReferences = new EntityReferenceCollection();
119-
120-
accounts.Entities.ToList().ForEach(x => {
121-
accountReferences.Add(x.ToEntityReference());
122-
});
123-
124-
// The contact to associate to the accounts
125-
var jimGlynn = new EntityReference("contact",
126-
new Guid("cf76763a-ba1c-e811-a954-000d3af451d6"));
127-
128-
// The relationship to use
129-
var relationship = new Relationship("account_primary_contact");
130-
131-
// Use the Disassociate method
132-
svc.Disassociate(jimGlynn.LogicalName, jimGlynn.Id, relationship, accountReferences);
133-
```
134-
Although there is no particular advantage in doing so, if you wanted to use the <xref:Microsoft.Xrm.Sdk.Messages.DisassociateRequest>, you can replace the last line with this:
75+
You can view an example `Disassociate` method call in the previously shown code sample. If you wanted to use the <xref:Microsoft.Xrm.Sdk.Messages.DisassociateRequest>, the code would look like this:
13576

13677
```csharp
137-
// Use DisassociateRequest
13878
DisassociateRequest request = new DisassociateRequest()
13979
{
140-
RelatedEntities = accountReferences,
141-
Relationship = relationship,
142-
Target = jimGlynn
80+
RelatedEntities = relatedEntities,
81+
Relationship = relation,
82+
Target = primaryEntity
14383
};
14484

145-
svc.Execute(request);
85+
service.Execute(request);
14686
```
14787

14888
### See also

powerapps-docs/developer/data-platform/org-service/samples/work-activity-party-records.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ contributors:
1818
This sample code shows how to work with activity party records.
1919

2020
> [!div class="nextstepaction"]
21-
> [SDK for .NET: Work with activity party records sample code](https://github.com/microsoft/PowerApps-Samples/tree/master/dataverse/orgsvc/CSharp/ActivityPartyRecords)
21+
> [SDK for .NET: Work with activity party records sample code](https://github.com/microsoft/PowerApps-Samples/tree/master/dataverse/orgsvc/CSharp-NETCore/Activities/ActivityParty)
2222
2323
## How to run this sample
2424

0 commit comments

Comments
 (0)