MS Graph (Blazor)
This tutorial will show you how to connect to Microsoft Graph data using Azure AD authentication.
1. Create new application in Azure portal.
2. Add http://localhost:5000/ms-graph-callback
in Authentication -> Redirect URIs. Add permissions to sign-in and read the user. Check also Microsoft Graph REST API reference for more info how to access and manage users.
3. Create new Blazor application in Radzen, add new OData data-source connected to https://graph.microsoft.com/v1.0/
endpoint with AzureAD authentication. Set https://login.microsoftonline.com/common/v2.0/
for Authorization Endpoint and enter ClientID, and Client Secret obtained from your application in Azure portal. Add user.read
to Scopes and check all entities.
4. Create new page, invoke MSGraph.getMe
data source method using Load event and assign ${result.displayName}
to name
property. Add new Label component to the page and set Text property to ${name}
.
5. Run the application, sign-in and check the result.
Let’s now list Microsoft OneDrive files and folders!
- Create new page named OneDrive, drag and drop a Row and two Column component from Radzen toolbox. Set 3 and 9 for the size of first and second column and add Tree component to the first column and DataGrid component to the second column.
- Define page property
rootItems
of typeList<DriveItem>
withnull
as value. - Invoke MSGraph data-source
getDrives()
method on page Load event and set page propertydriveId
toresult.Value.ToList()[0].id
. - In
getDrives()
Then invoke MSGraph data-sourcegetDrivesRoot()
method with${driveId}
as parameter. SetrootItems
tonew List<DriveItem>(){ result }
and define propertyitemId
with value${result.id}
. Execute${grid0.Reload()}
. - Bind the Tree component to
rootItems
page property and define one level usingmicrosoft.graph.driveItem
schema. Setname
for the level TextProperty property andchildren
for the level ChildrenProperty property. - Invoke MSGraph data-source
getDrivesItemsChildren()
method on Tree component Expand event with three parameters:- id =
${driveId}
. - driveItemId =
{((DriveItem)args.Value).id}
. - $filter =
file eq null
. Filter drive items to return only folders. Executeevent.Children.TextProperty = "name"
andevent.Children.Data = result.Value
in invoke Then.
- id =
- Set page property
itemId
to((DriveItem)event.Value).id
on Tree component Change event and execute${grid0.Reload()}
. - Bind the DataGrid component to
getDrivesItemsChildren()
using new data wizard, remove unwanted columns and paging parameters from LoadData eventgetDrivesItemsChildren()
invoke and add two parameters to:- id =
${driveId}
. - driveItemId =
${itemId}
. Turn off DataGrid paging, sorting and filtering and setVisible
property to${itemId != null}
.
- id =
.. and that’s all!