Getting Started with Blazor TreeView in Blazor Server App

28 Jul 20268 minutes to read

This section explains how to integrate the Blazor TreeView component into a Blazor Server App using Visual Studio, Visual Studio Code, and the .NET CLI.

Create a new Blazor Server App

Create a Blazor Server App using Visual Studio via Microsoft Templates or the Syncfusion® Blazor Extension. For detailed instructions, refer to the Blazor Server App Getting Started documentation.

Run the following command to create a new Blazor Server App.

dotnet new blazor -o BlazorApp --interactivity Server
cd BlazorApp

Alternatively, create a Blazor Server App using Visual Studio Code via Microsoft Templates, the Syncfusion® Blazor Extension, or the C# Dev Kit extension.

Run the following command to create a new Blazor Server App.

dotnet new blazor -o BlazorApp --interactivity Server
cd BlazorApp

Configure the appropriate Interactive render mode and Interactivity location while creating a Blazor Server App. For detailed information, refer to the interactive render mode documentation.

Install the required Blazor packages

Install Syncfusion.Blazor.Navigations and Syncfusion.Blazor.Themes NuGet packages. All Syncfusion Blazor packages are available on nuget.org. See the NuGet packages topic for details.

  1. Go to Tools → NuGet Package Manager → Manage NuGet Packages for Solution.
  2. Search the required NuGet packages (Syncfusion.Blazor.Navigations and Syncfusion.Blazor.Themes) and install them.

Alternatively, you can install the same packages using the Package Manager Console with the following commands.

Install-Package Syncfusion.Blazor.Navigations -Version 35.1.37
Install-Package Syncfusion.Blazor.Themes -Version 35.1.37

Open the terminal and run the following commands.

dotnet add package Syncfusion.Blazor.Navigations -v 35.1.37
dotnet add package Syncfusion.Blazor.Themes -v 35.1.37

Open the command prompt and run the following commands.

dotnet add package Syncfusion.Blazor.Navigations -v 35.1.37
dotnet add package Syncfusion.Blazor.Themes -v 35.1.37

Add import namespaces

After the packages are installed, open the ~/_Imports.razor file and import the Syncfusion.Blazor and Syncfusion.Blazor.Navigations namespaces.

@using Syncfusion.Blazor
@using Syncfusion.Blazor.Navigations

Register the Blazor service

Open the Program.cs file in Blazor Server App and register the Blazor service and include the required namespace reference using Syncfusion.Blazor; at the top.

builder.Services.AddSyncfusionBlazor();

Add stylesheet and script resources

The theme stylesheet and script can be accessed from NuGet through Static Web Assets. Include the stylesheet at the end of the <head> section in the App.razor file.

<link href="_content/Syncfusion.Blazor.Themes/fluent2.css" rel="stylesheet" />

Include the required script references at the end of the <body> section in the App.razor file to enable TreeView functionality.

<script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>

Add Blazor TreeView component

Open a Razor file located in the ~/Pages/*.razor (for example, Home.razor) and add the Blazor TreeView component inside the razor file.

If the interactivity location is set to Per page/component, define a render mode at the top of the razor file. (For example InteractiveServer). If the Interactivity is set to Global, the render mode is automatically configured in the App.razor file by default.

@rendermode InteractiveServer

@using Syncfusion.Blazor.Navigations

<SfTreeView TValue="MailItem">
    <TreeViewFieldsSettings TValue="MailItem" Id="Id" DataSource="@MyFolder" Text="FolderName" ParentID="ParentId" HasChildren="HasSubFolders" Expanded="Expanded"></TreeViewFieldsSettings>
</SfTreeView>

@code {
    public class MailItem
    {
        public string? Id { get; set; }
        public string? ParentId { get; set; }
        public string? FolderName { get; set; }
        public bool Expanded { get; set; }
        public bool HasSubFolders { get; set; }
    }
    List<MailItem> MyFolder = new List<MailItem>();
    protected override void OnInitialized()
    {
        base.OnInitialized();
        MyFolder.Add(new MailItem
        {
            Id = "1",
            FolderName = "Inbox",
            HasSubFolders = true,
            Expanded = true
        });
        MyFolder.Add(new MailItem
        {
            Id = "2",
            ParentId = "1",
            FolderName = "Categories",
            Expanded = true,
            HasSubFolders = true
        });
        MyFolder.Add(new MailItem
        {
            Id = "3",
            ParentId = "2",
            FolderName = "Primary"
        });
        MyFolder.Add(new MailItem
        {
            Id = "4",
            ParentId = "2",
            FolderName = "Social"
        });
        MyFolder.Add(new MailItem
        {
            Id = "5",
            ParentId = "2",
            FolderName = "Promotions"
        });
    }
}

Run the application

Press Ctrl+F5 (Windows) or ⌘+F5 (macOS) to launch the application. The Blazor TreeView component will render in your default web browser.

Open the terminal and run the following command.

dotnet run

Open the command prompt and run the following command.

dotnet run
Blazor TreeView Component

View sample in GitHub.