【问题标题】:Sharepoint ListTemplate Multiple Content Types - Ref FieldsSharepoint ListTemplate 多种内容类型 - 参考字段
【发布时间】:2009-10-17 22:01:59
【问题描述】:

我正在尝试使用 CAML 在 SharePoint 功能中创建列表模板。我有两种内容类型 “新闻”和“新闻发布”,它们都共享两个名为Overview和说明。

我一直在阅读“listtemplate”caml 元素不会自动添加来自内容类型的字段,您需要指定所有字段。指定字段时,SharePoint 不会更新共享点列表设置 (screenshot) 中的“已使用”。 这是一个问题,因为无法使用这些字段更新视图。

可以用 c# 编写的特征接收器来解决这个问题吗?

有什么想法吗?

<?xml version="1.0" encoding="utf-8" ?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListTemplate Name="News"
                DisplayName="News"
                Description="News"
                Type="100"
                BaseType="0"
                OnQuickLaunch="true"
                SecurityBits="11"
                Sequence="410"
                Image="/_layouts/images/itgen.gif"
                Unique="True"
                DisableAttachments="True" />
</Elements>

<?xml version="1.0" encoding="utf-8" ?>
<List Name="News"
      Title="News"
      FolderCreation="FALSE"
      Direction="$Resources:Direction;"
      Url="Lists/News"
      EnableContentTypes="True"
      BaseType="0" 
      Type="100"
      xmlns="http://schemas.microsoft.com/sharepoint/"
      xmlns:ows="Microsoft SharePoint">
  <MetaData>
    <ContentTypes>
      <ContentTypeRef ID="0x010007196C9EB6E5B04BAE108FD1969FD42B01" />
      <ContentTypeRef ID="0x010007196C9EB6E5B04BAE108FD1969FD42B02" />
    </ContentTypes>
    <Fields>
      <Field ID="{1E061768-0380-48e4-8E71-86CAE6DDDF30}" Type="Note" DisplayName="Overview" Name="Overviews" />
      <Field ID="{9406510E-511A-438f-AD9F-A55CED16B033}" Type="Note" DisplayName="Description" StaticName="Description" Name="Description" />
    </Fields>
    <View>
      Removed For Post
    </View>          
    <Forms>
      <Form Type="DisplayForm" Url="DispForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="EditForm" Url="EditForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
      <Form Type="NewForm" Url="NewForm.aspx" SetupPath="pages\form.aspx" WebPartZoneID="Main" />
    </Forms>
  </MetaData>
</List>

【问题讨论】:

    标签: c# xml sharepoint wss caml


    【解决方案1】:

    这是让 CAML 为您进行初始化的问题之一,如果可能的话,我总是会在代码中做事 :)

    前几天我遇到了类似的问题,最终从列表中删除了 ContentTypeRef 部分,让它仅从项目和功能接收器中继承,创建列表,添加我想要的内容类型,然后删除项目内容类型.这样,列表就可以正确填充来自内容类型的字段

    以下是我的特征接收器:

    public void CreateList(SPFeatureReceiverProperties prop)
            {
                logger.Info("Creating list");
                using (SPWeb currentWeb = WebHelper.GetWeb(prop))
                {
                    try
                    {
                        SPList list = ListHelper.CreateList("Other Documents", "", "Rhb Other Documents List", true, currentWeb);
                        logger.Info("List created successfully");
    
                        logger.Info("Attaching content types");
                        list.ContentTypesEnabled = true;
                        list.Update();
                        list.ContentTypes.Add(currentWeb.ContentTypes["RhbOtherDocuments"]);
                        list.Update();
                        list.ContentTypes["Item"].Delete();
                        list.Update();
                        logger.Info("Content type attached");
                    }
                    catch (Exception e)
                    {
                        logger.Error("List creation failed", e);
                        Console.WriteLine(e);
                    }
                }
            }
    

    特征接收器中使用的网络助手类:

    public class WebHelper
        {
            #region Helper functions
            public static SPWeb GetWeb(SPFeatureReceiverProperties prop)
            {
                using (SPSite site = prop.Feature.Parent as SPSite)
                {
                    return site != null ? site.RootWeb : prop.Feature.Parent as SPWeb;
                }
            }
            #endregion  
        }
    

    特征接收器中使用的列表助手类:

    public static SPList CreateList(string ListName, string description, string templateName, bool Visible,
                                            SPWeb web)
            {
                SPListTemplate template = web.Site.RootWeb.ListTemplates[templateName];
                SPList list = web.Lists[web.Lists.Add(ListName, description, template)];
                list.EnableVersioning = false;
                list.EnableAttachments = false;
                list.OnQuickLaunch = false;
                list.EnableFolderCreation = false;
                list.Update();
                return list;
            }
    

    需要注意的一点是,这种机制可能比通过 CAML 更好,因为这样我们就不必同时在内容类型和列表 CAML 中维护字段信息

    我最初发现一篇文章 (see here) 提供了我的解决方案的很大一部分。

    【讨论】:

      猜你喜欢
      • 2019-11-18
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 2010-09-08
      • 2010-11-20
      • 1970-01-01
      • 2014-06-08
      • 2012-08-13
      相关资源
      最近更新 更多