Obtain content types:
SPWeb web = SPContext.Current.Web; string contentTypeName = "My Content Type"; SPList list = SPContext.Current.List; // Example #1 SPContentType ct = web.AvailableContentTypes[new SPContentTypeId("0x0101")]; // Example #2 SPContentType ct = web.AvailableContentTypes[contentTypeName]; // Example #3 SPContentType ct = web.AvailableContentTypes[SPBuiltInContentTypeId.Item]; // Example #4 SPContentType ct = web.ContentTypes[contentTypeName]; // Example #5 SPContentType ct = list.ContentTypes[contentTypeName]; -or- SPContentTypeCollection contentTypes = list.ContentTypes; SPContentType ct = contentTypes[contentTypeName]; // Example #6 IList<SPContentType> folderContentTypes = list.RootFolder.UniqueContentTypeOrder; foreach (SPContentType ct in folderContentTypes) { // Use properties of ct: // ct.Id // ct.Id.IsChildOf(SPBuiltInContentTypeId.UntypedDocument); // ... }
Add a content type to a list:
// 'web' is of type SPWeb // 'ct' is of type SPContentType SPList list = web.Lists["My Content Type"]; list.ContentTypes.Add(ct); list.Update();
Create a content type:
// Example #1 // 'web' is of type SPWeb // 'contentTypeName' is the name of the content type to create if (web.AvailableContentTypes[contentTypeName] == null) { // 0x0101 indicates Document Content Type // http://msdn.microsoft.com/en-us/library/aa543822.aspx SPContentType parentContentType = web.AvailableContentTypes[new SPContentTypeId("0x0101")]; SPContentType newContentType = new SPContentType(parentContentType, web.ContentTypes, contentTypeName); web.ContentTypes.Add(newContentType); } // Example #2 SPContentType ct = web.ContentTypes["My Content Type"]; if (ct == null) { // Obtain the parent. SPContentType parentContentType = web.AvailableContentTypes[SPBuiltInContentTypeId.Document]; // Create the new content type. ct = new SPContentType(parentContentType, web.ContentTypes, "My Content Type"); web.ContentTypes.Add(ct); // Initialize the new content type. ct = web.ContentTypes[ct.Id]; ct.Group = "Custom Content Types"; // Add columns. Child content types inherit the columns. ct.FieldLinks.Add( new SPFieldLink(web.Fields["MyField"])); // MyField has to exist in the web // Commit changes. ct.Update(); } // Example #3 SPWeb web = SPContext.Current.Web; // Obtain the parent a content. SPContentType parentContentType = web.AvailableContentTypes[SPBuiltInContentTypeId.Item]; // Create the new content type. SPContentType ct = new SPContentType(parentContentType, web.ContentTypes, "New Content Type"); web.ContentTypes.Add(ct); // Initialize the new content type. ct = web.ContentTypes[ct.Id]; ct.Group = "My Group Content Types"; // Add the columns. Child content types inherit the columns. // field1 and field2 are variables of types that derive from SPField // such as SPFieldBoolean or SPFieldText. ct.FieldLinks.Add(new SPFieldLink(field1)); ct.FieldLinks.Add(new SPFieldLink(field2)); // Commit changes. ct.Update();
Determine the parent content type:
// Example #1 SPListItem item = SPContext.Current.ListItem; if (item != null) { bool isDS = item.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.DocumentSet)); // ... }
Add content types identified by the names 'MyContentType1' and 'MyContentType2' to the root folder of a library 'lib':
List<SPContentType> contentTypes = new List<SPContentType>(); contentTypes.Add(lib.ContentTypes["MyContentType1"]); contentTypes.Add(lib.ContentTypes["MyContentType2"]); lib.RootFolder.UniqueContentTypeOrder = contentTypes; lib.RootFolder.Update();
Obtain the name of a content type in an event receiver. Return null if the name can't be determined:
private string GetContentTypeName(SPItemEventProperties properties) { if (properties.AfterProperties["ContentTypeId"] != null) { string contentTypeIdStr = properties.AfterProperties["ContentTypeId"].ToString(); SPContentTypeId contentTypeId = new SPContentTypeId(contentTypeIdStr); SPContentType contentType = properties.List.ContentTypes[contentTypeId]; if (contentType != null) { string contentTypeName = contentType.Name.ToLower(); return contentTypeName; } else { return null; } } else { return null; } }
Make sure a library 'lib' accepts multiple content types:
if (!lib.AllowContentTypes) { lib.ContentTypesEnabled = true; lib.Update(); }