For example, I have created a list called “Test” with two custom columns, Column1 and Column2. By default when you try opening a list in SharePoint, the default view called “All Items” will be rendered. This view will show you three columns. They are Title, Column1 and Column2.
Creating SharePoint list view using JSOM:
In this example, we will see how we can view only our custom columns by filtering the SharePoint list values.
The following flow will show you the implementation.
- Load the SP.js files and get the context of site
- Get all the views for a specific list present on the site
- Create a custom view ViewCreationInformation method. Set view title and the fields to be shown on the view using the following code.
- Then build your query to retrieve the values for the view. This step can be an optional one.
- The view can be limited with custom row limit.
- The view can be still customized with view types. The applicable types are html, grid, etc. The following code shows you how to set the type.
Type Value none 0 html 1 grid 2048 calendar 524288 recurrence 8193 chart 131072 gantt 67108864
- Then add the view to the collection and execute the query.
- After execution of the above approach, go to List tab and you can see the new view, “Grid View,” created. Click on the new view to see the list with necessary filters set.
The above steps helped us in creating views for custom list.
Modifying or Updating SharePoint list views using JSOM:
The approach is similar to creating views. Here instead of creating new view object, the existing view is retrieved and the properties are updated. The below code shows how you can update existing views.
Note: There are a few limitations in modifying views, such as the view type and view fields can’t be changed.
The following piece of code will help you in modifying the existing list view we have created above.
Deleting SharePoint list views using JSOM:
You will learn how a list view can be deleted. This can be achieved by using deleteObject method. The below code will help you in deleting a view.