SPContentType.Update method (Boolean, Boolean)
更新內容的型別定義儲存在資料庫中、 選擇性地更新所有內容的型別繼承自這個內容的型別,並選擇性地在遇到無法修改子系內容類型時擲回例外狀況。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public Sub Update ( _
updateChildren As Boolean, _
throwOnSealedOrReadOnly As Boolean _
)
'用途
Dim instance As SPContentType
Dim updateChildren As Boolean
Dim throwOnSealedOrReadOnly As Boolean
instance.Update(updateChildren, throwOnSealedOrReadOnly)
public void Update(
bool updateChildren,
bool throwOnSealedOrReadOnly
)
參數
updateChildren
Type: System.Booleantrue推入至繼承自這個內容類型的內容類型的內容類型所做的變更。
throwOnSealedOrReadOnly
Type: System.Boolean這個方法會遇到無法修改,因為它是密封或唯讀的 ; 內容型別時所擲回例外狀況的true否則false。
Exceptions
Exception | Condition |
---|---|
SPContentTypeSealedException | 這個內容型別,或是此內容類型的子系的Sealed屬性的值為true。 |
SPContentTypeReadOnlyException | 這個內容型別中的 [ ReadOnly ] 屬性的值為true。 -或- updateChildren是true ,此內容類型的子系的ReadOnly屬性的值為true。 |
備註
您所做的變更到透過物件模型的網站內容類型,您的程式碼實際上會對網站內容類型的記憶體中表示的那些變更。只有當您呼叫Update方法時,才沒有SharePoint Foundation永久的變更,藉由認可回儲存在站台資料庫中的內容類型定義。
如需詳細資訊,請參閱Updating Content Types。
當您變更網站的內容類型,您可以選擇傳播,或下推,其子父系內容類型所做的變更站台,並列出內容類型。
如需詳細資訊,請參閱Updating Child Content Types。
Examples
下列範例是一個主控台應用程式,會列舉所有的內容類型在網站集合中,尋找參考到站台欄位命名為 「 擁有者 」。如果找到的話,應用程式會嘗試刪除SPFieldLink物件的站台的內容型別和所有子系內容類型。應用程式會報告例外狀況由列印訊息至主控台。
請注意應用程式選擇不擲回例外狀況,是否更新遭遇子內容,這個型別為密封或唯讀。
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 fldName As String = "Owner"
Dim id As Guid = GetFieldId(fldName, web.Fields)
' Try to delete links to the field from site content types.
Dim found As Boolean = False
For Each ct As SPContentType In web.ContentTypes
Dim fldLnk As SPFieldLink = ct.FieldLinks(id)
If fldLnk IsNot Nothing Then
found = True
Console.WriteLine("Content type {0} links to field {1}.", ct.Name, fldName)
ct.FieldLinks.Delete(id)
Try
' Do not throw an exception if child is readonly or sealed.
ct.Update(true, false)
Console.Write("Field deleted from the site content type and all children ")
Console.WriteLine("except those that are sealed or read-only.”)
Catch ex As SPException
Console.Write("Update failed. ")
Console.WriteLine(ex.Message)
End Try
End If
Next ct
If Not found Then
Console.WriteLine("No site content type links to field {0}", fldName)
End If
Finally
web.Dispose()
End Try
Finally
site.Dispose()
End Try
Console.Write(vbCrLf + "Press ENTER to continue...")
Console.ReadLine()
End Sub
Function GetFieldId(ByVal name As String, ByVal fields As SPFieldCollection) As Guid
Dim id As Guid = Guid.Empty
Dim fld As SPField = Nothing
Try
fld = fields.GetField(name)
Catch ex As ArgumentException
Console.WriteLine("Exception thrown by a call to {0} with argument '{1}'", ex.TargetSite, name)
End Try
If fld IsNot Nothing Then
id = fld.Id
End If
Return id 'Might be Guid.Empty
End Function
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 fldName = "Owner";
Guid id = GetFieldId(fldName, web.Fields);
// Try to delete links to the field from site content types.
bool found = false;
foreach (SPContentType ct in web.ContentTypes)
{
SPFieldLink fldLnk = ct.FieldLinks[id];
if (fldLnk != null)
{
found = true;
Console.WriteLine("Content type {0} links to field {1}.",
ct.Name, fldName);
ct.FieldLinks.Delete(id);
try
{
// Do not throw an exception if child is readonly or sealed.
ct.Update(true, false);
Console.Write("Field deleted from the site content type and all children ");
Console.WriteLine("except those that are sealed or read-only.");
}
catch (SPException ex)
{
Console.Write("Update failed. ");
Console.WriteLine(ex.Message);
}
}
}
if (!found)
Console.WriteLine("No site content type links to field {0}", fldName);
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
static Guid GetFieldId(string name, SPFieldCollection fields)
{
Guid id = Guid.Empty;
SPField fld = null;
try
{
fld = fields.GetField(name);
}
catch (ArgumentException ex)
{
Console.WriteLine("Exception thrown by a call to {0} with argument '{1}'", ex.TargetSite, name);
}
if (null != fld)
id = fld.Id;
return id; //Might be Guid.Empty
}
}
}