SPContentTypeCollection.Add method

Adds a content type to the collection.

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

'Declaration
Public Function Add ( _
	contentType As SPContentType _
) As SPContentType
'Usage
Dim instance As SPContentTypeCollection
Dim contentType As SPContentType
Dim returnValue As SPContentType

returnValue = instance.Add(contentType)

Parameters

contentType
Type: Microsoft.SharePoint.SPContentType

The content type to add to the collection.

Return value

Type: Microsoft.SharePoint.SPContentType
A fully initialized instance of the content type.

ExceptionCondition
SPException

contentType is null.

-or-

The content type is already in the collection.

-or-

The collection is ready-only.

A content type is not completely initialized until it is added to a content type collection. Among other things, it is assigned a content type ID that is relevant to the collection. For example, when you add a site content type to the content type collection associated with a list, the resulting list content type has a content type ID that is different from the original site content type. For more information, see Site and List Content Types.

The following example shows a console application that gets a content type from the collection that is returned by the AvailableContentTypes property. It then adds that content type to the collection that is returned by the ContentTypes property of an SPList object.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("http://localhost")
         Using web As SPWeb = site.OpenWeb()

            ' Get a content type.
            Dim ct As SPContentType = web.AvailableContentTypes(SPBuiltInContentTypeId.Document)

            If ct IsNot Nothing Then ' We have a content type

               Try ' Get a list.
                  Dim list As SPList = web.Lists("Test List") ' Throws exception if does not exist

                  ' Make sure you can add content types.
                  list.ContentTypesEnabled = True

                  ' Add the content type to the list.
                  If Not list.IsContentTypeAllowed(ct) Then
                     Console.WriteLine("The {0} content type is not allowed on the {1} list", _
                                       ct.Name, list.Title)
                  ElseIf list.ContentTypes(ct.Name) IsNot Nothing Then
                     Console.WriteLine("The content type name {0} is already in use on the {1} list", _
                                       ct.Name, list.Title)
                  Else
                     list.ContentTypes.Add(ct)
                  End If

               Catch ex As ArgumentException ' No list
                  Console.WriteLine("The list does not exist.")
               End Try

            Else ' No content type found.
               Console.WriteLine("The content type is not available in this site.")
            End If

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module
Show: