Click to Rate and Give Feedback
Community Content
In this section
Statistics Annotations (0)
Collapse All/Expand All Collapse All
This page is specific to
The 2007 product release

Other versions are also available for the following:
SPContentTypeCollection.Add Method (Microsoft.SharePoint)
Adds the specified content type to the collection.

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
Visual Basic (Declaration)
Public Function Add ( _
    contentType As SPContentType _
) As SPContentType
Visual Basic (Usage)
Dim instance As SPContentTypeCollection
Dim contentType As SPContentType
Dim returnValue As SPContentType

returnValue = instance.Add(contentType)
C#
public SPContentType Add (
    SPContentType contentType
)

Parameters

contentType

An SPContentType object to add to the collection.

Exception typeCondition

SPException

The name of the content type is either an empty string or is already in use by another content type in the collection.

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.

Visual Basic
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
C#
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("http://localhost"))
         {
            using (SPWeb web = site.OpenWeb())
            {
               // Get a content type.
               SPContentType ct = web.AvailableContentTypes[SPBuiltInContentTypeId.Document];

               if (ct != null) // We have a content type
               {
                  try // Get a list.
                  {
                     SPList list = 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 (!list.IsContentTypeAllowed(ct))
                        Console.WriteLine("The {0} content type is not allowed on the {1} list",
                                           ct.Name, list.Title);
                     else if (list.ContentTypes[ct.Name] != null)
                        Console.WriteLine("The content type name {0} is already in use on the {1} list",
                                           ct.Name, list.Title);
                     else
                        list.ContentTypes.Add(ct);
                  }
                  catch (ArgumentException ex) // No list is found.
                  {
                     Console.WriteLine("The list does not exist.");
                  }
               }
               else // No content type is found.
               {
                  Console.WriteLine("The content type is not available in this site.");
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker