SPContentType class
Represents a site or list content type.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Content types are designed to help users organize their SharePoint content in a more meaningful way. A content type is a reusable collection of settings that you want to apply to a certain category of content. Content types enable you to manage the metadata and behaviors of a document or item type in a centralized, reusable way.
For more information, see Introduction to Content Types.
The following example is a console application that creates a content type and adds it to the site content type collection. Next, the example creates a field (column) and links the field to the new content type. Finally, the example creates a list and adds the new content type to the content type collection that belongs to the list.
Before running the application, you need to add a reference to Microsoft.Sharepoint.dll to your project.
using System; using Microsoft.SharePoint; namespace Test { class ConsoleApp { #region fields private static string requestUrl = "http://localhost"; // Name and type of content type to create. private static string ctName = "Customer"; private static SPContentTypeId parentId = SPBuiltInContentTypeId.Contact; // Name and type of site column to create. private static string fldName = "LastOrder"; private static SPFieldType fldType = SPFieldType.DateTime; // Name, type, and description of list to create. private static string lstName = "Customers"; private static SPListTemplateType lstType = SPListTemplateType.Contacts; private static string lstDesc = "A list of customers."; #endregion static void Main(string[] args) { using (SPSite site = new SPSite(requestUrl)) { using (SPWeb web = site.OpenWeb()) { // Check for duplicate content type, field, and list names. if (ValidNames(web)) { // Create a new site content type. SPContentTypeCollection cts = web.ContentTypes; SPContentType ct = new SPContentType(cts[parentId], // parent cts, // collection ctName); // name // Add the content type to the site collection. cts.Add(ct); Console.WriteLine( "Added {0} content type to site collection.", ct.Name); // Create a site field to link to. SPFieldCollection fields = web.Fields; fldName = fields.Add(fldName, fldType, false); Console.WriteLine("Created {0} site column.", fldName); // Link the content type to the field. SPField field = fields.GetField(fldName); SPFieldLink fieldLink = new SPFieldLink(field); ct.FieldLinks.Add(fieldLink); Console.WriteLine( "Linked {0} content type to {1} column.", ct.Name, field.InternalName); // Commit changes to the database. ct.Update(); // Create a list. SPListCollection lists = web.Lists; Guid listID = lists.Add(lstName, lstDesc, lstType); SPList list = lists[listID]; list.OnQuickLaunch = true; list.Update(); Console.WriteLine("Created {0} list.", list.Title); // Apply the new content type to the list. list.ContentTypesEnabled = true; if (list.IsContentTypeAllowed(ct)) { // Add the new content type. SPContentType lstCT = list.ContentTypes.Add(ct); // Remove the default content type. list.ContentTypes[0].Delete(); // Commit changes to the list. list.Update(); Console.WriteLine("Applied {0} content type to {1} list.", lstCT.Name, list.Title); } else { Console.WriteLine("{0} list does not allow {1} content type.", list.Title, ct.Name); } } } } Console.Write("\nPress ENTER to continue..."); Console.ReadLine(); } // Checks for duplicate content type, field, and list names. static bool ValidNames(SPWeb web) { bool valid = true; // Duplicate content type name? if (web.AvailableContentTypes[ctName] != null) { valid = false; Console.WriteLine("Duplicate content type name."); } // Invalid characters in content type name? try { SPContentType.ValidateName(ctName); } catch (SPException ex) { Console.WriteLine("Invalid character in content type name."); } // Duplicate field name? if (web.Fields.ContainsField(fldName)) { valid = false; Console.WriteLine("Duplicate field name."); } // Duplicate list name? try { SPList list = web.Lists[lstName]; // Exception if not found valid = false; Console.WriteLine("Duplicate list name."); } catch (ArgumentException ex) { // List name does not exist. } return valid; } } }