SPContentTypeCollection.Add 方法
向集合中添加内容类型。
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
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
)
参数
contentType
类型:Microsoft.SharePoint.SPContentType要添加到集合的内容类型。
返回值
类型:Microsoft.SharePoint.SPContentType
完全初始化该内容类型的实例。
异常
异常 | 条件 |
---|---|
SPException | contentType为空。 - 或 - 集合中已有的内容类型。 - 或 - 该集合是只准备。 |
备注
内容类型未完全初始化之前它将添加到内容类型集合。除此之外,它被分配与该集合的内容类型 ID。例如,当您将网站内容类型添加到与列表关联的内容类型集合,生成的列表内容类型都有一个不同于原始的网站内容类型的内容类型 ID。有关详细信息,请参阅Site and List Content Types。
示例
下面的示例显示的控制台应用程序由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();
}
}
}
另请参阅
引用
SPContentType(SPContentTypeId, SPContentTypeCollection, String)