Export (0) Print
Expand All

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

[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.

ExceptionCondition
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.

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.

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.

using System;
using Microsoft.SharePoint;

namespace Test
{
    class ConsoleApp
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://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();
        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft