Sunday 5 July 2020

Consume Microsoft Dynamics NAV/BC ODATA using .NET C#

I meddled into consuming NAV Odata using C# this evening.

The walkthrough followed as documented by Microsoft: Walkthrough: Creating and Interacting with a Page Web Service (OData)

I had made some changes as my request is quite different and below is what I did:

Tools Used

  1. VS 2017
  2. OData Connected Service.
    1. Download from here: OData Connected Service OR
    2. Download from VS => Tools => Extension and Updates => Search for OData Connected Service.
Steps
  • Expose the Odata URL/endpoint on NAV/BC
  • Get Authorization Code: I used Postman to get this.

  • Create Console Application on VS2017
  • Add Connected Service to the project
  • Enter Service Name, Address, Authorization header


  • Use the necessary references and namespace of the service

  • See code sample

using System;using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using NAV;
namespace NAVConsole
{
class Program
{
static void Main(string[] args)
{
String username = "Test";
String password = "your password";
NAV.NAV nav = new NAV.NAV(new Uri("http://<servercomputer>:<odataport>/<serverinstance>/ODataV4/Company('PILOT')"));
nav.Credentials = new NetworkCredential(username, password);
PrintSalaryAdvances(nav);
Console.ReadLine();
}
private static void PrintSalaryAdvances(NAV.NAV nav)
{
var salAdv = from s in nav.StockAutomationCard
select s;
foreach (var item in salAdv.ToList())
{
Console.WriteLine($"Requester: {item.User_ID}, No: {item.No}");
}
}
}
}


  • Result


No comments:

Post a Comment