Export (0) Print
Expand All

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)

public SPContentType(
	SPContentType parentContentType,
	SPContentTypeCollection collection,
	string name
)

Parameters

parentContentType
Type: Microsoft.SharePoint.SPContentType

The content type from which this content type is derived.

collection
Type: Microsoft.SharePoint.SPContentTypeCollection

The collection to which this content type is added.

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.

ExceptionCondition
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.

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.

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

SPSite site = new SPSite("http://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();
Show:
© 2014 Microsoft