SPContentType.Update Method (Boolean)

Updates the content type definition that is stored in the database and, optionally, updates all content types that inherit from this content type.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
<ClientCallableExceptionConstraintAttribute(FixedId := "2", ErrorCode := , ErrorType := GetType(SPContentTypeReadOnlyException),  _
    Condition := "This content type or one of its children (if updateChildren is true) is marked as ReadOnly",  _
    ErrorValue := "Content type is only")> _
<ClientCallableAttribute> _
<ClientCallableExceptionConstraintAttribute(FixedId := "3", ErrorCode := , ErrorType := GetType(SPException),  _
    Condition := "Collection is read only or updateChildren is true and content type has no children.",  _
    ErrorValue := "Can not modify or update collection")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "1", ErrorCode := , ErrorType := GetType(SPContentTypeSealedException),  _
    Condition := "UpdateChildren is true and one of the children is marked as sealed.",  _
    ErrorValue := "Content type is sealed")> _
Public Sub Update ( _
    updateChildren As Boolean _
)
'Usage
Dim instance As SPContentType
Dim updateChildren As Boolean

instance.Update(updateChildren)
[ClientCallableExceptionConstraintAttribute(FixedId = "2", ErrorCode = , ErrorType = typeof(SPContentTypeReadOnlyException), 
    Condition = "This content type or one of its children (if updateChildren is true) is marked as ReadOnly", 
    ErrorValue = "Content type is only")]
[ClientCallableAttribute]
[ClientCallableExceptionConstraintAttribute(FixedId = "3", ErrorCode = , ErrorType = typeof(SPException), 
    Condition = "Collection is read only or updateChildren is true and content type has no children.", 
    ErrorValue = "Can not modify or update collection")]
[ClientCallableExceptionConstraintAttribute(FixedId = "1", ErrorCode = , ErrorType = typeof(SPContentTypeSealedException), 
    Condition = "UpdateChildren is true and one of the children is marked as sealed.", 
    ErrorValue = "Content type is sealed")]
public void Update(
    bool updateChildren
)

Parameters

  • updateChildren
    Type: System.Boolean

    true to push the changes made to the content type down to content types that inherit from it; 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 or 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 creates a field link by using an existing site column, adds it to a site content type, and then updates the content type without propagating the change to its 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();
        }
    }
}

See Also

Reference

SPContentType Class

SPContentType Members

Update Overload

Microsoft.SharePoint Namespace

Other Resources

Updating Content Types

Updating Child Content Types

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy