2. Content Type Metadata
You’ve seen how content types are organized into a
hierarchy and how inheritance determines the functionality that is
enabled by a given content type. Now let’s take a look at the type of
metadata, and therefore the types of functionality, that can be exposed
by content types.
Workflow Associations
Workflows can be associated with content types and
set to run in response to certain events. By attaching workflows to
content types rather than individual lists, you can define business
processes for specific types of data regardless of where the data is
stored within a SharePoint site.
Document Template
For some types of content, particularly those based
on Microsoft Office documents such as Word or Excel docs, custom
templates can be specified that users can populate with relevant
details. Details of such a template can be specified as content type
metadata, allowing the template to be used wherever the content type is
added to a document library.
Display, Edit, and New Forms
Each content type can define custom display, edit,
and new forms allowing customization of the user interface presented at
these stages. Various options are available for customizing these
forms; however, from a content type perspective, it’s important to note
that two properties exist for each form. For example, to set the
Display form, you can use a DisplayFormTemplateName property and a
DisplayFormUrl property. Which property you use depends on the level of
customization that’s required. By setting the DisplayFormTemplateName
property, you can specify a template that will be used by the DataForm
Web part to render the appropriate view. However, if a greater level of
customization is required, the DisplayFormUrl can be set to the URL for
a custom Active Server Page Framework (ASPX) page, allowing a much
greater degree of flexibility with the drawback that none of the usual
SharePoint user interface elements will be present on the page by
default.
Mobile Display, Edit, New Pages
New in 2010—SharePoint
2010 provides a number of new features for rendering content for mobile
browsers. One example of this new functionality is the ability to
specify custom forms for creating and editing content using mobile
browsers at the content type level.
XML Documents
The ability to add practically any XML-based
metadata to a content type is an incredibly powerful feature.
Interestingly, some of the metadata already described, although
accessible through the object model via dedicated properties, is
actually attached to a content type via additional XML documents.
The following code sample shows how metadata can be
added by creating a custom class that supports XML serialization and
then setting properties on the class to contain the appropriate values.
Using this technique, you can attach practically any additional data to
a content type.
static void Main(string[] args)
{
string siteUrl = "http://localhost";
Program p = new Program();
using (SPSite site = new SPSite(siteUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPContentType newContentType;
newContentType = p.CreateContentType(web, "MyFirstContentType", "Item");
CustomMetadata metaData = new CustomMetadata();
metaData.MyIntegerProperty = 46;
metaData.MyTextProperty = "Data stored as custom metadata";
p.AddCustomMetadata(metaData, newContentType);
metaData = null;
metaData = p.ReadCustomMetaData(newContentType);
Console.WriteLine(metaData.MyTextProperty);
}
}
Console.ReadLine();
}
void AddCustomMetadata(CustomMetadata data, SPContentType contentType)
{
XmlSerializer s = new XmlSerializer(typeof(CustomMetadata));
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
s.Serialize(writer, data);
}
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
contentType.XmlDocuments.Add(doc);
}
CustomMetadata ReadCustomMetaData(SPContentType contentType)
{
CustomMetadata data;
Type t = typeof(CustomMetadata);
var attrib = (XmlRootAttribute)t.GetCustomAttributes(
typeof(XmlRootAttribute), false).FirstOrDefault();
if (contentType.XmlDocuments[attrib.Namespace] != null)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(contentType.XmlDocuments[attrib.Namespace]);
using (StringReader sr = new StringReader(
contentType.XmlDocuments[attrib.Namespace]))
{
using (XmlReader rdr = XmlReader.Create(sr))
{
XmlSerializer s = new XmlSerializer(t);
data = (CustomMetadata)s.Deserialize(rdr);
}
}
return data;
}
else
{
throw new KeyNotFoundException();
}
}
[XmlRoot(Namespace="Http://www.chaholl.com/SP2010Apps/ContentTypesDemo")]
public class CustomMetadata
{
[XmlElement()]
public int MyIntegerProperty { get; set; }
[XmlElement()]
public string MyTextProperty { get; set; }
}