SPContentType.Update method (Boolean, Boolean)
Updates the content type definition that is stored in the database, optionally updates all content types that inherit from this content type, and optionally throws an exception when it encounters a child content type that cannot be modified.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'Declaration
Public Sub Update ( _
updateChildren As Boolean, _
throwOnSealedOrReadOnly As Boolean _
)
'Usage
Dim instance As SPContentType
Dim updateChildren As Boolean
Dim throwOnSealedOrReadOnly As Boolean
instance.Update(updateChildren, throwOnSealedOrReadOnly)
public void Update(
bool updateChildren,
bool throwOnSealedOrReadOnly
)
Parameters
updateChildren
Type: System.Booleantrue to push the changes made to the content type down to content types that inherit from this content type.
throwOnSealedOrReadOnly
Type: System.Booleantrue to throw an exception when this method encounters a content type that cannot be modified because it is sealed or read-only; otherwise false.
Exceptions
Exception | Condition |
---|---|
SPContentTypeSealedException | The Sealed property of this content type or of a child of this content type has a value of true. |
SPContentTypeReadOnlyException | The ReadOnly property of this content type has a value of true. -or- updateChildren is true and the ReadOnly property of a child of this content type has a value of true. |
Remarks
As you make changes to a site content type through the object model, your code is actually making those changes to the in-memory representation of the site content type. Only when you call the Update method does SharePoint Foundation make those changes permanent, by committing them back to the content type definition that is stored in the site database.
For more information, see Updating Content Types.
When you make changes to a site content type, you can choose to propagate, or push down, changes made to the parent content type to its child site and list content types.
For more information, see Updating Child Content Types.
Examples
The following example is a console application that enumerates all content types in the site collection, looking for references to a site field named “Owner.” If one is found, the application attempts to delete the SPFieldLink object from the site content type and all child content types. The application reports exceptions by printing messages to the console.
Note that the application chooses not to throw an exception if the update encounters a child content type that is sealed or read-only.
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
}
}
}