38
Chapter 10: Web Services 10-1 CHAPTER 10: WEB SERVICES Objectives The objectives are: Aware of how Microsoft Dynamics ® NAV supports Web services. Evaluate the benefits of Web services over other integration options in Microsoft Dynamics NAV. Understand how to expose codeunit and page objects as Web services. Consume Web services using functions developed through Visual Studio and Infopath. Introduction The World Wide Web Consortium (W3C), which is regarded by the industry as the keeper of Web standards (including XML and Web services), describes Web services as follows "Web services provide a standard means of interoperating between different software applications, running on a variety of platforms and/or frameworks. Web services are characterized by their great interoperability and extensibility, as well as their machine-processable descriptions thanks to the use of XML. They can be combined in a loosely coupled way in order to achieve complex operations. Programs providing simple services can interact with each other in order to deliver sophisticated added-value services." (source: http://www.w3.org/2002/ws/activity (http://www.w3.org/2002/ws/activity) "Web Services" as of January 2009) MSDN Online has the following description of XML Web services: "XML Web services are the fundamental building blocks in the move to distributed computing on the Internet. Open standards and the focus on communication and collaboration among people and applications have created an environment where XML Web services are becoming the platform for application integration. Applications are constructed using multiple XML Web services from various sources that work together regardless of where they reside or how they were implemented." (source: http://msdn.microsoft.com/en-us/library/ms996507.aspx (http://msdn.microsoft.com/en-us/library/ms996507.aspx) "XML Web Services Basics" Web Services Technical Articles as of January 2009) Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Na2009 enus devii_10

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Na2009 enus devii_10

Chapter 10: Web Services

10-1

CHAPTER 10: WEB SERVICES Objectives

The objectives are:

• Aware of how Microsoft Dynamics® NAV supports Web services. • Evaluate the benefits of Web services over other integration options

in Microsoft Dynamics NAV. • Understand how to expose codeunit and page objects as Web

services. • Consume Web services using functions developed through Visual

Studio and Infopath.

Introduction The World Wide Web Consortium (W3C), which is regarded by the industry as the keeper of Web standards (including XML and Web services), describes Web services as follows

"Web services provide a standard means of interoperating between different software applications, running on a variety of platforms and/or frameworks. Web services are characterized by their great interoperability and extensibility, as well as their machine-processable descriptions thanks to the use of XML. They can be combined in a loosely coupled way in order to achieve complex operations. Programs providing simple services can interact with each other in order to deliver sophisticated added-value services."

(source: http://www.w3.org/2002/ws/activity (http://www.w3.org/2002/ws/activity) "Web Services" as of January 2009)

MSDN Online has the following description of XML Web services:

"XML Web services are the fundamental building blocks in the move to distributed computing on the Internet. Open standards and the focus on communication and collaboration among people and applications have created an environment where XML Web services are becoming the platform for application integration. Applications are constructed using multiple XML Web services from various sources that work together regardless of where they reside or how they were implemented."

(source: http://msdn.microsoft.com/en-us/library/ms996507.aspx (http://msdn.microsoft.com/en-us/library/ms996507.aspx) "XML Web Services Basics" Web Services Technical Articles as of January 2009)

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 2: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-2

Web services are a standardized way for independent software systems to communicate with one another over standard Internet protocols. XML is used to define the interface as well as the data that is exchanged by the interface. Web services architecture is designed to allow programs to communicate with each other that may be written in different languages or even on different platforms, in a language-independent, platform-independent manner.

Different software manufacturers may have different implementations of Web services, and there may be minor differences in the way that their interfaces are developed. A few things, however, are common to most of these definitions:

• Functionality is exposed through a standard web protocol, such as HTTP or SOAP (Simple Object Access Protocol, an XML based protocol)

• The interface is described by using a common Web services Description Language (WSDL), which uses XML.

• Public Web services are registered with Universal Discovery Description and Integration (UUDI).

Microsoft Dynamics NAV 2009 allows business logic to be published as Web services, which makes integrating Microsoft Dynamics NAV with other systems much easier than previously. Instead of having to develop additional components to integrate with Microsoft Dynamics NAV business logic, it is now possible to publish entire codeunits and pages as Web pages. Implementation of the Web services capability requires use of the three tier configuration.

To learn more about Web Services and Microsoft products, visit http://msdn.microsoft.com/en-us/library/ms996507.aspx for additional information.

Web Services Overview Web services use XML to describe machine-readable metadata. This metadata is used to describe the message interchange formats that a Web service supports, and the valid message exchange patterns of the Web service itself, as well as the capabilities and requirements of a service.

Web Services Description Language (WSDL), an XML-based language for defining Web services, is used to express the interchange formats and message exchange patterns of the Web services. Many major software development environments, such as Microsoft Visual Studio, can be used to build applications that use Web services. Because Web services are XML based, Web services can be built across platforms and programming languages.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 3: Na2009 enus devii_10

Chapter 10: Web Services

10-3

Web Services Architecture

With Microsoft Dynamics NAV 2009 it is possible to publish codeunits and pages as Web services, providing access to internal business logic in a standard format, using standard protocols. External systems can read and write data on pages and call codeunits as defined by the common Web service protocols, with correct authentication and authorization.

With the Web services that can be published with Microsoft Dynamics NAV 2009, it is possible to provide the same functionality to an external application that is used within the application. The credentials of the application used to consume a Web service in Microsoft Dynamics NAV 2009 must be set up with the proper permissions to access the information.

Microsoft Dynamics NAV Web services are useful to customers and partners who want to use business logic or use a standard interface to access data from outside Microsoft Dynamics NAV.

FIGURE 10.1 MICROSOFT DYNAMICS NAV 2009 THREE-TIER ARCHITECTURE

Microsoft Dynamics NAV Web Services

There are several simple types of Web services that can be published by Microsoft Dynamics NAV. All Web services can run C/AL code and validation triggers.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 4: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-4

The simplest Web service type uses the page object. By publishing a page object, Microsoft Dynamics NAV constructs a Web service that has a set of default database methods, such as Create, Read, Update and Delete operations. Publishing Pages as Web services provides indirect access to the validation triggers in the underlying tables.

Another Web service type uses codeunits and the functions that are defined within those codeunits. Publishing a codeunit provides the functions in the codeunit as methods of the Web service.

The last type of Web service includes the ability to pass complex data types by using an XMLport object as a parameter in a codeunit function.

Pages and/or codeunits can be published as a Web service simply by inserting a reference to the page or codeunit into the Web service table, and marking it as a published Web service. They are immediately published and available for Web service requests over the network. It could not be simpler.

The Microsoft Dynamics NAV Business Web Services server application is implemented as a Windows service that can simply be installed on the server. The complexity normally faced in manually setting up the framework, and managing the WSDL description is taken care of by this service.

Consumers of these Web services, which are systems integrating with Microsoft Dynamics NAV, only have to know the network name (or address) of the computer that is running Microsoft Dynamics NAV Server and the names given to the individual pages and codeunits.

For example, with the following configuration:

• Computer name that runs Microsoft Dynamics NAV Service: NAV_SRV_01

• Web Service port: 7047 • Microsoft Dynamics NAV instance: DynamicsNAV • Company Name: CRONUS International Ltd. • The published page Web service: MyCustomer

Then the MyCustomer Web service is available at the following URL:

http://NAV_SRV_01:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Page/MyCustomer (http://nav_srv_01:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Page/MyCustomer)

Microsoft Dynamics NAV manages Web service requests exactly like it handles requests from end-users within the Classic client or the RoleTailored client. User rights authorization and validation, input data validation, business logic invocation, and concurrency control are all managed in the same manner as requests from a Microsoft Dynamics NAV client.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 5: Na2009 enus devii_10

Chapter 10: Web Services

10-5

This guarantees that the integrity of the Microsoft Dynamics NAV data is not compromised by using Web services. It also means code which validates data, or invokes business logic for systems using the Web services that are provided by Microsoft Dynamics NAV do not have to be replicated.

Writing C/AL Code for Web Services

The code for Web services cannot interact with the client that called the code. It cannot respond to a dialog box or any other client interaction requests. Running code with client interaction causes an exception to be created. The exception can be caught and handled, but the Web service task will not be completed. When writing code for Web services, avoid using any end-user confirmation dialog pages or message pages.

On occasion, standard business logic includes some degree of user interaction. That must be avoided when the logic is published as a Web service. To detect such a situation, the GUIALLOWED function can be used, and then default responses can be implemented in the business logic. When code is executed from a Web service, this keyword is always set to FALSE, just as it is when code is executed from within the Microsoft Dynamics NAV Application Server (NAS).

For instance, standard business logic might include a confirmation like this:

OK := CONFIRM(‘Do you wish to continue?’);

If this functionality were to be published as a Web service, it is extended as follows:

IF GUIALLOWED THEN OK := CONFIRM(‘Do you wish to continue?’) ELSE OK := TRUE;

When GUIALLOWED equals TRUE, meaning a user is running this code from the Classic client or the RoleTailored client, the user has a choice. If GUIALLOWED equals FALSE, meaning the code is executed as a Web service or by NAS, a default value is assumed for the CONFIRM function.

Web Service Opportunities Integration with other systems and communicating with different applications other than Microsoft Dynamics NAV is often a challenge. In many cases, customers need to integrate their Microsoft Dynamics NAV system with other systems in use within their organization. In some instances, they may have a requirement to exchange data with entities outside their organization, entities which may use another system or even a different platform and operating system.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 6: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-6

Historical Integration Option

Historically, Microsoft Dynamics NAV offers some integration possibilities by using the following technologies:

• C/Front • C/ODBC • OCX • Automation • NAV Application Server (NAS

These integration options have their own pros and cons, and choosing the best technology is case dependent.

With the release of Microsoft Dynamics NAV 2009, Web services have been added to provide a new method of integrating with the system. Because Web services use industry standard protocols and technologies, there are many more possibilities.

As a developer in Microsoft Dynamics NAV, it is very important to understand Web services. It is intended to be the new basis for cooperative efforts with developers from other technologies. To be able to use Web service properties in the right context will be a very important and useful skill.

Suitable Candidates for Web Services

Web services are suited for communication across platforms and programming languages. Microsoft Dynamics NAV 2009 makes is easy to integrate with other systems and benefit from the flexibility that Web services offers. The following are candidates for Web services:

• Solutions to execute business logic or read data from Microsoft Dynamics NAV.

• Solutions to write data to Microsoft Dynamics NAV and use the system to validate the data with the existing business logic.

• Solutions to extend Microsoft Dynamics NAV with additional information (such as: customer information, exchange rates, or product information), and have that extension be accessed from other systems.

Creating, Exposing and Consuming Web Services Microsoft Dynamics NAV supports page objects, codeunits, and XMLport objects passed as parameters in a codeunit function to be published as Web services.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 7: Na2009 enus devii_10

Chapter 10: Web Services

10-7

Reusable with Graphic

An example of the usage of Web services in Microsoft Dynamics NAV is publishing a Web service that lists all customers and has that Web service immediately available for authorized requests over the network. Web services allow for easy communication and data exchange in a secured environment.

The first labs in this chapter will look at very simple Web services, by reviewing how pages and codeunits can be published as Web services, and by creating a few simple console applications to consume these Web services.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 8: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-8

Lab 10.1 - Creating a Web Service Web services provide a new, industry standard way of providing access to Microsoft Dynamics NAV data and the associated processes. It is logical to expect that in the future, seminar registrations will be handled at some level via Internet access. Because Web services is a new capability, some experimentation is required to learn more about how to expose Web services so they may be used by an external application.

Scenario

In this lab, you will learn how to:

• Register a codeunit Web service. • Register a page Web service. • Publish the Web services.

The following steps show how to create a simple codeunit, with a function that converts an input string to all upper case characters.

Step by Step

In the Microsoft Dynamics NAV Classic Client:

1. Click Tools > Object Designer. The Object Designer opens. 2. Click the Codeunit button to pen the Codeunit list. 3. Click New. The C/AL Editor opens. 4. Click View > C/AL Globals. The C/AL Global window opens. 5. Click the Functions tab, and type the following:

o Name: Capitalize

6. Click the Locals button. The C/AL Locals window opens. 7. In the Parameters tab, enter the following:

o Name: InputString o DataType: Text o Length: 250

8. Click the Return Value tab, and enter the following:

o Name: OutputString o ReturnType: Text o Length: 250

9. Close the C/AL Locals window and then close the C/AL Globals

window. 10. In the Capitalize function, type the following:

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 9: Na2009 enus devii_10

Chapter 10: Web Services

10-9

OutputString := UPPERCASE(InputString);

11. Save the codeunit by clicking File > Save As. The Save As dialog box opens.

12. Enter 123456710 in the ID and "Capitalize" in the Name and then click OK. The codeunit now looks like the following image.

FIGURE 10.2 THE CAPITALIZE CODEUNIT

13. Close the C/AL editor.

After the codeunit is created and saved, register it in the Web Services form and select the Published check box. The following steps show how to register Web services. In the Object Designer:

14. Click the Form button to open the Form list. 15. Select form 810, Web Services and then click Run. The Web

Services form opens. 16. Type the lines as shown in the following table. The first line registers

the codeunit Web service that is created in the previous steps. The second line registers a page Web service, the Customer page.

Object Type Object ID Service Name

Codeunit 92010 CapitalizeCU

Page 21 CustomerPage

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 10: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-10

17. Check the Published check box on both lines to publish the Web services. The Web Services form now looks like the following image:

FIGURE 10.3 THE WEB SERVICES FORM

When the Web service is marked as published in the Web Services form, it is immediately available for requests over the network.

18. Close the Web Services form.

Microsoft Dynamics NAV Web Services generate and publish the WSDL automatically on the Server. The following steps show how to browse to the WSDL document to ensure the Web services are available.

19. Start Internet Explorer. 20. In the Address bar, enter the following:

http://localhost:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Services

NOTE: This address is based on the name of the computer that is running the Microsoft Dynamics NAV Service, and the company that is used. The company name is case sensitive. The example used "localhost" instead of the computer name.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 11: Na2009 enus devii_10

Chapter 10: Web Services

10-11

21. The page lists the Web services registered and published in previous steps.

FIGURE 10.4 THE WEB SERVICES LISTED IN INTERNET EXPLORER

22. Close Internet Explorer.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 12: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-12

Lab 10.2 - Create a Console Application to Consume the Codeunit Web Service

Now that Web service functions have been published (aka “exposed”), the next step is to create an application to use (aka “consume”) that published service in an external application.

Scenario

In this lab you will learn to create a console application to consume the Capitalize codeunit Web service. The following steps show how to create a console application in Visual Studio to consume the codeunit Web service.

Step by Step

1. Open Visual Studio. 2. Click File > New, and then click Project. The New Project window

opens. 3. Expand the Visual C# node, and select Console Application. 4. Enter "UseCapitalizeCU" as the Name of the console application. 5. Click OK. Visual Studio now generates the default files and

references that make up the UseCapitalizeCU project, including a code module with the proper name space and general construct to enter the C# code.

To be able to use the published Web service in C# code, the Web service must be added as a Web reference into the project in Visual Studio:

6. In the Solution Explorer, right-click the References node in the project and then click Add Web Reference. The Add Web Reference window opens.

NOTE: Adding a Web Reference directly from the References node in the object explorer is a function of the .NET framework version 2.0. Visual Studio 2008 is installed with a higher version of the .NET framework (by default), and this selection is not available directly from the Reference node. If this is the case, select "Service Reference" and then click the "Add Web Reference" button in the Service Reference Settings window. This will open the same dialog box that can be used to define the Web reference.

7. Enter the following in the URL bar:

http://localhost:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Services

NOTE: This address is the same as that address used when checking the WSDL by using Internet Explorer.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 13: Na2009 enus devii_10

Chapter 10: Web Services

10-13

8. Click GO. The CapitalizeCU service is displayed together with other services that are available.

FIGURE 10.5 THE ADD WEB REFERENCE WINDOW

9. Click View Service for the CapitalizeCU service and then click Add

Reference.

FIGURE 10.6 THE CAPITALIZECU WEB SERVICE DESCRIPTION

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 14: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-14

The namespace and the main constructs for the console application are created, and the proper reference to the Web service is added to the project. What remains is to write the code that consumes the Web service. The following steps show how to write C# code to consume the Web service. For detailed information about C# syntax, refer to the proper documentation, which is available from the online help system in Visual Studio, as well as many references on the Internet, such as MSDN online and other online communities.

NOTE: Many people are working on Web service projects who may have run into similar problems, and often offer to share their solutions in online communities. Online communities should be used since they are excellent resources for peer support.

10. Find the program.cs tab. If it is not displayed in the code section, it can be opened by double-clicking it in the project explorer. In the UseCapitalizeCU namespace above the line where the Program class starts, add the Web reference, by typing the following:

using localhost; // using the web reference

11. In the Main function, write the code that consumes the Web service:

// Create a new instance of the service CapitalizeCU ws = new CapitalizeCU();

// Authenticating against Microsoft Dynamics NAV ws.UseDefaultCredentials = true;

// Declare the variables to use string InputString =""; string OutputString ="";

Console.WriteLine("Enter a lowercase sentence, and press [ENTER]"); InputString = Console.ReadLine();

// Call the codeunit Web service OutputString = ws.Capitalize(InputString);

// Write output to the screen Console.WriteLine("Result: {0}", OutputString);

// Keep the console window up until you press ENTER Console.ReadLine();

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 15: Na2009 enus devii_10

Chapter 10: Web Services

10-15

12. Click File, Save All to save all the changes in the project.

FIGURE 10.7 THE CODE IN VISUAL STUDIO

13. Press F5 to run the application. The console window opens. 14. Write something with lower case characters, such as: "this is a

lowercase sentence." 15. Press ENTER. The console application now looks like the following

image:

FIGURE 10.8 THE CONSOLE APPLICATION RESULT

The input string 'this is a lowercase sentence' is capitalized by using the codeunit Web service that is running on Microsoft Dynamics NAV Service. The result in the output string is 'THIS IS A LOWERCASE SENTENCE.'

16. Press ENTER to close the console window.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 16: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-16

Page Web Services When a page object is published as a Web service, it exposes a number of very familiar standard methods. These methods are the basic database operations (Create, Read, Update and Delete) for the page’s source table. They correspond to familiar table triggers which fire when these database operations are executed. Page Web services enable quick access to standard Microsoft Dynamics NAV business logic that is contained within the page object, and indirectly, also the business logic that is implemented in the source table.

• The Create method accepts an XML document that represents a table record as a parameter, and returns the record after it is finished. As the record is inserted into the table, the code in the OnInsert trigger of the table is executed, invoking exactly the same processing logic that is invoked by either the Classic client or the RoleTailored client.

• The Update method accepts a record for the underlying table as a parameter and returns the modified record. As the record is modified, the code in the OnModify trigger of the table is executed.

• The Delete method takes the key value of the record as a parameter, and returns TRUE or FALSE. It executes the OnDelete code of the underlying table.

• The Read method accepts the primary key of the table as a parameter, and returns the record. This method is comparable to the GET method in C/AL. The ReadMultiple method takes a number of optional parameters, with the filter as the most important one, and it returns a list of records. This method is comparable to the FINDSET method in C/AL.

In addition to the standard database operations, page Web services also expose additional functionality, such as access to individual fields, and metadata that describe these fields. For more detailed information, please refer to the Microsoft Dynamics NAV Developer and IT Pro Help, which has extensive information about Web services.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 17: Na2009 enus devii_10

Chapter 10: Web Services

10-17

Lab 10.3 - Create a Console Application to Consume the Seminar Card Page Web Service

Codeunits published as a Web service provide access to all of the functions in the codeunit. Pages published as a Web service provide access to a set of functions that in turn access table data. Such accesses are controlled by the data validation rules that are built into the target table. The next step is to create an external application to consume Web services exposed by means of a published page.

Scenario

In Lab 10.1, two Web services were exposed and published. Lab 10.2 shows how to create a console application to consume the codeunit Web service. This lab shows how to create a console application to consume the page Web service. The application uses the standard database operation methods.

Step by Step

In Visual Studio:

1. Click File > New, and then click Project. The New Project window opens.

2. Expand the Visual C# node, and select Console Application. 3. Enter "UseSeminarPage" as the Name. 4. Click OK. The UseSeminarPage project initiates with default files

and references, and the placeholder for the C# code that will be added later.

The next step is to add the page Web service published as "Seminar" as a Web reference into the project. Begin in Visual Studio.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 18: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-18

5. Refer to Lab 10.2 for detailed instructions on how to add a Web Reference to the console application.

6. Instead of using the CapitalizeCU service, click View Service for the Seminar service and then click Add Reference.

FIGURE 10.9 THE SEMINAR WEB SERVICE DESCRIPTION

The namespace and the code placeholders are generated, and the reference to the Seminar Web service is already added to the project. The following steps show how to write the C# code to consume the page Web service. In Visual Studio:

7. Find the Program.cs tab, or open it from the project explorer. In the UseSeminarPage namespace, above the line where the Program class starts, add the Web reference, by entering the following C# code:

using localhost; // using the web reference

8. In the Main function, write the C# code that consumes the Web service:

// Create instance of service and set credentials Seminar_Service ws = new Seminar_Service(); ws.UseDefaultCredentials = true; // Create instance of a Seminar and ask for the name Msg("Enter a name for the new Seminar and press [ENTER]"); Seminar MySeminar = new Seminar(); MySeminar.Name = Console.ReadLine();

// Insert Seminar ws.Create(ref MySeminar); PrintSeminar(MySeminar);

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 19: Na2009 enus devii_10

Chapter 10: Web Services

10-19

Msg("The Web service inserted a new Seminar. Press [ENTER] to continue."); Console.ReadLine();

// Create filter for searching for seminars List<Seminar_Filter> filter = new List<Seminar_Filter>(); Seminar_Filter nameFilter = new Seminar_Filter(); nameFilter.Field = Seminar_Fields.Name; nameFilter.Criteria =""; // no actual filter specified yet filter.Add(nameFilter); Msg("Seminar List after inserting the new Seminar"); PrintSeminarList(ws, filter); Console.ReadLine();

// Modify Seminar Msg("Enter a new name for the Seminar and press [ENTER]"); MySeminar.Name = Console.ReadLine(); ws.Update(ref MySeminar); PrintSeminar(MySeminar); Msg("The Web service modified the name of the new Seminar. Press [ENTER] to continue"); Console.ReadLine(); Msg("List after modifying the Seminar"); PrintSeminarList(ws, filter); Console.ReadLine();

// Delete Seminar ws.Delete(MySeminar.Key); Msg("List after deleting the Seminar"); PrintSeminarList(ws, filter); Console.ReadLine(); Msg("Press [ENTER] to exit program."); Console.ReadLine();

9. Note that the code contains calls to a number of functions which will need to be added following the Main function:

static void PrintSeminarList(Seminar_Service service, List<Seminar_Filter> filter) { Msg("Printing Seminar List");

//Conduct the actual search Seminar[] list = service.ReadMultiple(filter.ToArray(), null, 100); foreach (Seminar s in list) { PrintSeminar(s); } Msg("End of List, press [ENTER] to continue"); } static void PrintSeminar(Seminar s) {

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 20: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-20

Console.WriteLine("No: {0} Name: {1}", s.No, s.Name); } static void Msg(string msg) { Console.WriteLine(msg); }

10. Click File, Save All to save all the changes in the project.

The C# code to consume the Seminar Web service is now complete. The next steps show how to test the console application. In Visual Studio:

11. Press F5 to run the application in debug mode. The console window opens, asking for the name of the new Seminar. Enter the name and press ENTER.

FIGURE 10.10 INSERT A NEW SEMINAR NAME

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 21: Na2009 enus devii_10

Chapter 10: Web Services

10-21

12. The application inserts a new Seminar record with the name that is just entered. Note that the next available number from the default Seminar No. Series is used as the primary key value of the new Seminar.

FIGURE 10.11 INSERT A NEW SEMINAR

13. Press ENTER to continue. The application prints a list of Seminars. Note that the new Seminar is listed.

FIGURE 10.12 SEMINAR LIST

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 22: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-22

14. Press ENTER to continue. Enter a new name for the Seminar and press ENTER again. The application modifies the new Seminar name to the name that is just entered.

FIGURE 10.13 MODIFY THE NEW SEMINAR NAME

15. Press ENTER to continue. The application prints the Seminar list after modification. Note that the Seminar's name has changed.

FIGURE 10.14 SEMINAR LIST AFTER MODIFICATION

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 23: Na2009 enus devii_10

Chapter 10: Web Services

10-23

16. Press ENTER to continue. The application deletes the new Seminar and prints a new list. Note that the new Seminar is no longer listed.

FIGURE 10.15 SEMINAR LIST AFTER DELETION

17. Press ENTER to close the console window.

NOTE: When the console application is run again, it creates another new Seminar, verifying that the Web service uses standard Microsoft Dynamics NAV logic that creates the next number from the default numbering series.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 24: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-24

Lab 10.4 - Create an Infopath Form to Consume a New Page Web Service for Seminar Participants

As with most tasks, there is more than one way to accomplish the task of consuming a page’s exposed Web services. A console application was using in Lab 10.3. Now an Infopath form will be used to consume a different page based Web service.

Scenario

This lab will create a new Web service that is based on page 123456711 – Seminar Registration Subpage, which shows records from the Seminar Registration Line table. This Web service will accept a filter for the Seminar Registration Number field, and returns a list of participants.

The goal is to create a form in Infopath to consume the new Web service. This Microsoft® Office program can be used to test a Web service.

Step by Step

In Microsoft Dynamics NAV:

1. Open page 123456711 - Seminar Registration Subpage. 2. Add a line to the repeater group of the "Seminar Registration No."

field. This field will enable the Web service to be able to filter on this field. Ensure that the field cannot be modified on the page by a RoleTailored client user, by setting Visible, Enabled and Editable properties to FALSE.

3. Enter a comment in the Documentation trigger to document the change in the object.

4. Save and close the object.

This page is now ready to be published as a Web service.

5. Open Form 810 - Web Services. 6. Enter a new line for Page 123456711, and enter SeminarParticipant

as its Service Name. 7. Check the Published field.

The Web service is now published, and ready to be consumed. To ensure that the Web service is available, open a Web browser and type in the following URL:

http://localhost:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Page/SeminarParticipant

The Web service's WSDL is now displayed in the browser, indicating this Web service is available to be consumed.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 25: Na2009 enus devii_10

Chapter 10: Web Services

10-25

8. Open Microsoft Office Infopath 2007 from the Microsoft Office menu. When Infopath opens, it displays the Getting Started wizard. If this does not display, open the wizard by selecting Fill Out a Form from the File menu.

FIGURE 10.16 GETTING STARTED IN INFOPATH

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 26: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-26

9. There is no existing template that meets the requirement. Click Design a Form Template on the left-hand side of the Getting Started wizard. This opens a dialog called Design a Form Template.

10. Click the Web Service icon and then click OK. This will start the Data Connection Wizard.

FIGURE 10.17 SELECT WEB SERVICE

11. Select Receive data, and then click Next. 12. Enter the URL for the SeminarParticipant Web service and click

Next.

http://localhost:7047/DynamicsNAV/WS/CRONUS_International_Ltd/Page/SeminarParticipant

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 27: Na2009 enus devii_10

Chapter 10: Web Services

10-27

Infopath will now connect to the Web service and retrieve the available methods for selection, as shown in the following image.

FIGURE 10.18 SELECT WEB SERVICE METHOD IN INFOPATH

13. Select ReadMultiple, and then click Next. 14. Accept Main Query as the name for the data connection, and then

click Finish.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 28: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-28

Infopath now has sufficient information to create a form based on the Web service. By default, Infopath will display a heading area at the top of the page, a box for query fields, a button called Run Query, and a box for the results with the data connection on the right-hand side. The following image shows the default empty form in Infopath.

FIGURE 10.19 DEFAULT EMPTY FORM IN INFOPATH

15. Click the title area and enter Seminar Participant List. 16. Expand the queryFields node, so that everything in this node is

visible. Notice that some of the selections have a red asterisk, which means these elements are mandatory elements, and they will need to be entered into the query section to the left.

17. Drag the following fields from queryFields node into the box labeled Drag query fields here: o Field - When this field is dropped into the query area, a drop-

down list will appear. Select Drop-Down List Box (Repeating). The user can enter filter criteria for any field that is available in the Web service.

o Criteria - This will provide an area for the user to enter filter criteria for the selected field.

o setSize - This is a mandatory element and sets the maximum number of records that is returned by the Web service.

Fields from the dataFields node can be dragged into the results box. By default this will put fields in a single display structure. The requirement is to display a list of participants, so now create a list box to display the records.

18. Click the box labeled Drag data fields here.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 29: Na2009 enus devii_10

Chapter 10: Web Services

10-29

19. From the Insert menu, select Repeating Table. This will start a new wizard that will help bind data to the repeating table.

20. Expand all in the dataFields node. Notice that the actual fields from the Seminar Registration Line table are available as elements of a node called SeminarPartcipant, which is the name entered for the new Web service.

21. Select SeminarParticipant, and then click Next, as shown in the following image.

FIGURE 10.20 SELECT SEMINARPARTICIPANT

22. In the next form, select the required fields and then click Finish. Be sure the following fields are selected: o Bill_to_Customer_No o Participant_Contact_No o Paticipant_Name o Register_Date

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 30: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-30

o Confirmation_Date o Registered

FIGURE 10.21 SELECT FIELDS

After clicking Finish, Infopath creates the repeating table with all the selected fields, as shown in the following image. The form is now ready to run and consume the Web service.

FIGURE 10.22 FINISHED INFOPATH FORM DESIGN

23. Preview the form by using one of the following methods: o Click the Preview button in the toolbar o Select Preview and then Form from the File menu o Press Ctrl+Shift+B

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 31: Na2009 enus devii_10

Chapter 10: Web Services

10-31

When the form starts in preview mode, the system may display a warning message that the network connection is not available. Select Try to Connect and the form will display.

FIGURE 10.23 NETWORK CONNECTION WARNING

24. Select the Seminar_Registration_No field from the drop-down list (this was the field added to the Seminar Registration Subpage earlier in this Lab).

25. Enter a valid Seminar Registration No. into the Criteria text box. When an invalid value is entered into the Criteria textbox, Infopath will return an error message saying Infopath cannot run the specified query.

26. Enter a numeric value into the Set Size textbox. This is a mandatory field (meaning that Infopath will display an error message if no value is entered), and signifies the maximum number of records that are returned.

27. Click the Run Query button.

Infopath will immediately display a security notice, as shown in the following image.

FIGURE 10.24 SECURITY NOTICE

28. Click Yes.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 32: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-32

Infopath will now connect to the Web service and retrieve any participants that are registered for the Seminar Registration No. that were entered into the Criteria box. The following image shows the form with possible values.

FIGURE 10.25 INFOPATH FORM IN RUNTIME

NOTE: This lab shows how to create a very simple form in Infopath to consume a Web service. Infopath forms can be used in other applications, such as SharePoint. A set of Infopath forms can be created to consume a variety of Web services, and those forms can be made available on the SharePoint Intranet site.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 33: Na2009 enus devii_10

Chapter 10: Web Services

10-33

Summary This chapter discussed the following:

• Web service capabilities in Microsoft Dynamics NAV 2009 as a new integration option.

• Some benefits of Web services. • A description of how Web services are supported in Microsoft

Dynamics NAV 2009. • How to create a simple console application in Visual Studio to

consume a Web service. • How to create a simple form in Microsoft Office Infopath to

consume a Web service.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 34: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-34

Test Your Knowledge 1. What are Web services?

2. What is the common language called that describes Web services?

3. Which two object types in Microsoft Dynamics NAV can be published as Web services?

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 35: Na2009 enus devii_10

Chapter 10: Web Services

10-35

4. How are Microsoft Dynamics NAV objects published as a Web service?

5. Which general database operations are provided by page Web services?

6. Name two technologies that can be used to consume Web services?

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 36: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-36

Quick Interaction: Lessons Learned Take a moment and write down three key points you have learned from this chapter

1.

2.

3.

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 37: Na2009 enus devii_10

Chapter 10: Web Services

10-37

Solutions Test Your Knowledge

1. What are Web services?

MODEL ANSWER:

Web services are a standardized way for independent software systems to communicate with each other over standard Internet protocols.

2. What is the common language called that describes Web services?

MODEL ANSWER:

Web Services Description Language, or WSDL

3. Which two object types in Microsoft Dynamics NAV can be published as Web services?

MODEL ANSWER:

Pages and Codeunits

4. How are Microsoft Dynamics NAV objects published as a Web service?

MODEL ANSWER:

By entering a reference to them in the Web Services form, and putting a checkmark into the Published field.

5. Which general database operations are provided by page Web services?

MODEL ANSWER:

Create, Read, Update and Delete

6. Name two technologies that can be used to consume Web services?

MODEL ANSWER:

Microsoft Visual Studio and Microsoft Office Infopath

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement

Page 38: Na2009 enus devii_10

C/SIDE Solution Development in Microsoft Dynamics® NAV 2009

10-38

Microsoft Official Training Materials for Microsoft Dynamics ® Your use of this content is subject to your current services agreement