Influence Simple.OData.Client @odata.bind collection route

111 Views Asked by At

Introduction

I am writing a .NET app where I'm connecting to Dynamics and trying to update a record. I'm making use of the Simple.OData.Client package v6.0.1 (repo here). I've got a model where I've got a lookup property that I'd like to update together with other properties of my model. Here are my classes:

public class UpdateRequest
{
    [JsonProperty("msdyn_systemstatus")]
    public required Status Status { get; set; }

    [JsonProperty("msdyn_substatus")]
    public required Substatus Substatus { get; set; }

    //more properties
}

Status is an enum (choice) and Substatus a lookup field which has the following class definition:

[DataContract(Name = "msdyn_itemsubstatus")]
public class Substatus
{
    [JsonProperty("msdyn_substatusid")]
    public Guid? Id { get; set; }

    [JsonProperty("msdyn_name")]
    public string? Name { get; set; }
}

My update code is simple:

IODataClient _odataClient; //injected in constructor
var result = await _odataClient.For<Item>()
    .Key(id)
    .Set(updateRequest) // Status and Substatus.Id are filled, Substatus.Name is null
    .UpdateEntryAsync(true);

When logging the request, I see the following is created:

{
    "@odata.type": "#Microsoft.Dynamics.CRM.msdyn_parententity",
    "msdyn_systemstatus": 690970000,
    "[email protected]": "{dynamicsbaseurl}/msdyn_itemsubstatus(insertguid)"
}

The problem

The generated url in the msdyn_substatus property refers to the singular form of the related entity and therefore the update fails. When doing the same update request through postman with the plural form, it works.

I've done quite a bit of research but haven't found any code examples of how to influence the collection name used and also can't seem to pinpoint where in the library it's being done. Any help understanding how the serialization works and can be influenced (maybe an attribute of sorts?) would be greatly appreciated.

1

There are 1 best solutions below

0
On

The dataverse web api uses ‘collection name’ for addressing entities. You can get the name of a table/entity from maker studio, or using the web api itself. See https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/web-api-samples.

That said, if your using .net, you may want to consider using the dataverse service client which provides and IOrganizationService interface for communicating to datavers. It also works with the pac modelbuilder build strong types, which allow you to create types for tables, custom apis and such. https://github.com/microsoft/PowerPlatform-DataverseServiceClient