SharePoint 2010开发工具图解系列:Visual Studio 2010创建列表

练习 :利用Visual Studio 2010创建列表

在此次练习中,您将利用包括Visual Studio 2010在内的SharePoint开发工具来新建一个SharePoint列表。

  1. 在开始此次练习之前,您可能需要运行位于c:\Student\Labs\03_Lists\文件夹中的SetupLab03.bat批处理文件,来新建一个用于测试和调试您将在此次实验中所编写的代码的网站集。该批处理文件将在http://moss.contoso.com/sites/Lab03位置新建一个网站集。
  2. 启动Internet Explorer并导航到位于http://moss.contoso.com/sites/Lab03的顶级网站。对该网站稍做观察,并确保其符合预期要求。注意,该设置脚本将新建一个位于顶级的新网站集。
  3. 启动Visual Studio 2010,选择文件» 新建项目并使用如下信息,来新建一个空白的SharePoint项目:

    项目类型: 已安装的模板» SharePoint » 2010

    模板: 空白SharePoint项目

    名称: ListsAndSchemas

     

  4. SharePoint 自定义向导中,输入之前步骤创建的网站的URL地址(http://moss.contoso.com/sites/Lab03),来将其作为调试用网站,然后,勾选部署为场解决方案复选框。

     
  5. 首先要为待创建的新列表定义网站栏目和内容类型。要实现该目的,需要向当前项目添加一个新的项目条目。在解决方案资源管理器窗口中,右击ListsAndSchemas项目,并选择添加» 新建项。在SharePoint » 2010模板列表中,选择内容类型,并将其命名为Product。此时,该向导对话框将提示您为新建内容类型指定继承自哪个现有的内容类型。选择项目内容类型。

     
  6. 在创建内容类型之前,首先要创建2个网站栏。在<ContentType>元素的起始位置之前添加如下CAML。注意,您不必使用与如下代码相同的GUID。只要确保一直在使用您自己的GUID即可,因为后面的步骤需要对其进行引用。
    请确保ID=""属性使用大写字母。IntelliSense将试图使用无效的Id=""形式。
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Field SourceID="http://schemas.microsoft.com/sharepoint/v3"
    ID
    ="{36819A9B-E748-47D5-9949-A65DD195BF80}"
    Name
    ="ProductDescription"
    DisplayName
    ="Product Description"
    Group
    ="My Custom Columns"
    Type
    ="Text"
    DisplaceOnUpgrade
    ="TRUE" />
    <Field SourceID="http://schemas.microsoft.com/sharepoint/v3"
    ID
    ="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}"
    Name
    ="ProductID"
    DisplayName
    ="Product ID"
    Group
    ="My Custom Columns"
    Type
    ="Number"
    DisplaceOnUpgrade
    ="TRUE" />
    </Elements>

  7. 在创建网站栏之后,您就可以将这些栏添加到当前的内容类型。为每个由<ContentType>节点包围的2个栏添加一个包含若干<FieldRef>元素的<FieldRefs>元素,来实现将各个栏添加到当前的内容类型,定义完成的内容类型应该如下CAML所示。确保为ProductDescription 和 ProductID使用正确的GUID,以便可以引用到前一步骤所创建的栏目。
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Field SourceID="http://schemas.microsoft.com/sharepoint/v3" />
    <Field SourceID="http://schemas.microsoft.com/sharepoint/v3" />
    <ContentType ID="0x01001586d96f1cb54a19becaf8ee2f193899"
    Name
    ="Product"
    Group
    ="我的自定义内容类型"
    Description
    ="我的Product内容类型"
    Inherits
    ="TRUE"
    Version
    ="0">
    <FieldRefs>
    <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}"
    Name
    ="Title"
    DisplayName
    ="Product Name" />
    <FieldRef ID="{36819A9B-E748-47D5-9949-A65DD195BF80}"
    Name
    ="ProductDescription" />
    <FieldRef ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}"
    Name
    ="ProductID" />
    </FieldRefs>
    </ContentType>
    </Elements>

    完成后如下图所示。 

  8. 现在,就可以利用该内容类型来创建列表模板及其实例。在解决方案资源管理器窗口中,右击ListsAndSchemas项目,选择添加» 新建项,从SharePoint » 2010列表中选取列表定义模板,并将其命名为ProductList。在随后打开的对话框中,将该列表的名称设置为ProductList,并将列表定义的类型是什么设置为自定义列表,保留为此列表定义添加列表实例复选框的勾选状态。

     
  9. 现在,将对列表的定义(或者叫做:模板)及其架构进行修改。储在ListsAndSchemas解决方案的ListsAndSchemas\ProductList\Elements.xml文件之中的列表定义包含列表模板。将其Type属性从10000 更改为 10001,以为其赋予唯一ID值。
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <ListTemplate
    Name="ProductList"
    Type
    ="10001"
    BaseType
    ="0"
    OnQuickLaunch
    ="TRUE"
    SecurityBits
    ="11"
    Sequence
    ="410"
    DisplayName
    ="ProductList"
    Description
    =""
    Image
    ="/_layouts/images/itgen.gif"/>
    </Elements>
  10. 接下来,打开当前项目的ListsAndSchemas\ProductList区域中的schema.xml文件。该文件包含有关列表的所有详细信息,例如列表中的显示字段、内容类型以及视图。
  11. 首先,将EnableContentTypes="TRUE"属性添加到<List>节点。
  12. 接下来,将之前创建的内容类型添加到靠近schema.xml文件顶部的<ContentTypes>节中,如下所示:
    <?xml version="1.0" encoding="utf-8"?>
    <List xmlns:ows="Microsoft SharePoint" Title="ProductList" EnableContentTypes="TRUE" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/ListsAndSchemas-ProductList" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
    <MetaData>
    <ContentTypes>
    <ContentTypeRef ID="0x01">
    <Folder TargetName="Item" />
    </ContentTypeRef>
    <ContentTypeRef ID="0x0120" />
    <ContentTypeRef ID="0x01001586d96f1cb54a19becaf8ee2f193899" />
    </ContentTypes>


    .应确保使用与您之前所创建的内容类型相同的内容类型ID
  13. 现在,将通过向schema.xml文件中的<Fields>节中添加字段的方式,来将这些字段包含在当前列表中。添加当前内容类型中的3个字段,如下所示:
    <?xml version="1.0" encoding="utf-8"?>
    <List xmlns:ows="Microsoft SharePoint" Title="ProductList" EnableContentTypes="TRUE" FolderCreation="FALSE" Direction="$Resources:Direction;" Url="Lists/ListsAndSchemas-ProductList" BaseType="0" xmlns="http://schemas.microsoft.com/sharepoint/">
    <MetaData>
    <ContentTypes></ContentTypes>
    <Fields>
    <Field ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}"
    Name
    ="Title"
    DisplayName
    ="Product Name"
    Type
    ="Text" />
    <Field ID="{36819A9B-E748-47D5-9949-A65DD195BF80}"
    Name
    ="ProductDescription"
    DisplayName
    ="Product Description"
    Type
    ="Text" />
    <Field ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}"
    Name
    ="ProductID"
    DisplayName
    ="ProductID"
    Type
    ="Number" />
    </Fields>
  14. 在向列表添加内容类型和字段之后,还要将这些字段添加到2个默认的列表视图。找到<ViewFields>元素,并添加这些字段。在schema.xml文件中包含2个默认的视图,找到它们。其中,第一个视图如下所示:
    <ViewFields>
    <FieldRef Name="LinkTitleNoMenu"></FieldRef>
    <FieldRef ID="{36819A9B-E748-47D5-9949-A65DD195BF80}"
    Name
    ="ProductDescription"
    DisplayName
    ="Product Description" />
    <FieldRef ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}"
    Name
    ="ProductID"
    DisplayName
    ="ProductID" />
    </ViewFields>
  15. 第二个视图如下所示:
    <ViewFields>
    <FieldRef Name="Attachments"></FieldRef>
    <FieldRef Name="LinkTitle"></FieldRef>
    <FieldRef ID="{36819A9B-E748-47D5-9949-A65DD195BF80}"
    Name
    ="ProductDescription"
    DisplayName
    ="Product Description" />
    <FieldRef ID="{5CD2C0C1-67AC-4F9E-BF21-463CFEE9B382}"
    Name
    ="ProductID"
    DisplayName
    ="ProductID" />
    </ViewFields>

  16. 在完成创建列表模板和定义之后,您需要对列表实例进行修改,以使其不再使用当前模板。首先,将将ProductList\ListInstance1 重命名为 ProductList\Products

  17. 接下来,打开ProductList\Products\Elements.xml文件。按照如下代码更改<ListInstance>元素,包括更改Title、TempateType 和 Url属性:
    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <ListInstance Title="Products"
    OnQuickLaunch
    ="TRUE"
    TemplateType
    ="10001"
    Url
    ="Lists/Products"
    Description
    ="">
    </ListInstance>
    </Elements>
  18. 保存所有更改并按[CTRL]+[F5],来生成和部署当前解决方案,以便对您的工作成果进行测试。最终,Visual Studio将在浏览器中打开该网站,您应该在快速启动区中看到Products列表。

     
  19. 您可以进入网站栏库网站内容类型库,来查看当前Visual Studio 2010项目所创建的其他资源。


     

在此次练习中,您已经使用Visual Studio 2010中全新的SharePoint Tools新建了若干网站栏、一个内容类型、列表模板以及该列表的一个实例。

相关文件下载03_Lists.zip

参考资料:

Sharepoint 2010 Developer Training Kit

 

posted @ 2012-04-08 23:48  Sunmoonfire  阅读(2201)  评论(0编辑  收藏  举报