【问题标题】:Programmatically create SharePoint 2010 content type using XML definition file使用 XML 定义文件以编程方式创建 SharePoint 2010 内容类型
【发布时间】:2011-12-28 12:32:26
【问题描述】:

是否有任何方法可以使用 XML 定义文件以编程方式创建 SharePoint 2010 内容类型? SPFields 可以通过以下方式添加:

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

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

【问题讨论】:

    标签: c# xml sharepoint-2010 contenttype


    【解决方案1】:

    您可以以编程方式创建/添加内容类型,但不能使用 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 的回复,我更喜欢使用功能。

    【讨论】:

      【解决方案2】:

      最常见的方法是使用功能定义,然后为您的网站集激活该功能。该功能的 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查看完整示例。

      您尝试使用对象模型是否有特定原因?

      【讨论】:

      • 我想在我的功能的 FeatureUpgrading 事件处理程序中执行此操作,以及其他升级操作(以符合当前项目中使用的做法),而不是使用 ApplyElementManifests 元素。
      猜你喜欢
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 2012-12-23
      相关资源
      最近更新 更多