SP.ViewCollection object

Represents a collection of SP.View object (sp.js).

Applies to: apps for SharePoint | SharePoint Foundation 2013 | SharePoint Server 2013

var object = new SP.ViewCollection()

Members

The ViewCollection object has the following members.

Constructor

The ViewCollection object has the following constructor.

Constructor

Description

ViewCollection

This member is reserved for internal use and is not intended to be used directly from your code.

Methods

The ViewCollection object has the following methods.

Method

Description

add

Adds a new list view to the collection.

getById

Gets the list view with the specified ID.

getByTitle

Gets the list view with the specified title.

itemAt

Gets the list view at the specified index in the collection.

Properties

The ViewCollection object has the following properties.

Property

Description

childItemType

item

Gets the list view at the specified index in the collection.

Example

The following example creates an Input button on an application page that adds a new view to the Tasks list of the current site, and displays the Tasks list's current views.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

    var viewCollection = null;
    function runCode() {

        var clientContext = new SP.ClientContext.get_current();
        if (clientContext != undefined && clientContext != null) {
            var web = clientContext.get_web();

            var listCollection = web.get_lists();
            var list = listCollection.getByTitle("Tasks");
            this.viewCollection = list.get_views();

            var viewInfo = new SP.ViewCreationInformation();
            viewInfo.set_title('MyView');
            this.viewCollection.add(viewInfo);

            clientContext.load(this.viewCollection);
            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }
    }

    function onQuerySucceeded() {
        var viewInfo = 'Tasks list current views: \n\n';
        var viewEnumerator = this.viewCollection.getEnumerator();
        while (viewEnumerator.moveNext()) {
            var view = viewEnumerator.get_current();
            viewInfo += view.get_title() + '\n';
        }
        alert(viewInfo);
    }

    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }

</script>

    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />

</asp:Content>

REST resource endpoint

See ViewCollection resource for more information.

Endpoint URI structure

http://<sitecollection>/<site>/_api/web/lists(listid)/Views

HTTP requests

This resource supports the following HTTP commands:

GET syntax

GET http://<sitecollection>/<site>/_api/web/lists(listid)/Views

POST syntax

POST http://<sitecollection>/<site>/_api/web/lists(listid)/Views