Articles in this section

How to implement user based restrict editing and approval workflow in Blazor DOCX Editor

You can restrict document editing per user and implement approval workflows using the Syncfusion® Blazor DOCX Editor. This allows defining role-based editing access, tracking changes, reviewing comments, and controlling revision acceptance or rejection.

Demonstrates user-based document restrictions and approval workflows

The loaded Adventure Works Cycles document already holds the user-based editable ranges for Nancy’s price values, and each user’s restrictions are applied on the fly when a user is selected from the dropdown:

  • Nancy ([email protected]) – Price Editor: ReadOnly protection with editable ranges embedded in the document, so she can edit only the price values. The editable ranges are highlighted only for Nancy.
  • Andrew ([email protected]) – Reviewer: RevisionsOnly protection, so all his edits are tracked as revisions. He cannot accept or reject revisions.
  • Steven ([email protected]) – Commenter: CommentsOnly protection, so he can insert comments only and cannot edit the document content.
  • Michael ([email protected]) – Editor: no protection, full editing access on the entire document. He reviews and approves the tracked changes (approval workflow).
  • Laura ([email protected]) – Viewer: ReadOnly protection and read-only mode; she can only read the document.

Prerequisites

Refer to the system requirements documentation.

Steps to implement user-based restrict editing and approval workflow in DOCX Editor

Step 1: Create the application from the Getting Started base

Start with the Blazor getting started sample, which renders the SfDocumentEditorContainer with a toolbar in a Blazor WebAssembly or Blazor Server application.

Step 2: Define the users and their permissions

Define a list of users with the email bound to the Document Editor’s CurrentUser, the role, and the protection type to be enforced for each user.

public class UserInfo
{
    public string Name { get; set; } = string.Empty;
    public string Email { get; set; } = string.Empty;
    public string Role { get; set; } = string.Empty;
    public ProtectionType ProtectionType { get; set; }
    public string Description { get; set; } = string.Empty;
}

private readonly List<UserInfo> Users = new()
{
    new UserInfo { Name = "Nancy", Email = "[email protected]", Role = "Price Editor", ProtectionType = ProtectionType.ReadOnly, Description = "Can edit only the price values (editable ranges marked in the document)" },
    new UserInfo { Name = "Andrew", Email = "[email protected]", Role = "Reviewer", ProtectionType = ProtectionType.RevisionsOnly, Description = "All edits are tracked as revisions; cannot accept or reject revisions" },
    new UserInfo { Name = "Steven", Email = "[email protected]", Role = "Commenter", ProtectionType = ProtectionType.CommentsOnly, Description = "Can insert comments only; cannot edit document content" },
    new UserInfo { Name = "Michael", Email = "[email protected]", Role = "Editor", ProtectionType = ProtectionType.NoProtection, Description = "Full editing access on the entire document" },
    new UserInfo { Name = "Laura", Email = "[email protected]", Role = "Viewer", ProtectionType = ProtectionType.ReadOnly, Description = "Read-only access to the document" }
};

The supported protection types are NoProtection, ReadOnly, FormFieldsOnly, CommentsOnly, and RevisionsOnly. Refer to the restrict editing documentation for more details.

Step 3: Load the document with user-based editable ranges

Open the SFDT document that holds the user-based editable ranges (Nancy’s price values) and the document protection settings. The editable ranges are prepared in the document itself, so they are applied automatically when the document opens. In the sample, the SFDT data is embedded as a C# string in Pages/DocumentData.cs and exposed as Data.ProtectedData. Bind DocumentEditorSettings to a model that enables editable range highlighting.

@using Syncfusion.Blazor.DocumentEditor

<SfDocumentEditorContainer @ref="container"
                          ID="container"
                          EnableToolbar="true"
                          Height="100%"
                          DocumentEditorSettings="@settings"
                          ServiceUrl="https://document.syncfusion.com/web-services/docx-editor/api/documenteditor/">
    <DocumentEditorContainerEvents Created="OnContainerCreated"
                                   BeforeAcceptRejectChanges="OnBeforeAcceptRejectChanges">
    </DocumentEditorContainerEvents>
</SfDocumentEditorContainer>

