SPContentTypeCollection.Add - Méthode
Ajoute un type de contenu à la collection.
Espace de noms : Microsoft.SharePoint
Assembly : Microsoft.SharePoint (dans Microsoft.SharePoint.dll)
Syntaxe
'Déclaration
Public Function Add ( _
contentType As SPContentType _
) As SPContentType
'Utilisation
Dim instance As SPContentTypeCollection
Dim contentType As SPContentType
Dim returnValue As SPContentType
returnValue = instance.Add(contentType)
public SPContentType Add(
SPContentType contentType
)
Paramètres
contentType
Type : Microsoft.SharePoint.SPContentTypeLe type de contenu à ajouter à la collection.
Valeur renvoyée
Type : Microsoft.SharePoint.SPContentType
Une instance du type de contenu d'entièrement initialisée.
Exceptions
Exception | Condition |
---|---|
SPException | contentType est null. - ou - Le type de contenu est déjà dans la collection. - ou - La collection est prête. |
Remarques
Un type de contenu n'est pas complètement initialisé jusqu'à ce qu'il est ajouté à une collection de type de contenu. Entre autres choses, il reçoit un ID de type de contenu qui s'applique à la collection. Par exemple, lorsque vous ajoutez un type de contenu de site à la collection de type de contenu associée à une liste, le type de contenu de liste résultant possède un ID de type de contenu qui est différent du type de contenu de site d'origine. Pour plus d'informations, consultez Site and List Content Types.
Exemples
L'exemple suivant montre une application de console qui obtient un type de contenu de la collection qui est retournée par la propriété AvailableContentTypes . Puis, il ajoute ce type de contenu à la collection qui est retournée par la propriété ContentTypes d'un objet SPList .
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();
}
}
}
Voir aussi
Référence
SPContentTypeCollection classe
SPContentTypeCollection - Membres
Microsoft.SharePoint - Espace de noms
SPContentType(SPContentTypeId, SPContentTypeCollection, String)