SPContentType 类

表示网站或列表内容类型。

继承层次结构

System.Object
  Microsoft.SharePoint.SPContentType

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public NotInheritable Class SPContentType
用法
Dim instance As SPContentType
public sealed class SPContentType

备注

内容类型旨在帮助用户更有意义的方式组织 SharePoint 内容。内容类型是内容的您想要应用于特定类别的设置的可重用集合。内容类型使您能够集中、 可重用的方式来管理元数据和行为的文档或项目的类型。

For more information, see Introduction to Content Types.

示例

下面的示例是一个控制台应用程序创建内容类型,并将其添加到网站内容类型集合。接下来,本示例创建字段 (列),并链接到新的内容类型的字段。最后,该示例创建一个列表,并将新的内容类型添加到列表所属的内容类型集合。

之前运行的应用程序,您需要向项目中添加对Microsoft.Sharepoint.dll的引用。

Imports System
Imports Microsoft.SharePoint

Module Test

#Region "fields"
   Private requestUrl As String = "https://localhost"

   ' Name and type of content type to create.
   Private ctName As String = "Customer"
   Private parentId As SPContentTypeId = SPBuiltInContentTypeId.Contact

   ' Name and type of site column to create.
   Private fldName As String = "LastOrder"
   Private fldType As SPFieldType = SPFieldType.DateTime

   ' Name, type, and description of list to create.
   Private lstName As String = "Customers"
   Private lstType As SPListTemplateType = SPListTemplateType.Contacts
   Private lstDesc As String = "A list of customers."
#End Region

   Sub Main()
      Using site As SPSite = New SPSite(requestUrl)
         Using web As SPWeb = site.OpenWeb()
            If ValidNames(web) Then
               ' Create a new site content type.
               Dim cts As SPContentTypeCollection = web.ContentTypes
               Dim ct As New SPContentType(cts(parentId), cts, ctName)

               ' Add the content type to the site collection.
               cts.Add(ct)
               Console.WriteLine( _
                  "Added {0} content type to site collection.", ct.Name)

               ' Create a site field to link to.
               Dim fields As SPFieldCollection = web.Fields
               fldName = fields.Add(fldName, fldType, False)
               Console.WriteLine("Created {0} site column.", fldName)

               ' Link the content type to the field.
               Dim field As SPField = fields.GetField(fldName)
               Dim fieldLink As SPFieldLink = New SPFieldLink(field)
               ct.FieldLinks.Add(fieldLink)
               Console.WriteLine( _
                  "Linked {0} content type to {1} column.", _
                  ct.Name, field.InternalName)

               ' Commit changes to the database.
               ct.Update()

               ' Create a list.
               Dim lists As SPListCollection = web.Lists
               Dim listID As Guid = lists.Add(lstName, lstDesc, lstType)
               Dim list As SPList = lists(listID)
               list.OnQuickLaunch = True
               list.Update()
               Console.WriteLine("Created {0} list.", list.Title)

               ' Apply the new content type to the list.
               list.ContentTypesEnabled = True
               If list.IsContentTypeAllowed(ct) Then
                  ' Add the new content type.
                  Dim lstCT As SPContentType = list.ContentTypes.Add(ct)
                  ' Remove the default content type.
                  list.ContentTypes(0).Delete()
                  ' Commit changes to the list.
                  list.Update()
                  Console.WriteLine("Applied {0} content type to {1} list.", _
                                    lstCT.Name, list.Title)
               Else
                  Console.WriteLine("{0} list does not allow {1} content type.", _
                                    list.Title, ct.Name)
               End If
            End If
         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub

   ' Checks for duplicate content type, field, and list names.
   Private Function ValidNames(ByRef web As SPWeb) As Boolean
      Dim valid As Boolean = True

      ' Duplicate content type name?
      If web.AvailableContentTypes(ctName) IsNot Nothing Then
         valid = False
         Console.WriteLine("Duplicate content type name.")
      End If

      ' Invalid characters in content type name?
      Try
         SPContentType.ValidateName(ctName)
      Catch ex As SPException
         valid = False
         Console.WriteLine("Invalid character in content type name.")
      End Try

      ' Duplicate field name?
      If web.Fields.ContainsField(fldName) Then
         valid = False
         Console.WriteLine("Duplicate field name.")
      End If

      ' Duplicate list name?
      Try
         Dim list As SPList = web.Lists(lstName) ' Exception if not found
         valid = False
         Console.WriteLine("Duplicate list name.")
      Catch ex As ArgumentException
         ' List name does not exist.
      End Try

      Return valid
   End Function

