ContentTypeCollection.Add method

Adds a new content type to the collection.

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

Syntax

'Declaration
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)
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. The 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