SPContentType.Update Method (Boolean, Boolean)
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
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 Windows SharePoint Services 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 } } }