End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      #region fields
      private static string requestUrl = "https://localhost";

      // Name and type of content type to create.
      private static string ctName = "Customer";
      private static SPContentTypeId parentId = SPBuiltInContentTypeId.Contact;

      // Name and type of site column to create.
      private static string fldName = "LastOrder";
      private static SPFieldType fldType = SPFieldType.DateTime;

      // Name, type, and description of list to create.
      private static string lstName = "Customers";
      private static SPListTemplateType lstType = SPListTemplateType.Contacts;
      private static string lstDesc = "A list of customers.";
      #endregion

      static void Main(string[] args)
      {
         using (SPSite site = new SPSite(requestUrl))
         {
            using (SPWeb web = site.OpenWeb())
            {
               // Check for duplicate content type, field, and list names.
               if (ValidNames(web))
               {
                  // Create a new site content type.
                  SPContentTypeCollection cts = web.ContentTypes;
                  SPContentType ct = new SPContentType(cts[parentId], // parent
                                                       cts,           // collection
                                                       ctName);       // name

                  // Add the content type to the site collection.
                  cts.Add(ct);
                  Console.WriteLine(
                      "Added {0} content type to site collection.", ct.Name);

                  // Create a site field to link to.
                  SPFieldCollection fields = web.Fields;
                  fldName = fields.Add(fldName, fldType, false);
                  Console.WriteLine("Created {0} site column.", fldName);

                  // Link the content type to the field.
                  SPField field = fields.GetField(fldName);
                  SPFieldLink fieldLink = new SPFieldLink(field);
                  ct.FieldLinks.Add(fieldLink);
                  Console.WriteLine(
                            "Linked {0} content type to {1} column.",
                             ct.Name, field.InternalName);

                  // Commit changes to the database.
                  ct.Update();

                  // Create a list.
                  SPListCollection lists = web.Lists;
                  Guid listID = lists.Add(lstName, lstDesc, lstType);
                  SPList list = lists[listID];
                  list.OnQuickLaunch = true;
                  list.Update();
                  Console.WriteLine("Created {0} list.", list.Title);

                  // Apply the new content type to the list.
                  list.ContentTypesEnabled = true;
                  if (list.IsContentTypeAllowed(ct))
                  {
                     // Add the new content type.
                     SPContentType lstCT = list.ContentTypes.Add(ct);
                     // Remove the default content type.
                     list.ContentTypes[0].Delete();
                     // Commit changes to the list.
                     list.Update();
                     Console.WriteLine("Applied {0} content type to {1} list.",
                                        lstCT.Name, list.Title);
                  }
                  else
                  {
                     Console.WriteLine("{0} list does not allow {1} content type.",
                                        list.Title, ct.Name);
                  }

               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }

      // Checks for duplicate content type, field, and list names.
      static bool ValidNames(SPWeb web)
      {
         bool valid = true;

         // Duplicate content type name?
         if (web.AvailableContentTypes[ctName] != null)
         {
            valid = false;
            Console.WriteLine("Duplicate content type name.");
         }

         // Invalid characters in content type name?
         try
         {
            SPContentType.ValidateName(ctName);
         }
         catch (SPException ex)
         {
            Console.WriteLine("Invalid character in content type name.");
         }


         // Duplicate field name?
         if (web.Fields.ContainsField(fldName))
         {
            valid = false;
            Console.WriteLine("Duplicate field name.");
         }

         // Duplicate list name?
         try
         {
            SPList list = web.Lists[lstName]; // Exception if not found
            valid = false;
            Console.WriteLine("Duplicate list name.");
         }
         catch (ArgumentException ex)
         {
            // List name does not exist.
         }

         return valid;
      }
   }
}

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPContentType 成员

Microsoft.SharePoint 命名空间

其他资源

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy