Get data from Odata service Business Central

191 Views Asked by At

I want to consume Business Central Odata service from ASP.NET project.

This is Odata URI: http://windowsbc:7048/BC230/ODataV4/Company('Copia%20Cronus')/Power_BI_Vendor_List

This is result in Postman: "@odata.context": "http://windowsbc:7048/BC230/ODataV4/$metadata#Company('Copia%20Cronus')/Power_BI_Vendor_List", "value": [ { "Vendor_No": "01254796", "Vendor_Name": "Progressive Home Furnishings", "Balance_Due": 169430.59, "Posting_Date": "2024-12-31", "Applied_Vend_Ledger_Entry_No": 0, "Amount": 169430.59, "Amount_LCY": 170260.8, "Transaction_No": 99, "Entry_No": 71, "Remaining_Pmt_Disc_Possible": 0 }, etc.

this is my C# code:

var credentials = new NetworkCredential("johon", "myPassword");
var settings = new ODataClientSettings("http://windowsbc:7048/BC230/ODataV4/Company('Copia%20Cronus')/Power_BI_Vendor_List/", credentials);
var client = new ODataClient(settings);            
var Vendor = await client.For("Power_BI_Vendor_List").FindEntriesAsync();

but it return this error:

WebRequestException: The request URI is not valid. Since the segment 'Power_BI_Vendor_List' refers to a collection, this must be the last segment in the request URI or it must be followed by an function or action that can be bound to it otherwise all intermediate segments must refer to a single resource.

How I can solve?

1

There are 1 best solutions below

2
On

I believe the settings for the ODataClient should be initialized with the base URL and not the full URL to the resource you want to consume:

var settings = new ODataClientSettings("http://windowsbc:7048/BC230/ODataV4/Company('Copia%20Cronus')", credentials);

The way you are doing it now ends up with a double specification of the resource i.e.:

http://windowsbc:7048/BC230/ODataV4/Company('Copia%20Cronus')/Power_BI_Vendor_List/Power_BI_Vendor_List

Your feedback in the comment below suggests that Power_BI_Vendor_List needs to be part of the URL when initializing the settings.

Try adding it again but without a / after Power_BI_Vendor_List:

var settings = new ODataClientSettings("http://windowsbc:7048/BC230/ODataV4/Company('Copia%20Cronus')/Power_BI_Vendor_List", credentials);