Aracılığıyla paylaş


SP.ContentTypeCollection.add(parameters) Method

Applies to: SharePoint Foundation 2010

Adds a new content type to the collection.

var value = SP.ContentTypeCollection.add(parameters);

Parameters

  • parameters
    A SP.ContentTypeCreationInformation Class object that represents information associated with the content type. It specifies properties that will be used to construct the new content type. It must not be null. The content type scope of its ParentContentType property must not be an ancestor of the parent of the content type collection. ParentContentType property specifies the parent content type for the content type that will be constructed.

Type: SP.ContentTypeCreationInformation

Return Value

Type: SP.ContentType

Applies To

SP.ContentTypeCollection Class

Exceptions

  • [ServerException]
    The collection is read only.
  • [SPException]
    A content type with a name specified by the Name property of parameters exists in the collection. Error code: 183.

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 website’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>

See Also

Reference

SP.ContentTypeCollection Methods

SP.ContentTypeCollection Properties

SP Namespace