65.9K
CodeProject 正在变化。 阅读更多。
Home

SharePoint 2007 列表定义功能操作指南

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (2投票s)

2011年3月16日

CPOL

7分钟阅读

viewsIcon

41332

downloadIcon

647

这是一篇关于创建列表定义功能的“操作指南”文章。

必备组件

此项目是在安装了 Visual Studio 2008 Windows SharePoint Services 3.0 扩展 (版本 1.2)WSPBuilder 的 VS 2008 中创建的。要编译和部署,您需要在安装了 MOSS 2007 的机器上打开此项目。

更新

  • 我添加了其他列表类型。现在包括一个文档库和任务示例以及一个列表示例。
  • 我修复了该项目,使其可以在 SharePoint 服务器之外和没有扩展的情况下查看。
  • 使用 SharePoint Solution Generator 创建 schema 文件时出现了一个问题:schema.xml 中包含 ColName="ntext5"ColName="nVarcharn"(其中 n = 字段中的数字)导致了以下错误:
  • “URL ‘FileName’ 无效。它可能指向一个不存在的文件或文件夹,或指向一个不在当前 Web 中的有效文件或文件夹。”

    通过从 schema.xml 中的字段中删除这些属性,此问题得到了修复。我在项目示例中修复了此问题。还将 instance.xml 中的文档库从 List/ExampleList 更改为 Folders/Example List,以便将列表放在文件夹集合中。

背景

在本文中,我将讨论如何为 MOSS 2007 创建自己的列表定义项目,包括文档库列表的定义代码和架构。我还包含了一个功能,用于在激活后创建您的列表,甚至包含了一个全局功能,以防您有多个列表。

Using the Code

在 SharePoint 2007 中构建任何功能都不是一件简单的事情。您必须做得恰到好处才能成功。您的项目层次结构很重要。

ProjectView.jpg

此处不会深入讨论项目的基本层次结构,但可以说它很重要。对于我们的需求,C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\FEATURES 的 HIVE 位置将是我们定位和安装功能的位置。在我们的项目中,此层次结构从 TEMPLATE 开始。因此,我们的功能将位于 TEMPLATE\FEATURES\ 和功能名称中。

对于这个项目,我们有三个功能

  • Example.ListDefinition.ExampleList - 这包含实际的列表定义。
  • Example.ListDefinition.ExampleList.Instance - 这包含的功能将在部署和激活后在我们的网站上创建一个列表。
  • Example.ListDefinition - 由于其他功能为了激活方便而标记为隐藏,因此我们使用此功能来激活其他两个功能。此外,如果您有多个功能(或一个项目的多个列表),您可以使用此功能从激活的角度将它们全部联系起来。由于您的所有其他功能都标记为“隐藏”,这意味着它们不会出现在 UI 的 SharePoint 网站功能列表中,因此只有顶层功能才会出现。由于它们是隐藏的依赖项,因此它们将与顶层或可见功能一起激活或停用。

Example.ListDefinition 功能除了 feature.xml 文件中定义依赖项的部分外,没有什么特别之处。

<ActivationDependencies>
<!-- Requires the Example.ListDefinition.ExampleList.Instance to be installed -->
<ActivationDependency FeatureId="9AE45499-1370-4b23-ACB1-663C142809AD"/>
<!-- Requires the Example.ListDefinition.ExampleList to be installed -->
<ActivationDependency FeatureId="434BA6CB-63A8-425d-8C68-CFCAA45745C3"/>
</ActivationDependencies>

由于其他两个功能都是隐藏的,并且 SharePoint 不支持分层依赖链 (请参阅本文) 以允许多个功能,因此我们必须在顶层功能中将列表定义功能和列表创建功能都标识为依赖项。

实际的列表定义文件存在于 Example.ListDefinition.ExampleList 功能中,其中包括一个架构文件(在功能的 ExampleList 文件夹下),用于修改后的文档库,并添加了一些自定义列。此功能的第一部分是 feature.xml。有功能接收器,但它们除了在停用时清理列表外,没有做太多事情。但要小心,您的内容也会随之消失!

注意:如果您剪切并粘贴此代码以创建新的列表功能,则必须更改列表定义功能的“Id”属性(实际上,您可能应该更改复制功能下的所有 GUID)。复制您创建的新 GUID 并将其放入 Example.ListDefinition feature.xml 中,还将此 GUID 放入 Example.ListDefinition.ExampleList.Instance 功能文件的 instance.xml 中,在 ListDefinition 节点的“FeatureId”属性下。还要更改顶层功能 Example.ListDefinition feature.xml 文件中的激活依赖项,并为新功能添加一个依赖项节点。

