Showing applied filters on SharePoint View using REST API

Introduction

Using views in SharePoint platform is very easy and a highly used featured within the organizations to keep data well-presented and organized.

How to create view in SharePoint

SharePoint views play very important role in organizing your data. With the filtering feature in views, you will reduce the number of data showing to your end users which result in better understanding of data in your SharePoint site. You can also control the columns (show / hide) to display data.  Due to reduction the number of columns, the rendering of the SharePoint page will become faster too. Views display prioritized content that you have organized which reduces unnecessary time filtering the output for the end user in SharePoint. You can also create private views to access columns which are useful for your specific type of work.

Thinking man

 

Problem Statement

Having all such useful features, SharePoint views have a drawback or we can call it a missing functionality for end users. By default, SharePoint does not display the filter information applied on the view. There is no OOB way to display the filter applied on the view to end user. Most of the time, it is a good idea to show to end users which data are filtered. Sometimes view name might be misleading or might not give correct idea that what the view is all about. Also, most of the users don’t have Contribute or Full Control access to the list settings to understand what kind of filter is applied to the view.

Solution

So, I have implemented a custom REST API based code to reverse engineer the filter mechanism applied on the view and display the detailed information about the current SharePoint view. The information obtained was a bit mechanical, so, I have further restructured it to show the information in plain English. This solution can be applied to SharePoint 2013, 2016 and SharePoint online.

Implementation

Let’s see the step by step process, how did I achieve the results on this blog post.

Outputs

Default view (No filter is applied)

sharepoint-image-1

 

Custom views (filter is applied as per my logic)

sharepoint-image-2

 

sharepoint-image-3

 

Code implementation can be explained as below:

  • Add CSS code and JavaScript references:

  • Initialize script to load SP.js then call the main function to create applied filter text using ExecuteOrDelayUntilScriptLoaded(createSchema, “SP.js”) function.
  • First call to function is createSchema(), this function retrieves view name for which the page is loaded. Which calls createFilter(viewname) function, where viewname is the name of the view.
  • createFilter(viewname) function is responsible for creating the filtered view description. It can be understood with the steps below:
  • Get the site url, list name and the REST url to get the RAW CAML query behind the current view:

  • Below AJAX call retrieves data from specified URL and creates temporary array tmpstk[ ] to get raw CAML data. The obtained data is mechanical with just AND, OR, EQ, GEQ kind of data.

Sample output looks like below:

[“OR”, “AND”, “LEQ”, “FIELDREF”, “Rank”, “VALUE”, “3”, “GT”, “FIELDREF”, “ID”, “VALUE”, “0”, “NEQ”, “FIELDREF”, “Title”, “VALUE”, “Google”]
  • This is all a developer needs to understand the filter. Now I will convert it to plain English to understandable for end users.
  • So, we need to parse the above result to get exact output in plain English. So, we have created 2 arrays to get mechanical data,

narray[] – to get the individual filter like RANK LEQ 3

literalArray[] – to get AND & OR clauses

 

Below logic fills both the arrays with appropriate data with the help of stack push method:

  • Next step is, to create string which stores the CAML converted data into pain text or simple English. Below code does that:

  • Last step is to embed that into the page. We just replace the OOB placeholder with the help of below code.

 

About

View all posts by

Leave a Reply