有没有办法以编程方式使用XML定义文件创建SharePoint 2010内容类型? 可以通过以下方式添加SPField:
SPContext.Current.Web.Fields.AddFieldAsXml("<xml />");
是否有任何类似的方式以编程方式将内容类型添加到网站集/网站?
答案 0 :(得分:6)
您可以以编程方式创建/添加内容类型,但不能使用XML定义(据我所知)。您必须构造它,将其添加到内容类型集合,并手动将字段引用添加到字段链接集合。
一个粗略的例子是:
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.OpenWeb())
{
SPContentType contentType = new SPContentType(web.ContentTypes["Document"], web.ContentTypes, "Financial Document");
web.ContentTypes.Add(contentType);
contentType.Group = "Financial Content Types";
contentType.Description = "Base financial content type";
contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField("OrderDate")));
contentType.FieldLinks.Add(new SPFieldLink(web.Fields.GetField("Amount")));
contentType.Update();
}
}
您无法以这种方式控制内容类型ID。我更喜欢按照Greg Enslow的回应使用功能。
答案 1 :(得分:0)
最常用的方法是使用功能定义,然后激活网站集的功能。该功能的xml将如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Parent ContentType: Document (0x0101) -->
<ContentType ID="0x0101000728167cd9c94899925ba69c4af6743e"
Name="Financial Document"
Group="Financial Content Types"
Description="Base financial content type"
Version="0">
<FieldRefs>
<FieldRef ID="{1511BF28-A787-4061-B2E1-71F64CC93FD5}" Name="OrderDate" DisplayName="Date" Required="FALSE"/>
<FieldRef ID="{060E50AC-E9C1-4D3C-B1F9-DE0BCAC300F6}" Name="Amount" DisplayName="Amount" Required="FALSE"/>
</FieldRefs>
</ContentType>
</Elements>
请参阅http://msdn.microsoft.com/en-us/library/ms463449.aspx的完整示例。
您是否有特定原因尝试使用对象模型?
答案 2 :(得分:0)
通过 SharePoint 2010 SSOM API 支持不支持基于指定架构(XML)的内容类型供应。但是Reflection
在这里得到了拯救,内容类型功能可以扩展,并支持基于指定的模式创建它,如下所示:
namespace ContentTypesDeployment
{
/// <summary>
/// Content Type
/// </summary>
public static class SPContentTypeExtensions
{
/// <summary>
/// Creates a content type based on the specified schema.
/// </summary>
/// <returns>
/// An instance of the new content type.
/// </returns>
/// <param name="contentTypes">Content Type collection</param>
/// <param name="schemaXml">A Collaborative Application Markup Language (CAML) string that contains the schema.</param>
public static SPContentType AddContentTypeAsXml(this SPContentTypeCollection contentTypes, string schemaXml)
{
SPContentType contentType;
using (var xrdr = new XmlTextReader(new StringReader(schemaXml)))
{
xrdr.ProhibitDtd = true;
contentType = contentTypes.CreateContentType();
LoadXmlInternal(contentType, xrdr);
contentTypes.Add(contentType);
}
return contentType;
}
/// <summary>
/// Create content type
/// </summary>
/// <param name="contentTypes"></param>
/// <returns></returns>
private static SPContentType CreateContentType(this SPContentTypeCollection contentTypes)
{
var constructor = (typeof(SPContentType)).GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
null,
Type.EmptyTypes,
null);
var contentType = (SPContentType)constructor.Invoke(new object[0]);
contentType.SetWeb(contentTypes.GetWeb());
return contentType;
}
/// <summary>
/// Load schema for content type
/// </summary>
/// <param name="contentType"></param>
/// <param name="xmlReader"></param>
private static void LoadXmlInternal(SPContentType contentType, XmlReader xmlReader)
{
var loadMethod = contentType.GetType().GetMethod("Load",
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new[] { typeof(XmlReader) },
null);
loadMethod.Invoke(contentType, new object[] { xmlReader });
}
private static SPWeb GetWeb(this SPContentTypeCollection contentTypes)
{
var webProp = typeof(SPContentTypeCollection).GetProperty("Web", BindingFlags.NonPublic | BindingFlags.Instance);
return (SPWeb)webProp.GetValue(contentTypes, null);
}
private static void SetWeb(this SPContentType contentType,SPWeb web)
{
var webProp = typeof(SPContentType).GetProperty("Web", BindingFlags.NonPublic | BindingFlags.Instance);
webProp.SetValue(contentType, web, null);
}
}
}
using (var site = new SPSite("http://intranet.contoso.com"))
{
using (var web = site.OpenWeb())
{
var manifestDoc = new XmlDocument();
manifestDoc.Load("ContentTypeManifest.xml");
var contentTypeElement = manifestDoc.GetElementsByTagName("ContentType")[0];
var ctSchema = contentTypeElement.OuterXml;
web.ContentTypes.AddContentTypeAsXml(ctSchema); //create a content type based on the specified schema (XML)
}
}
有关详细信息,请参阅this发布。