Inkey Solution Logo
banner

Blogs

Swagger in Microsoft Dynamics 365 Web API

, March 12, 2018 12617 Views

The Swagger UI is one of the most popular tools for generating an interactive documentation from your OpenAPI document. It makes the process of viewing the results of the Web API much easier.

There are situations, where, we need to make use of the Web API, for connecting the external website/resource with the dynamics CRM instance, to process with the data, existing in crm. Proper documentation and having a good API explorer (Playground) is a crucial thing for your API success.
And, in this blog, I will discuss about, how swagger can be helpful in Microsoft Dynamics 365 for this purpose.

Requirement scenario:

Recently we had a requirement of connecting the external resource(mobile app), with the dynamics crm instance and create / update the record in crm. This was possible with the use of the .Net Web API, that was called from the external resource. Now, to make sure of what exactly is the outcome from the methods written inside the controllers of Web API and what all are the required inputs, swagger can be very helpful, as it offers a very simple UI, specifying, the inputs required and data models and even, we can execute the controller as required.

So we made use of swagger tool to validate the methods of the controller.

Installing swagger tool:

We can add the swagger to our Web API project, by installing an opensource project called Swashbuckle(https://www.nuget.org/packages/Swashbuckle), using the nuget with the below command.

PM> Install-Package Swashbuckle -version 5.6.0

Note:- Version wise the above command changes. Please refer https://www.nuget.org/packages/Swashbuckle, for more details.

The swaggerConfig.cs file gets added to the AppStart folder of the web application project. It initiates the Swashbuckle on the application start-up using WebActivatorEx. This file requires the below lines for enabling the swagger :

GlobalConfiguration.Configuration
.EnableSwagger(c => c.SingleApiVersion(“v1”, “Name of the web api”));
.EnableSwaggerUi();

The Swashbuckle makes a best attempt at generating Swagger compliant JSON schemas for the various types, exposed in your API.

Once the installation is done, run the project and append the url with “/swagger” (for example : http://localhost:PortNumber/swagger) and observe the output in the swagger UI. The path of calls to the webapi methods are added under the respective GET, POST, PUT, DELETE etc. We can select the response content-type as application/json, text/json, application/xml or text/xml. Also, provide the parameters required by the method as input.

Validate results:

In our case, we wanted to add the parameters like first name, last name, surname, mobile, address, email address, city and state, that was to be passed from the mobile app and was then to be updated in the dynamics crm.

Below image gives the clarification of how the swagger looks like, when we run the application project. Here, we can see that, there are two controllers in our project and it is also specifying the methods in each controller:-

To process your webapi using swagger, click on the ‘Expand Operations’ button, in the right top corner of the controller name as shown in the below image:-

When you click on the ‘Expand Operations’ button, you will see the data model of your controller. Click on it, then, you will see the parameters filled, same as in data model. Now, you can give the values as required to each of the parameters:-

After, setting the parameter values, click on the ‘Try it out’ button, to see the output from the webapi method as below:-

In this way, using Swagger in dynamic 365, can be very helpful for describing, consuming, and visualizing your RESTful APIs.

I hope this helps you!!

Happy CRMing.


 

Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/drag-and-drop-listbox

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

Leave a Reply

Your email address will not be published. Required fields are marked *

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Drop file here

Would you like to digitize your business and put it on the cloud?
Do you need clear, concise reports for your organization?