<?xml version="1.0" encoding="utf-8"?>
<Feature
  Id="434BA6CB-63A8-425d-8C68-CFCAA45745C3"
  Title="Example.ListDefinition.ExampleList"
  Description="DO NOT mess with this list definition feature to prevent 
      dependancy crashes. Perform all activation deactivation 
      against the TOP LEVEL Example.ListDefinition feature."
  Scope="Web"
  Hidden="true"
  ActivateOnDefault="FALSE" 
  AutoActivateInCentralAdmin="FALSE"
  AlwaysForceInstall="TRUE" 
  ImageUrl="Example_ListDefinition\cl.jpg"
  ReceiverAssembly="Example.ListDefinition, Version=1.0.0.0, 
                       Culture=neutral, PublicKeyToken=9f4da00116c38ec5"
  ReceiverClass="Example.ListDefinition.ExampleListFeatureReceiver"
  xmlns="http://schemas.microsoft.com/sharepoint/">

<ElementManifests> 
<ElementManifest Location="ExampleList/ListDefinition.xml"/> 
</ElementManifests>

</Feature>

请注意 Example.ListDefinition.ExampleList 功能中对 ListDefinition.xml 的元素引用。这是我们的列表定义清单文件。feature.xml 指向它。“ExampleList/ListDefinition.xml”的位置名称很重要,因为当创建实例的功能使用此功能创建实例时,它会在名为“ExampleList”的文件夹中查找。

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ListTemplate 
Name="ExampleList" 
DisplayName="ExampleList" 
Description="" 
BaseType="1" 
Type="101" 
OnQuickLaunch="FALSE" 
SecurityBits="11" 
Sequence="110" 
Image="/_layouts/images/itdl.gif" 
DocumentTemplate="101" />
</Elements>

如上所述,这里需要注意的重要事项是“ExampleList”名称。名称本身不重要,但它必须与 Example.ListDefinition.ExampleList 功能下它所在的文件夹匹配。

注意:如果您剪切并粘贴这些列表功能以创建新的列表定义,则必须更改 Example.ListDefinition.ExampleList 功能下的此文件夹名称,以匹配 ListTemplate 中的“Name”属性。

接下来是 schema.xml 文件和支持文件。这些文件在示例项目中位于 TEMPLATE\FEATURES\Example.ListDefinition.ExampleList\ExampleList 文件夹下。

要为您的项目创建架构文件,您可以使用像 SharePoint Manager 这样的工具,从您通过 SharePoint 网站设置 UI 创建的 SharePoint 列表中获取架构,或者自己编写。

我们示例的架构文件基于文档库。这由架构结构以及“Type”和“BaseType”属性指示。

<List Name="ExampleList" 
Title="ExampleList" 
Direction="0" 
BaseType="1" 
Url="ExampleList" 
DisableAttachments="TRUE" 
Type="101" 
Id="42AB31FD-4935-4c70-9397-78B3E5EEA3A9" 
xmlns="http://schemas.microsoft.com/sharepoint/">
.....

您在默认文档库列之上创建的任何列都位于架构中的不同位置。我提供了一些示例,只是为了让您了解它可能是什么样子。

<Fields>
<Field ....
<Field Type="Text" DisplayName="ExampleListType" Required="TRUE" 
  MaxLength="255" ID="{039b22c9-7967-4e46-8550-ca4d933c19a8}" 
  SourceID="{0ea41a0b-8f0c-4af0-8d9f-57909e889a8a}" StaticName="ExampleListType" 
  Name="ExampleListType" ColName="nvarchar10" RowOrdinal="0" />
<Field Type="Text" DisplayName="ExampleListId" Required="TRUE" 
  MaxLength="255" ID="{B412DE16-5CEC-4be0-BA27-D265C0B0EEC5}" 
  SourceID="{0ea41a0b-8f0c-4af0-8d9f-57909e889a8a}" StaticName="ExampleListId" 
  Name="ExampleListId" ColName="nvarchar10" RowOrdinal="0" />
<Field Type="Text" DisplayName="MemberId" Required="TRUE" 
  MaxLength="255" ID="{0B63375F-A0B1-4fb9-901C-41CD1DBD3E6A}" 
  SourceID="{0ea41a0b-8f0c-4af0-8d9f-57909e889a8a}" StaticName="MemberId" 
  Name="MemberId" ColName="nvarchar10" RowOrdinal="0" />
