Setting a Default Filter for Library Items
While working with the libraries in the Documents and Files/Images/Videos sections, when you try to filter some items, the system redirects you to a page where all items for the currently selected library are displayed. To set a default filter for library items, and a customized display (for example, displaying only the current user's items), you must change the default LibrariesMasterExtensions java script file with a new java script file in which the filter is applied.
In the sample below, you will set a default filter for the Documents & Files libraries and will display only the items of the current user. To implement this functionality, perform the following:
- Open your project in Visual Studio and create a new JavaScript file. Name the script JavaScript1.js.
- Copy the code provided below and paste it in the JavaScript file:
var myUserId;
function OnMasterViewLoadedCustom(sender, args) {
myUserId = sender._currentUserId;
var itemsGrid = sender.get_currentItemsList();
itemsGrid.applyFilter("Owner == (" + myUserId + ")");
}
- Open your project in the browser and go to Administration > Settings > Advanced.
- In the left panel, in the tree-view, select Content View > Controls > DocumentsBackend > Views > DocumentsBackendList > Scripts.
- Clicking on Script displays the default script on the right pane.
- You must now create a new script. Click the Create new button.
- In the Scripts page that appears, enter the following information:
- Script location: Enter the name of the JavaScript file~/MyFolder/JavaScript1.js (relative path to the script file) that you have just created in Visual Studio.
- Name of the load method: Enter the description of the load method name property as OnMasterViewLoadedCustom (from the script you just created.
- Save changes.
- Go to Administration > Settings > Advanced
- In the left panel, in the tree-view, select Content View > Controls > DocumentsBackend > Views > DocumentsBackendList > Sidebar > Sections > Filter > Items > AllDocuments.
- In the AllDocuments page that appears, navigate to the Button CSS class and delete the value in the field.
- Save changes.
- In the tree-view, navigate to the MyDocuments node 9under the AllDocuments node) and select it.
- In the MyDocuments page that appears, navigate to Button CSS class and add the value, sfSel in the field. Setting this value for Button CSS class makes the filter, My Items visibly selected.
- Save changes.
- Restart you application.
When you try to filter the library items the next time, MyDocuments will be the default filter applied.