共用方式為


SPContentTypeCollection.Add method

將內容類型加入至集合。

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

Syntax

'宣告
Public Function Add ( _
    contentType As SPContentType _
) As SPContentType
'用途
Dim instance As SPContentTypeCollection
Dim contentType As SPContentType
Dim returnValue As SPContentType

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

參數

傳回值

Type: Microsoft.SharePoint.SPContentType
內容型別的完整初始化執行個體。

Exceptions

Exception Condition
SPException

contentType是空值。

-或-

內容類型已在集合中。

-或-

這個集合是只準備。

備註

內容型別未完全初始化之前加入至內容的型別集合。在其他方面,將它指派為相關集合的內容類型識別碼。例如,當您新增站台的內容類型與清單相關聯的內容型別集合,結果清單的內容型別會有不同於原始網站的內容型別的內容型別 ID。如需詳細資訊,請參閱Site and List Content Types

Examples

下列範例會顯示主控台應用程式,從AvailableContentTypes屬性所傳回的集合中取得內容型別。接著,它會加入該內容的型別由SPList物件的 [ ContentTypes ] 屬性所傳回的集合。

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("https://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
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("https://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();
      }
   }
}

請參閱

參照

SPContentTypeCollection class

SPContentTypeCollection members

Microsoft.SharePoint namespace

SPContentType(SPContentTypeId, SPContentTypeCollection, String)

其他資源

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy