SP.ContentTypeCollection object

Represents a collection of Web site or list content types.

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

var object = new SP.ContentTypeCollection()

Members

The ContentTypeCollection object has the following members.

Constructor

The ContentTypeCollection object has the following constructor.

Constructor

Description

ContentTypeCollection

Initializes a new instance of the SP.ContentTypeCollection object.

Methods

The ContentTypeCollection object has the following methods.

Method

Description

add

Adds a new content type to the collection.

addExistingContentType

Adds the specified content type to the collection.

getById

Returns the content type with the specified ID from the collection.

itemAt

Gets the element at the specified index of the collection.

Properties

The ContentTypeCollection object has the following properties.

Property

Description

childItemType

item

Gets the element at the specified index of the collection.

Example

The following example creates an input button on an application page that initializes a custom content type, adds it to the collection of content types, and displays the names of the current Web site's content types.

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

var contentTypeCollection;

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

        this.contentTypeCollection = webSite.get_contentTypes();

        // Initialize a new content type.
        var contentInfo = new SP.ContentTypeCreationInformation();
        contentInfo.set_name('myContentType2');
        contentInfo.set_description('My custom content type 2');
        this.contentTypeCollection.add(contentInfo);

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

function onQuerySucceeded() {
    var contentTypeinfo = '';
    var contentTypeEnumerator = this.contentTypeCollection.getEnumerator();
    while (contentTypeEnumerator.moveNext()) {
        var content = contentTypeEnumerator.get_current();
        contentTypeinfo += 'Content Type Name: ' + content.get_name() + '\n';
    }
    alert(contentTypeinfo);
}

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

Endpoint URI structure

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

HTTP requests

This resource supports the following HTTP commands:

GET syntax

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

POST syntax

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