使用XML定义文件以编程方式创建SharePoint 2010内容类型

Mar*_*erg 2 c# xml sharepoint-2010 contenttype

有没有办法以编程方式使用XML定义文件创建SharePoint 2010内容类型?可以通过以下方式添加SPField:

SPContext.Current.Web.Fields.AddFieldAsXml("<xml />");
Run Code Online (Sandbox Code Playgroud)

是否有任何类似的方式以编程方式将内容类型添加到网站集/网站?

Nig*_*ing 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,您无法通过这种方式控制内容类型ID.我更喜欢按照Greg Enslow的回应使用功能.