Share via


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

Syntax

'Declaration
Public Sub Update
'Usage
Dim instance As SPContentType

instance.Update()
public void Update()

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

Important

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.

Examples

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.

Dim site As SPSite = New SPSite("https://localhost")
Try
    Dim web As SPWeb = site.OpenWeb()
    Try
        Dim ct As SPContentType = web.Lists("Announcements").ContentTypes("Announcement")
        Dim flinks As SPFieldLinkCollection = ct.FieldLinks

        ' Put the Expires field first and make it required.
        flinks.Reorder(New String() {"Expires", "Title", "Body"})
        flinks("Expires").Required = True
        ct.Update()
        
    Finally
        web.Dispose()
    End Try
Finally
    site.Dispose()
End Try
using (SPSite site = new SPSite("https://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();
    }
}

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