簡體   English   中英

使用 XML 定義文件以編程方式創建 SharePoint 2010 內容類型

[英]Programmatically create SharePoint 2010 content type using XML definition file

有沒有辦法使用 XML 定義文件以編程方式創建 SharePoint 2010 內容類型? 可以通過以下方式添加 SPFields:

SPContext.Current.Web.Fields.AddFieldAsXml("<xml />");

是否有任何類似的方法可以以編程方式將內容類型添加到網站集/網站?

您可以以編程方式創建/添加內容類型,但不能使用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的回復使用功能。

通過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)
                }
            }

有關更多詳細信息,請遵循帖子。

最常見的方法是使用功能定義,然后為您的網站集激活該功能。 該功能的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/zh-cn/library/ms463449.aspx的完整示例。

您嘗試使用對象模型是否有特定原因?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM