Content Type
From WSSWiki
A new and core concept added to Windows SharePoint Services 3.0 is known as a Content Type. Content Types are widely used throughout Windows SharePoint Services as a way to help organize the content stored in SharePoint.
Contents |
[edit] Introduction to Content Types
A content type is a group of reusable settings and can be defined for any item type within Windows SharePoint Services. Content types provide a way of consistently organizing content across libraries and lists within Site Collections. A single list or library can contain one (1) or more content types.
[edit] What makes up a content type?
A single content type can define the following:
- Metedata
- Document Templates (applies to document content types only)
- Workflows
- Information Management Policies
- Document Information Panel
- Document Conversions (applies to document content types only)
[edit] How they work
Content types work around a hierachical structure with allow one (1) content type to inherit the make and properties from another content type.
Content types first get defined at a site level via the Site Content Type Gallary; At this level, they are known as Site Content Types. If a content type is defined at the top level site of a site collection, it is available to all sites within that site collection.
[edit] Enabling Content Types
The following instructions outline the process of enabling content types on a list or library:
- Navigate to the list or library you wish to contain the content type
- Click Settings and then List / Library Settings
- Click on Advanced Settings and select the Yes option on the "Allow management of content types?" section
- Click OK
At this point a new section will appear titled Content Types. From here, you can add and change the order of content types for this specific list or library.
[edit] Developer Information
[edit] Programmatically adding a content type
The following demonstrates adding a new content type that inherits from the 'Document' content type.
SPContentTypeCollection contentTypes = web.ContentTypes; SPContentType documentContentType = contentTypes["Document"]; SPContentType excelContentType = new SPContentType(documentContentType, contentTypes, "Excel Document"); SPContentType.Group = "Office Documents"; contentTypes.Add(excelContentType); web.Update();
[edit] Programmatically adding a template to a content type
The following section demonstrates how to add a document template to a content type using code.
Assumption(s)
- You have a content type already created (in this example called 'Excel Document')
string ctName = "Excel Document";
string templateName = "Template.xlsx";
SPContentType excelDocument = web.ContentTypes[ctName];
SPFolder excelDocumentFolder = excelDocument.ResourceFolder;
SPFile excelTemplate = null;
foreach (SPFile file in excelDocumentFolder.Files)
{
if (file.Name == templateName)
{
excelTemplate = file;
break;
}
}
if (excelTemplate == null)
{
byte[] excelDoc = System.IO.File.ReadAllBytes(string.Format(@"C:\Templates\{0}", templateName));
excelTemplate = excelDocumentFolder.Files.Add(templateName, excelDoc);
}
if (excelDocument.DocumentTemplate != templateName)
{
excelDocument.DocumentTemplate = templateName;
excelDocument.Update();
}
