Consuming OData services
The Open Data protocol (OData) is a common way to expose a database as a REST API. Radzen Blazor Studio provides first class support for OData services. It scaffolds for you:
- Service classes which make HTTP requests to the OData service and optionally authenticate.
- Model classes for all entities.
- Various pages that allow to to quickly make a full CRUD application from the OData service.
Connect to OData service
In the following tutorial we will show how to:
- Create an OData data source in Radzen Blazor Studio.
- Generate code for the entities.
- Display data.
The tutorial uses the public Northwind OData API available at https://services.odata.org/Experimental/Northwind/Northwind.svc
.
Configure the OData API connection
- Create a new Blazor application in Radzen Blazor Studio or open an existing one.
- Click the button.
- Select OData as the data source type in the Create or update data source dialog. This opens the OData connection editor.
- Enter the Northwind OData API connection details
- Set Name to
Northwind
. The name must be a valid C# identifier. It is used for namespaces and service class names. - Set URL to
https://services.odata.org/Experimental/Northwind/Northwind.svc
.
- Set Name to
- Click Next. Radzen Blazor Studio will infer the metadata and display all entity sets and singletons.
- Check the items from the OData service that you want to generate code for (models and CRUD methods). Click Next.
- Check Generate pages for CRUD operations (this is a premium feature). Pick the CRUD template.
- Select the entities you want CRUD pages generated for. In this case we will select the Category entity and uncheck Allow editing, Allow inserting and Allow deleting. Click Finish.
Radzen Blazor Studio will:
- Create a
NorthwindService
class which can fetch data from the Northwind OData service. - Create model classes for all checked entities.
- Create a
Categories.razor
page which will list the categories from the Northwind OData service.
Use the MSGraph API
Here is how create a OData Data Source for the MSGraph API.
- Create a new OData data source.
- Set Name to
MSGraph
. - Set URL to
https://graph.microsoft.com/v1.0/
. - Set Authorization type to
OAuth2
. - Set Client ID and Client Secret from your Azure AD app settings.
- Set Authorization URL to
https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/authorize
(replacetenant-id
with your AzureAD tenant id). - Set Access Token URL to
https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token
(replacetenant-id
with your AzureAD tenant id). - Set Scope to
openid
. - Click Next to infer the MSGraph API.
- Check the entities and or singletons you want to use.
IMPORTANT! The MSGraph API has a lot of surface and checking all entities and singletons would lead to scaffolding a few thousand files. Check only the entities that you plan to use.