如何:向列表添加内容类型

上次修改时间: 2011年5月27日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

可以在 XML 中引用列表定义的内容类型,以便每当用户创建该类型的列表时,Microsoft SharePoint Foundation 2010 默认情况下都会在该列表中包含内容类型。还可编写使用 SharePoint Foundation 对象模型的代码,以将内容类型添加到现有列表中。

指定列表定义中的内容类型

必须确保将列表配置为支持内容类型,才能将相应内容类型添加到列表定义中。首先要检查的内容是在其中创建列表实例的模板。如果 ListTemplate 元素具有名为 DisallowContentTypes 的属性,并且该属性设置为 TRUE,则该列表不支持内容类型。如果该属性设置为 FALSE 或缺失,则该列表支持内容类型。

接下来要检查是否对列表实例启用了内容类型。定义列表实例的 Schema.xml 文件具有一个 List 元素。该元素必须包含名为 EnableContentTypes 的属性,并且该属性必须设置为 TRUE。将该属性设置为 TRUE 等效于在"列表设置"的"高级设置"部分的"是否允许管理内容类型?"下选择"是"。

要向列表定义添加内容类型,可以将 ContentTypes 元素添加到列表架构中。ContentTypes 元素包含 ContentTypeRef 元素的集合。当用户创建指定类型的新列表时,每个 ContentTypeRef 元素均会将 SharePoint Foundation 应本地复制到该列表的网站内容类型指定为列表内容类型。ContentTypeRef 元素包含一个设置为内容类型 ID 的属性 ID。

引用的网站内容类型必须在列表范围内,即必须在网站层次结构中的同一网站级别或更高网站级别声明此内容类型。有关内容类型范围的详细信息,请参阅内容类型作用域

备注

当 SharePoint Foundation 创建列表实例时,它只会包括在列表的基类型架构或列表架构中声明的那些列。如果引用列表架构中的网站内容类型,并且该内容类型引用列表的基类型架构中或列表架构中未包含的网站栏,则不包括这些列。必须在列表架构中声明这些列,以便 SharePoint Foundation 在列表中包含它们。

有关网站栏的详细信息,请参阅列介绍

向列表定义添加内容类型

  1. 在列表定义 XML 中,将 ContentTypeRef 元素添加到 ContentTypes 元素。

  2. 将 ContentTypeRef 元素的 ID 属性设置为要在列表上包含的内容类型的内容类型 ID。

以下示例演示包括 ContentTypeRef 元素的列表定义架构的部分标记。

<List xmlns:ows="Microsoft SharePoint" Title="The List" Direction="$Resources:Direction;" FolderCreation="FALSE" Url="Lists/TheList" BaseType="0" xmlns="https://schemas.microsoft.com/sharepoint/">
  <MetaData>
    <ContentTypes>
      <ContentTypeRef ID="0x01060062efcfca3f4d4036a0c54ed20108fa2e" />
    </ContentTypes>
    ...
  </MetaData>
</List>

有关列表定义架构中 ContentTypes 元素的详细信息,请参阅 ContentTypes 元素(列表)

向现有列表中添加内容类型

可使用 SharePoint Foundation 对象模型向现有列表中添加内容类型。

向列表中添加内容类型

  1. 使用 Lists 属性为列表所在的网站获取列表集合。

  2. 声明 SPList 类型的变量,并将其设置为等于表示该列表的网站列表集合中的对象。

  3. 启用列表的内容类型,方法是将列表的 ContentTypesEnabled 属性的值设置为 true。

  4. 使用 AvailableContentTypes 属性访问可用于列表所在网站的内容类型。此方法将返回一个 SPContentTypeCollection 对象。

  5. 声明一个 SPContentType 类型的变量,并将其设置为等于集合中的 SPContentType 对象,该对象表示要添加到列表中的网站内容类型。

  6. 调用 IsContentTypeAllowed(SPContentType) 方法,以验证该列表是否可接受已选择的内容类型。

  7. 使用 ContentTypes 属性以访问指定列表上的列表内容类型的集合。此方法将返回一个 SPContentTypeCollection 对象。

  8. 使用 Add 方法以将 SPContentType 对象添加到列表内容类型集合。

当使用对象模型向列表中添加网站内容类型时,SharePoint Foundation 会自动添加内容类型包含的已不在列表中的任何列。这与引用列表架构中的内容类型不同,在此情况下,必须将这些列显式添加到列表架构中以便 SharePoint Foundation 将它们包含在列表实例中。

示例

下面是一个将网站内容类型添加到网站的"共享文档"列表中的控制台应用程序示例。此示例中使用的内容类型与如何:向网站添加内容类型中的示例创建的内容类型相同。

控制台应用程序可用于在开发环境中进行试验。在生产环境中,此示例的代码更有可能作为 SPFeatureReceiver 对象的 FeatureActivated 方法的一部分。

using System;
using Microsoft.SharePoint;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("https://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {

                    // Get a content type.
                    SPContentType ct = site.AvailableContentTypes["Financial Document"];

                    // The content type was found.
                    if (ct != null) 
                    {

                        // Get a list.
                        try 
                        {
                            SPList list = site.Lists["Shared Documents"]; // Throws exception if does not exist.

                            // Make sure the list accepts 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();
        }
    }
}
Imports System
Imports Microsoft.SharePoint

Module Test
   Sub Main()
      Using siteCollection As SPSite = New SPSite("https://localhost")
         Using site As SPWeb = siteCollection.OpenWeb()

            ' Get a content type.
            Dim ct As SPContentType = site.AvailableContentTypes("Financial Document")

            If ct IsNot Nothing Then

                ' The content type was found.
                Try

                    ' Get a list.
                    Dim list As SPList = site.Lists("Shared Documents")

                    ' Make sure the list accepts 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 is found.
                    Console.WriteLine("The list does not exist.")
                End Try
            Else

                ' No content type is 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

请参阅

概念

网站和列表内容类型

内容类型作用域

内容类型定义