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.
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 :
.EnableSwagger(c => c.SingleApiVersion(“v1”, “Name of the web api”));
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.
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!!
Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
© All Rights Reserved. Inkey Solutions 2019