Share via


ContentTypeCollection.Add Method

Adds a new content type to the collection.

Namespace:  Microsoft.SharePoint.Client
Assemblies:   Microsoft.SharePoint.Client.Silverlight (in Microsoft.SharePoint.Client.Silverlight.dll);  Microsoft.SharePoint.Client (in Microsoft.SharePoint.Client.dll)

Syntax

'Declaration
<RemoteAttribute> _
Public Function Add ( _
    parameters As ContentTypeCreationInformation _
) As ContentType
'Usage
Dim instance As ContentTypeCollection
Dim parameters As ContentTypeCreationInformation
Dim returnValue As ContentType

returnValue = instance.Add(parameters)
[RemoteAttribute]
public ContentType Add(
    ContentTypeCreationInformation parameters
)

Parameters

  • parameters
    Type: Microsoft.SharePoint.Client.ContentTypeCreationInformation

    A ContentTypeCreationInformation 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 a null reference (Nothing in Visual Basic). 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.

Return Value

Type: Microsoft.SharePoint.Client.ContentType
A new ContentType object to add to the collection.

Exceptions

Exception Condition
[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.

Examples

This code example initializes a custom content type, adds it to the collection of content types, and displays the names of the specified website’s content types.

using System;
using Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointFoundation.Samples
{
    class ContentTypeCollectionAddExample
    {
        static void Main()
        {

            string siteUrl = "http://MyServer/sites/MySiteCollection";

            ClientContext clientContext = new ClientContext(siteUrl);
            Web site = clientContext.Web;
            ContentTypeCollection collContentType = site.ContentTypes;

            // Initialize a new content type.
            ContentTypeCreationInformation contentInfo = new ContentTypeCreationInformation();
            contentInfo.Name = "myContentType";
            contentInfo.Description = "My custom content type";
            ContentType contentType = collContentType.Add(contentInfo);

            clientContext.Load(collContentType);
            clientContext.ExecuteQuery();

            foreach (ContentType myType in collContentType)
                Console.WriteLine("Content Type Name: {0}", myType.Name);
        }

    }
}

See Also

Reference

ContentTypeCollection Class

ContentTypeCollection Members

Microsoft.SharePoint.Client Namespace