1. NAV AND CODEUNIT WEBSERVICES
Recently I delivered project to customer using nav codeunit web services, and I want to share what did I do on this Project. Thank for Dimitry Katson for couraging me about sharing this post.
Why Codeunit Webservices. There is no sense about this but the customers web developers want only one end point so here we are.
First things always first.
Xmlports : I think that is the only way to export objects to webservices.
Simple Item export with sales prices and Serial numbers also including Item Picture Item Inventory.
There is some restiriction when you are creating XmlPorts.
On properties select UseDefaultNamespace to Yes
**If you miss this step your web service throw Namespace Error
You can not use Blobfield as xmlport field you have to parse it to string
You have to add First Elemet As Text.
After you create your xmlport now you are ready to go.
2. WEBSERVICE FROM CODE UNIT
Create a Codeunit and add code like below
GetItems(VAR ItemExport : XMLport ItemExport)
Item.SETRANGE("Web Item",TRUE);
ItemExport.SETTABLEVIEW(Item);
You can see we have var variable for return of web service function and then we filter Global Item variable then settableview of export object done! Now you can export your data to web endoint.
But if you want to filter sub Table data you can not reach it from here you have to filter it on Xmlport
3. ADD CODE UNIT TO WEB SERVICES
Now we have to add our codeunit to NAV webservices.
Goto Web Service Page(810)
And add new Codeunit, your codeunit id and Give a proper service name,
After this Nav gives you link for this web service copy it and place on internet Explorer hit enter. If you see something like this. You did everything correct.
Now we can pass the last step.
4. CONSUMING WEB SERVICE FROM C# APPLICATION
Open my old friend visual studion and create new c# form application add grid and button control etc.
Then on service reference right click and select add service reference.
Then enter the url of your web service and then click add. It asks you username and password please enter authorized nav user name and password. Then asks password again and again you have to enter username and password for 3 time. Normally I should ask for one time
Give Service reference a name.
private void createclient()
{
string baseURL = @"your nav service url";
BasicHttpBinding navBinding = new BasicHttpBinding();
navBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
navBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
navBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Windows;
navBinding.MaxReceivedMessageSize = Int32.MaxValue;
navBinding.ReceiveTimeout = new TimeSpan(1,0,0);
navBinding.MaxBufferSize = Int32.MaxValue;
EndpointAddress navendpoint = new EndpointAddress(baseURL);
client = new ECommerceWebService_PortClient(navBinding, navendpoint);
client.ClientCredentials.Windows.ClientCredential = new NetworkCredential("Username", "password", "");
}
This code gives you a connected client instance to NAV on your company. You have to change your url for other companies.
And then the last one get Items.
ItemElement itemroot = new ItemElement();
client.GetItems(ref itemroot);
webServiceGridControl.DataSource = itemroot.Item;
Itemroot variable is filled with your Items.
If you want to Insert Sales Order
SalesHeader sorder = new SalesHeader();
sorder.DocumentType = "Order";
sorder.SellToCustomerNo = "M.000001";
SalesLine sline = new SalesLine();
List<SalesLine> slineList = new List<SalesLine>();
sline.DocumentType = sorder.DocumentType;
sline.Type = "Item";
sline.No = "PRX_MI_SERIAL_ITEM";
sline.LocationCode = "MERKEZ";
sline.UnitofMeasureCode = "ADET";
sline.LineNo = 10000;
sline.UnitPrice = "1.00";
sline.Quantity = "100";
sline.CurrencyCode = "";
slineList.Add(sline);
sorder.SalesLine = slineList.ToArray();
client.AddSalesOrder(sorder);
You can see all nav object from my git address. I will upload c# application asap
*This post is locked for comments