SP.FieldCollection object

Represents a collection of SP.Field objects.

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

var object = new SP.FieldCollection()

Members

The FieldCollection object has the following members.

Constructor

The FieldCollection object has the following constructor.

Constructor

Description

FieldCollection

Initializes a new instance of the SP.FieldCollection object.

Methods

The FieldCollection object has the following methods.

Method

Description

add

Adds a field to the field collection.

addDependentLookup

Adds a secondary lookup field that depends on a primary lookup field for its relationship to the list where it gets its information.

addFieldAsXml

Creates a field based on the specified schema, Boolean value, and field options.

getById

Gets the field with the specified ID.

getByInternalNameOrTitle

Returns the first Field object with the specified internal name or title from the collection.

getByTitle

Returns the first field object in the collection based on the title of the specified field.

initPropertiesFromJson

itemAt

Gets the field at the specified index in the collection.

Properties

The FieldCollection object has the following properties.

Property

Description

childItemType

item

Gets the specified field from the collection.

schemaXml

Specifies the XML schema of the collection of fields.

Example

The following example creates an input button on an application page that uses the FieldCollection collection to display the available fields in the Announcements list.

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

    var fieldCollection;

    function runCode() {
        var clientContext = new SP.ClientContext();
        var targetList = clientContext.get_web().get_lists().getByTitle('Announcements');
        fieldCollection = targetList.get_fields();
        clientContext.load(fieldCollection, 'Include(Title)');
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }

    function onQuerySucceeded() {
        var message = "The following fields are available in the Announcements list:\n\n";
        var fields = '';
        var listEnumerator = fieldCollection.getEnumerator();
        while (listEnumerator.moveNext()) {
            fields += listEnumerator.get_current().get_title() + "; ";
        }
        alert(message + fields);
    }

    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 FieldCollection resource for more information.

Endpoint URI structure

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

HTTP requests

This resource supports the following HTTP commands:

GET syntax

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

POST syntax

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