<Field Type="Text" DisplayName="Status" Required="TRUE" 
  MaxLength="255" ID="{5E724380-F055-46a6-A961-D524C86B2690}" 
  SourceID="{0ea41a0b-8f0c-4af0-8d9f-57909e889a8a}" StaticName="Status" 
  Name="Status" ColName="nvarchar10" RowOrdinal="0" />
<Field Type="Text" DisplayName="Phase" Required="TRUE" 
  MaxLength="255" ID="{F4FAF8DA-7861-4226-B86A-78780DB9AE90}" 
  SourceID="{0ea41a0b-8f0c-4af0-8d9f-57909e889a8a}" 
  StaticName="Phase" Name="Phase" 
  ColName="nvarchar10" RowOrdinal="0" />

创建列表实例的功能是 Example.ListDefinition.ExampleList.Instance。

注意:请注意下面的示例中,我删除了对列表定义功能的依赖项。这是因为我将所有列表功能都隐藏了。我这样做是因为我想在一个顶层功能下放置多个列表,但不想让用户必须激活每个列表。将它们隐藏并使它们都依赖于顶层功能 Example.ListDefinition 即可实现这一点。

<?xml version="1.0" encoding="utf-8"?>
<Feature
Id="9AE45499-1370-4b23-ACB1-663C142809AD"
Title="Example.ListDefinition.ExampleList.Instance"
Description="DO NOT mess with this list definition feature to prevent 
   dependancy crashes. Perform all activation deactivation against 
   the TOP LEVEL Example.ListDefinition feature."
Scope="Web"
Hidden="true"
ActivateOnDefault="FALSE" 
AutoActivateInCentralAdmin="FALSE"
AlwaysForceInstall="TRUE"
ImageUrl="Example_ListDefinition\cl.jpg" 
xmlns="http://schemas.microsoft.com/sharepoint/">
<!--<ActivationDependencies>
--><!-- Requires the ContentType to be installed --><!--
<ActivationDependency FeatureId="434BA6CB-63A8-425d-8C68-CFCAA45745C3"/>
</ActivationDependencies>-->
<ElementManifests> 
<ElementManifest Location="instance.xml"/>
</ElementManifests>
</Feature>

Example.ListDefinition.ExampleList.Instance 文件夹中的 list instance.xml 包含对包含列表定义的功能的引用。它还通过“Url”属性告诉 SharePoint 将列表放在何处。

注意:ListInstance 节点中的“Title”属性应与 Example.ListDefinition.ExampleList 功能下的文件夹名称匹配。

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!--
The following Guid for FeatureId is used as a reference to the list definition, 
and it will be automatically replaced with actual feature id at deployment time.
-->
<ListInstance FeatureId="434BA6CB-63A8-425d-8C68-CFCAA45745C3" 
Title="ExampleList" 
Url="Lists/ExampleList"
RootWebOnly="FALSE" 
DocumentTemplate="101"
Id="101"
TemplateType="101" >
</ListInstance>
</Elements>

我在示例项目中包含了安装脚本,这些脚本是 cmd 文件,一旦您对项目运行 WSPBuilder,它们将获取您的 WSP 文件并将其移动到您喜欢的服务器。有关此内容的更多信息,请参阅示例项目中的 README TO INSTALL.txt

部署 WSP 文件后,转到您要激活功能的网站,然后转到 网站设置/网站功能 部分。您应该会在那里看到该功能。激活该功能。

feature.jpg

您现在可以转到网站内容页面并查看列表。

listdeployed.jpg

关注点

是的……和 SharePoint 2007 一样,找出所有设置以使其正常工作是困难的,但一旦我让它工作,添加一个新列表就是剪切和粘贴。您需要更改功能名称、列表定义文件夹名称、列表 GUID、名称等。如果您将我的示例用作列表定义项目的模板,它可能会为您节省一些时间。总而言之,我在新项目中直接使用它们。总是需要一些调整。但由于这是一篇免费文章……如果它不适合您,请不要抱怨。这就是我们得到报酬的一部分,就是让这些东西工作。否则,祝您编码愉快!

最后一点……如果您想知道 Web 部件代码是做什么用的,我将其用作 MOSS 2007 的一个“技巧”,这样它就不会在部署到服务器时抱怨没有要部署的 Web 级别代码。

© . All rights reserved.