SPContentType constructor (SPContentType, SPContentTypeCollection, String)

Initializes a new instance of the SPContentType class using the specified parent content type.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Sub New ( _
    parentContentType As SPContentType, _
    collection As SPContentTypeCollection, _
    name As String _
)
'Usage
Dim parentContentType As SPContentType
Dim collection As SPContentTypeCollection
Dim name As String

Dim instance As New SPContentType(parentContentType, _
    collection, name)
public SPContentType(
    SPContentType parentContentType,
    SPContentTypeCollection collection,
    string name
)

Parameters

  • name
    Type: System.String

    The name of the content type. This string is set as the value of the Name property in the new content type.

Exceptions

Exception Condition
SPException

The collection cannot be modified.

ArgumentException

The SPList on which the parentContentType resides is null .

-or-

The parentContentType has been removed from its parent SPContentTypeCollection.

Remarks

The collection object that you pass as the second argument to the constructor is used to obtain certain initial values for properties of the new content type. However, the new content type is not automatically added to the collection. You must do that yourself in a subsequent call to the Add(SPContentType) method.

This constructor sets the value of the new object’s Name property to the value passed in the name argument.

Examples

The following example creates a content type that is derived from the “Item” type and then adds the new type to the site collection.

Dim site As SPSite = New SPSite("https://localhost")
Dim web As SPWeb = site.OpenWeb()

' Get a reference to the site content type collection.
Dim contentTypes As SPContentTypeCollection = web.ContentTypes

' Create a Customer content type derived from the Item content type.
Dim contentType As SPContentType = New SPContentType(contentTypes("Item"), contentTypes, "Customer")

' Add the content type to the site collection.
contentType = contentTypes.Add(contentType)

' Clean up.
web.Dispose()
site.Dispose()
SPSite site = new SPSite("https://localhost");
SPWeb web = site.OpenWeb();

// Get a reference to the site content type collection.
SPContentTypeCollection contentTypes = web.ContentTypes;

// Create a Customer content type derived from the Item content type.
SPContentType contentType = new SPContentType(contentTypes["Item"], contentTypes, "Customer");

// Add the content type to the site collection.
contentType = contentTypes.Add(contentType);

// Clean up.
web.Dispose();
site.Dispose();

See also

Reference

SPContentType class

SPContentType members

SPContentType overload

Microsoft.SharePoint namespace

Other resources

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy