Get all list views in SharePoint using JSOM

The example in this topic show how to use JSOM to Get all list views in SharePoint

  • var oListViews, clientContext;
  • function GetAllListViews() {
  •     // You can optionally specify the Site URL here to get the context
  •     // If you don't specify the URL, the method will get the context of the current site
  •     // var clientContext = new SP.ClientContext("http://MyServer/sites/SiteCollection");
  •     clientContext = new SP.ClientContext();
  •  
  •     // Get List Object
  •     var olistCollection = clientContext.get_web().get_lists();
  •     var oList = olistCollection.getByTitle("List Name");
  •  
  •     // Get All the views of the list
  •     oListViews = oList.get_views();
  •  
  •     // Load list view collection object
  •     clientContext.load(oListViews);
  •  
  •     // Execute the query to the server.
  •     clientContext.executeQueryAsync(onsuccess, onfailed);
  • }
  •  
  • function onsuccess() {
  •     // Iterate through Enumerator
  •     var oEnumerator = oListViews.getEnumerator();
  •  
  •     while (oEnumerator.moveNext()) {
  •         console.log("Title of the view: " + oEnumerator.get_current().get_title() + '\n');
  •     }
  • }
  •  
  • function onfailed(sender, args) {
  •     console.log('Failed' + args.get_message() + '\n' + args.get_stackTrace());
  • }

Thank you for reading this article. This code was tested in SharePoint 2013


© 2019 Code SharePoint