SPContentType.Update 方法 (Boolean)
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public Sub Update ( _
updateChildren As Boolean _
)
用法
Dim instance As SPContentType
Dim updateChildren As Boolean
instance.Update(updateChildren)
public void Update(
bool updateChildren
)
参数
updateChildren
类型:System.Booleantrue推送到下 ; 从继承的内容类型的内容类型所做的更改否则为false。
异常
异常 | 条件 |
---|---|
SPContentTypeSealedException | 此内容类型或此内容类型的子级的Sealed属性的值为true。 |
SPContentTypeReadOnlyException | 此内容类型或此内容类型的子级的ReadOnly属性的值为true。 |
备注
向网站内容类型通过对象模型中进行更改时,您的代码实际内存中表示的网站内容类型中进行这些更改。仅当调用Update方法时,才SharePoint Foundation永久保存这些更改,通过提交回网站数据库中存储的内容类型定义。
For more information, see Updating Content Types.
当对网站内容类型进行更改,您可以选择传播,或向下推进,向其子父内容类型所做的更改网站和列表内容类型。
For more information, see Updating Child Content Types.
示例
下面的示例是通过使用现有的网站列,创建一个字段链接的控制台应用程序将其添加到网站内容类型,然后无需传播对其子内容类型的更改更新的内容类型。
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();
}
}
}