@code {
    private SfDocumentEditorContainer? container;
    private DocumentEditorSettingsModel settings = new DocumentEditorSettingsModel() { HighlightEditableRanges = true };

    private async Task OnContainerCreated(object args)
    {
        if (container?.DocumentEditor != null)
        {
            await container.DocumentEditor.OpenAsync(Data.ProtectedData);

            var defaultUser = Users.FirstOrDefault(u => u.Email == selectedEmail) ?? Users[0];
            await ApplyUserPermissionsAsync(defaultUser);
        }
    }
}

Step 4: Enforce the protection based on the selected user

When a user is selected from the dropdown, stop the previous protection, bind the user’s email to CurrentUser, and enforce the user’s protection type using the public Blazor async APIs. For the read-only viewer, set the editor to read-only mode. The editable range highlighting is enabled only for Nancy (Price Editor) so the editable ranges are not highlighted for other users.

private async Task OnUserChanged(ChangeEventArgs e)
{
    var email = e.Value?.ToString() ?? string.Empty;
    var user = Users.FirstOrDefault(u => u.Email == email);
    if (user != null)
    {
        selectedEmail = user.Email;
        bannerText = $"Signed in as {user.Name} ({user.Email}) - {user.Role}";
        await ApplyUserPermissionsAsync(user);
    }
}

private async Task ApplyUserPermissionsAsync(UserInfo user)
{
    if (container?.DocumentEditor == null)
    {
        return;
    }

    // Unprotect existing document protection if applied
    await container.DocumentEditor.Editor.StopProtectionAsync(string.Empty);

    container.CurrentUser = user.Email;
    container.DocumentEditor.CurrentUser = user.Email;

    StateHasChanged();
    await Task.Delay(100);

    settings.HighlightEditableRanges = user.Email == "[email protected]";

    if (user.ProtectionType != ProtectionType.NoProtection)
    {
        await container.DocumentEditor.Editor.EnforceProtectionAsync(string.Empty, user.ProtectionType);
    }

    if (user.Email == "[email protected]")
    {
        container.DocumentEditor.IsReadOnly = true;
        container.RestrictEditing = true;
    }
    else
    {
        container.DocumentEditor.IsReadOnly = false;
        container.RestrictEditing = false;
    }
}

Switching the user instantly re-applies the restrictions, since the Document Editor checks the protection along with the editable ranges of CurrentUser.

Step 5: Implement the approval workflow

Use the BeforeAcceptRejectChanges event on the DocumentEditorContainerEvents to block the accept/reject actions for the reviewer user. This way, Andrew’s edits remain as tracked changes that only a user with approval rights (Michael, the full editor) can accept or reject.

private void OnBeforeAcceptRejectChanges(RevisionActionEventArgs args)
{
    if (selectedEmail == "[email protected]")
    {
        args.Cancel = true;
    }
}

Refer to the track changes documentation for reviewing, accepting, and rejecting revisions.

Run the application

Run the following command from the document-protection sample directory:

dotnet run

This builds and launches the Blazor app in the browser.

Verify the demo

Select a user in the dropdown to apply that user’s restrictions:

  • Nancy – try editing a price value (allowed) and any other text (blocked). The editable ranges are highlighted.
  • Andrew – type anywhere; the edits appear as tracked changes, and the accept/reject actions are cancelled.
  • Steven – insert comments from the Review tab; editing the content is blocked.
  • Michael – edit and format anywhere and accept or reject Andrew’s revisions to approve the changes.
  • Laura – the document is read-only; editing the content is blocked.

User-based-restrict-editing-in-Blazor-DOCX-Editor.gif

You can download a complete working sample demonstrating user-based restrict editing and approval workflows in Blazor DOCX Editor from GitHub.

Conclusion

We hope this guide helped you understand how to implement user based restrict editing and approval workflow in Syncfusion® Blazor DOCX Editor. By combining protection types with user-based editable ranges and the review events, you can build role-based document review and approval workflows.

You can refer to our Blazor DOCX Editor feature tour page to learn about its other groundbreaking features and Getting Started documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor DOCX Editor example to understand how to create and manipulate data.

If you are new to Syncfusion®, you can try our 30-day free trial to check out DOCX Editor and its features.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Support Portal, or Feedback Portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied