Tuesday, 7 October 2008

Webservices in NAV2009

Anyone that has been reading up on NAV2009 cannot have failed to have noticed that NAV can now publish Webservices. I know clever developers have been building Webservices for NAV for a while now, but they have been building them from scratch. NAV2009 gives a simple form where you can expose a Codeunit, or a Page(the new object type used by the .NET client).
One of the examples in the Application Developers Guide publishes a Page built against the Customer Table. The example then goes on to build a simple console app that inserts, Modifies and deletes a customer record, displaying the results between each stage.
Now I know very little C# and the example has a number of bugs in it but even I have managed to get this work and add some very basic error catching in a few hours. One of the devs here told me it would have taken him somewhere between a day and 2 days top achieve the same in the past.

So the example I am talking about.
In NAV open Object Designer and run form 810
Add Page 21 and call it Customer

In Visual Studio 2008 I started a ConsoleApp Project.
I then added a reference to the WebService by Right Clicking Service References and selecting Add Service Reference. I then Clicked the Advanced Button

I then clicked Add Web Reference

I then add the webservice and named it WebService(not very original I know)

Once I had this I could start with the C# Code. Admittedly most of this was copy -  pasted from the example. But as I said there are a few bugs that I had to resolve, plus I wanted some simple error trapping
   1: using System;








   2: using System.Collections.Generic;








   3: using System.Text;








   4:  








   5: namespace ConsoleApplication1








   6: {








   7:    // import newly generated Web service proxy








   8:    using WebService;








   9:   








  10:    class Program








  11:    {








  12:        static void Main(string[] args)








  13:        {








  14:            //create instance of service and setting credentials








  15:            Customer_Service service = new Customer_Service();








  16:            service.UseDefaultCredentials = true;








  17:  








  18:            //create instance of customer








  19:            Customer cust = new Customer();








  20:            cust.Name = "Customer Name";








  21:            //Next line will throw an error. Comment out to get successful passing








  22:            cust.Address = "12345678901234567890123456789012345678901234567890123456";








  23:            Msg("Pre Insert");








  24:            PrintCustomer(cust);








  25:           








  26:            //insert customer








  27:            //service.Insert is wrong. Correct and added a Try/Catch for errors








  28:            //service.Insert(ref cust);








  29:            try








  30:            {








  31:                service.Create(ref cust);








  32:            }








  33:            catch (Exception ex)








  34:            {








  35:                Console.WriteLine(ex.Message);








  36:            }








  37:            Msg("Post Insert");








  38:            PrintCustomer(cust);








  39:  








  40:            //create filter for searching for customers








  41:            List<Customer_Filter> filter = new List<Customer_Filter>();








  42:            Customer_Filter nameFilter = new Customer_Filter();








  43:            nameFilter.Field = Customer_Fields.Name;








  44:            nameFilter.Criteria = "C*";








  45:            filter.Add(nameFilter);








  46:  








  47:            Msg("List before modification");








  48:            PrintCustomerList(service, filter);








  49:  








  50:            cust.Name = cust.Name + "Modified";








  51:            //service.Modify is wrong. Correct and added a Try/Catch for errors








  52:            //service.Modify(ref cust);








  53:            try








  54:            {








  55:                service.Update(ref cust);








  56:            }








  57:            catch (Exception ex)








  58:            {








  59:                Console.WriteLine(ex.Message);








  60:            }








  61:  








  62:  








  63:            Msg("Post Modify");








  64:            PrintCustomer(cust);








  65:  








  66:            Msg("List after modification");








  67:            PrintCustomerList(service, filter);








  68:            try








  69:            {








  70:                service.Delete(cust.Key);








  71:            }








  72:            catch (Exception ex)








  73:            {








  74:                Console.WriteLine(ex.Message);








  75:            }








  76:  








  77:  








  78:            Msg("List after deletion");








  79:            PrintCustomerList(service, filter);








  80:  








  81:            Msg("Press [ENTER] to exit program!");








  82:            Console.ReadLine();








  83:        }








  84:  








  85:        static void PrintCustomerList(Customer_Service service, List<Customer_Filter> filter)








  86:        {








  87:            Msg("Printing Customer List");








  88:  








  89:            //conduct the actual search








  90:            //service.Find is wrong. Correct and added a Try/Catch for errors








  91:            //Customer[] list = service.Find(filter.ToArray(), null, 100);








  92:            Customer[] list = service.ReadMultiple(filter.ToArray(), null, 100);








  93:           








  94:            foreach (Customer c in list)








  95:            {








  96:                PrintCustomer(c);








  97:            }








  98:            Msg("End of List");








  99:        }








 100:  








 101:        static void PrintCustomer(Customer c)








 102:        {








 103:            Console.WriteLine("No: {0} Name: {1}", c.No, c.Name);








 104:        }








 105:  








 106:        static void Msg(string msg)








 107:        {








 108:            Console.WriteLine(msg);








 109:        }








 110:    }








 111: }








 112:  
















This is an area I think will make my life a lot easier as I get involved in integration projects fairly often. 








As I get more knowledge I'll post up more examples.

No comments: