Export (0) Print
Expand All

SPContentType.Update Method

Updates the content type definition that is stored in the database with any changes you have made programmatically.

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

public void Update()

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 and Updating Child Content Types.

Important noteImportant

A content type that does not belong to a collection cannot be updated. If you create a new content type and modify its properties, you must add it to a collection before calling the Update method.

The following example changes the order of fields in the default content type for the Announcements list, putting the Expires field first and making it a required field. The default order in the Announcement content type is “Title, Body, Expires.” The code example changes the order to “Expires, Title, Body.” Note that the change desired here could also be accomplished by passing an array with only two strings, “Expires, Title”.

The application that includes this code example imports the System and Microsoft.Sharepoint namespaces, and the project that includes the application has references to System.dll and Microsoft.Sharepoint.dll.

using (SPSite site = new SPSite("http://localhost"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPContentType ct = web.Lists["Announcements"].ContentTypes["Announcement"];
        SPFieldLinkCollection flinks = ct.FieldLinks;

        // Put the Expires field first and make it required.
        flinks.Reorder(new[]{"Expires", "Title", "Body"});
        flinks["Expires"].Required = true;
        ct.Update();
    }
}
Show:
© 2015 Microsoft