Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
public void Update(
	bool updateChildren,
	bool throwOnSealedOrReadOnly
)

Parameters

updateChildren
Type: System.Boolean
true to push the changes made to the content type down to content types that inherit from this content type.
throwOnSealedOrReadOnly
Type: System.Boolean
true to throw an exception when this method encounters a content type that cannot be modified because it is sealed or read-only; 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 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.

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

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 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
        }
    }
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.