SPContentType.Update method (Boolean)
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Sub Update ( _
updateChildren As Boolean _
)
'用途
Dim instance As SPContentType
Dim updateChildren As Boolean
instance.Update(updateChildren)
public void Update(
bool updateChildren
)
參數
updateChildren
Type: System.Booleantrue要推入至繼承自 ; 它的內容類型的內容類型所做的變更否則, false。
Exceptions
Exception | Condition |
---|---|
SPContentTypeSealedException | 這個內容型別,或是此內容類型的子系的Sealed屬性的值為true。 |
SPContentTypeReadOnlyException | 這個內容型別,或是此內容類型的子系的ReadOnly屬性的值為true。 |
備註
您所做的變更到透過物件模型的網站內容類型,您的程式碼實際上會對網站內容類型的記憶體中表示的那些變更。只有當您呼叫Update方法時,才沒有SharePoint Foundation永久的變更,藉由認可回儲存在站台資料庫中的內容類型定義。
如需詳細資訊,請參閱Updating Content Types。
當您變更網站的內容類型,您可以選擇傳播,或下推,其子父系內容類型所做的變更站台,並列出內容類型。
如需詳細資訊,請參閱Updating Child Content Types。
Examples
下列範例是主控台應用程式,藉由使用現有的網站欄,建立欄位的連結將它加入至網站的內容類型,然後更新內容的型別不需要傳播至它的子系內容類型變更。
Imports System
Imports Microsoft.SharePoint
Module ConsoleApp
Sub Main()
Dim site As SPSite = New SPSite("https://localhost")
Try
Dim web As SPWeb = site.OpenWeb()
Try
Dim ctName As String = "Contact" ' Content type to modify
Dim fldName As String = "Birthday" ' Site field to link to
' Get the content type to modify.
Dim ct As SPContentType = web.ContentTypes(ctName) ' Null if not found
If ct Is Nothing Then
Console.WriteLine("{0} is not a site content type.", ctName)
End If
' Get the field to link to.
Dim fld As SPField = Nothing
Try
fld = web.Fields.GetField(fldName) ' Throws exception if not found
Catch ex As ArgumentException
Console.WriteLine("{0} is not a site column.")
End Try
' Add a field link to the content type.
If Nothing IsNot fld AndAlso Nothing IsNot ct Then
Dim lnk As New SPFieldLink(fld)
If Nothing Is ct.FieldLinks(lnk.Id) Then ' Does it exist in collection?
'No, so add it.
ct.FieldLinks.Add(lnk)
' Update the content type.
Try
ct.Update(False) ' Do not push down
Console.WriteLine("A link to {0} has been added to content type {1}.", fldName, ctName)
Catch ex As SPException
Console.Write("Update failed. ")
Console.WriteLine(ex.Message)
End Try
Else ' We have a duplicate link
Console.WriteLine("Content type {0} already has a link to {1}.", ctName, fldName)
End If
End If
Finally
web.Dispose()
End Try
Finally
site.Dispose()
End Try
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())
{
string ctName = "Contact"; // Content type to modify
string fldName = "Birthday"; // Site field to link to
// Get the content type to modify.
SPContentType ct = web.ContentTypes[ctName]; // Null if not found
if (ct == null)
Console.WriteLine("{0} is not a site content type.", ctName);
// Get the field to link to.
SPField fld = null;
try
{
fld = web.Fields.GetField(fldName); // Throws exception if not found
}
catch (ArgumentException ex)
{
Console.WriteLine("{0} is not a site column.");
}
// Add a field link to the content type.
if (null != fld && null != ct)
{
SPFieldLink lnk = new SPFieldLink(fld);
if (null == ct.FieldLinks[lnk.Id]) // Does it exist in collection?
{
//No, so add it.
ct.FieldLinks.Add(lnk);
// Update the content type
try
{
ct.Update(false); // Do not push down
Console.WriteLine("A link to {0} has been added to content type {1}.", fldName, ctName);
}
catch (SPException ex)
{
Console.Write("Update failed. ");
Console.WriteLine(ex.Message);
}
}
else // We have a duplicate link.
{
Console.WriteLine("Content type {0} already has a link to {1}.", ctName, fldName);
}
}
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
}
請參閱
參照
Microsoft.SharePoint namespace