Toolbox filtering: Implement the IToolboxFilter interface
The IToolboxFilter interface provides an extension point for filtering toolbox sections and toolbox items.
- In Visual Studio, open the context menu of your project and click Add » Class.
- Name the class file RoleToolboxFilter.cs and click Add.
- Make the RoleToolboxFilter implement the IToolboxFilter interface.
- Create a constructor that accepts an IToolboxFilter object.
The constructor forms a chain of responsibility, where each toolbox filter points to an instance of the next filter in line.
- Implement the IsSectionVisible() method from the IToolboxFilter interface.
The code checks whether a toolbox section complies with the specified conditions to be visible. It also calls the IsSectionVisible() method of the next filter in line. The method accepts the toolbox section and an
IToolboxFilterContext context as parameters.
Optionally, since Sitefinity CMS version 12.0 you can implement additional filtering logic using the
IToolboxFilterContext. It provides the following information:
MediaType - the media type indicates whether the toolbox is loaded in the context of the Pages, Templates, Forms, or Email campaigns modules screens. If you want your filtering to be applied only to some of these modules you can implement conditional logic based on this property.
ContainerId - the container Id property can help you distinguish in your code, whether you are working with the widgets toolbox or the layout controls toolbox. The possible values are
- Implement the IsToolVisible() method from the IToolboxFilter interface.
The code checks whether a widget complies with the specified conditions to be visible. It also calls the IsToolVisible() method of the next filter in line.
- Implement the role filtering logic, by passing a set of tags, representing the roles that can view a section or a tool, on method call.
Using the ClaimsManager class, the code retrieves the current user identity and compares the set of tags with the current user's roles. It returns a result that indicates if any of them overlap.
Use the following code sample: