【问题标题】:Update lists when changing custom list template更改自定义列表模板时更新列表
【发布时间】:2011-01-10 10:53:55
【问题描述】:

我在 Visual Studio 到 MOSS 中创建了一个功能,一个发布站点 - 此功能包含一些自定义列表模板和一些使用模板定义的列表。现在我需要更新列表模板,这不是问题,因为它只是在我的 schema.xml 中添加了几行,但我还需要一种方法来反映现有列表的更新。

据我所知,此功能不是标准的 Sharepoint,但我如何以编程方式解决此问题,例如是否在我的 OnActivated 中循环遍历我的列表并根据列表的模板更新(删除/添加)字段?

【问题讨论】:

    标签: sharepoint sharepoint-2007 publishing wspbuilder


    【解决方案1】:

    是的,当您更新列表架构时,它不会反映在已创建的列表中。为此,请在您的架构中添加一个 FeatureActivated 事件处理程序。每当您激活功能时,此事件处理程序都会运行代码。

    在您的功能中创建一个 XML 配置文件,其中将包含已创建的列表名称。然后代码将读取 XML 文件并更新您已经创建的列表。

    为了可扩展性和灵活性,请注意此代码需要尽可能具有防御性。例如,当您在未来某个时间再次激活该功能时,它不应再次进行更改,从而导致更改丢失或重复。它应该首先检查,然后才进行更改。

    相同的方案可用于内容类型。如果需要,我可以为您发布代码 sn-p。

     public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                // Fix the Article Date column
                if (properties != null)
                {
                    FixArticleDate(properties);
                }
    
                // Fix Metadata Tagging site columns by setting CustomField "MetadataType" to the Default value set in the field definition manifest file.
                if (properties != null && properties.Feature.Properties["FixMetadataTagging"] != null)
                {
                    RepairMetadataTaggingSiteColumns(properties);
                }
    
                // Fix Lookup site columns by retrieving lookup list GUID from List="url". 
                if (properties != null && properties.Feature.Properties["FixListTagging"] != null)
                {
                    RepairListTaggingSiteColumns(properties);
                }
    
                // Fixing Site Columns
                if (properties != null && properties.Feature.Properties["FixSiteColumns"] != null)
                {
                    RepairSiteColumns(properties);
                }
            }
            catch (SPException sharepointEx)
            {
                ExceptionManager.LogError(ULSTracerCategoriesEnum.FeatureReceivers, sharepointEx);
            }
        }
    

    XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <Feature Id="A23990CF-C35D-4771-BF5A-916C304C9EF9"
       Title="Content Types"
       Description="This Feature Creates all the Required Content Types and site columns"
       Version="1.0.0.0" Scope="Site" Hidden="FALSE"
       ReceiverAssembly="xxxx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=86597c5d57921943"
       ReceiverClass="xxxx.SharePoint.UI.Core.FeatureReceivers.CoreFeatureReceiver"        
       xmlns="http://schemas.microsoft.com/sharepoint/">
      <ElementManifests>
        <ElementManifest Location="SiteColumns\SiteColumns.xml" />
        <ElementManifest Location="ContentTypes\ContentTypes.xml" />
      </ElementManifests>
      <Properties>
        <Property Key="FixMetadataTagging" Value="SiteColumns\MetadataTaggingSiteColumnsManifest.xml"/>
        <Property Key="FixListTagging" Value="SiteColumns\ListTaggingSiteColumnsManifest.xml"/>
        <Property Key="FixSiteColumns" Value="ContentTypeFixes\SiteColumnAdditions.xml"/>
      </Properties>
    </Feature>
    

    【讨论】:

    • 使用给定的代码,我想我在修复方法中必须找到缺失的字段并使用 MySPList.Fields.Add 添加它们?
    • 是的,基本上,随便你。将数据放在 xml 中,就像您的字段名称一样,并在代码中读取并执行操作。确保它的防御性意味着,确保你不会通过一次又一次地激活功能而一次又一次地添加相同的字段
    • 也许有很多问题,但是您是否可以提供一些额外的线索,因为我尝试的任何事情都不是成功的。如果我查看现有列表的字段,所有字段都包括在内 - 如果我查看列表的内容类型,我可以确定新字段丢失,但我找不到添加缺失字段的方法?
    • 抱歉,我的回复迟了 - 我还有一些其他任务需要解决。与此同时,我找到了解决方案;通过构建我的自定义字段加上我的内容类型“独立”并将内容类型关联到我的自定义列表(而不是仅将字段添加到我的自定义列表),对内容类型的更改现在反映到从自定义列表创建的列表中任何代码。非常感谢您的意见:)
    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多