演练:创建应用程序页

“应用程序页”是 ASP.NET 页的专用形式。应用程序页包含与 SharePoint 母版页合并的内容。有关更多信息,请参见为 SharePoint 创建应用程序页

通过使用本地 SharePoint 网站,本演练演示如何创建应用程序页再对其进行调试。此页显示所有项,每个用户在服务器场中的所有网站中创建或修改。

本演练阐释了以下任务:

  • 创建 SharePoint 项目。

  • 向该 SharePoint 项目添加应用程序页。

  • 向该应用程序页添加 ASP.NET 控件。

  • 在 ASP.NET 控件后添加代码。

  • 测试应用程序页。

说明说明

以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。您安装的 Visual Studio 版本以及使用的设置决定了这些元素。有关更多信息,请参见Visual Studio 设置

系统必备

您需要以下组件来完成本演练:

  • 支持的 Windows 和 SharePoint 版本。有关更多信息,请参见开发 SharePoint 解决方案的要求

  • Visual Studio 专业版或 Visual Studio Application Lifecycle Management (ALM) 的某个版本。

创建 SharePoint 项目

首先,创建一个**“空 SharePoint 项目”。然后,向该项目添加“应用程序页”**项。

创建 SharePoint 项目

  1. 启动 Visual Studio。

  2. 打开 新建项目 对话框中,展开节点 Office/SharePoint 在要使用的语言下的,然后选择 SharePoint 解决方案 节点。

  3. Visual Studio 已安装的模板 窗格中,选择 SharePoint 2010 -空项目 模板。将项目命名为" MySharePointProject ",然后选择 确定 按钮。

    这将显示**“SharePoint 自定义向导”**。使用此向导可以选择用于调试项目的站点以及解决方案的信任级别。

  4. 选择 部署为场解决方案 选项按钮,然后选择 完成 按钮接受默认的本地 SharePoint 网站。

创建应用程序页

若要创建应用程序页,请向该项目添加**“应用程序页”**项。

创建应用程序页

  1. 解决方案资源管理器,选择 MySharePointProject 项目。

  2. 在菜单栏上,选择 项目添加新项

  3. 添加新项 对话框中,选择 应用程序页 (仅场解决方案 模板。

  4. 将该页命名为" SearchItems ",然后选择 添加 按钮。

    visual web 开发供应商设计器将显示在查看该页的 HTML 元素的 视图的应用程序页。该设计器显示多个 Content 控件的标记。每个控件均映射到在默认应用程序母版页中定义的 ContentPlaceHolder 控件。

设计应用程序页的布局

通过“应用程序页”项,您可以使用设计器将 ASP.NET 控件添加到应用程序页中。此设计器即是在 Visual Web Developer 中使用的设计器。添加一个标签,单选按钮列表和表与 的视图设计器,然后设置属性可以象,当您设计任何标准 ASP.NET 页时。

有关使用 Visual Web Developer 中的设计器的更多信息,请参见Visual Studio 11 beta 为 web 内容映射

设计应用程序页的布局

  1. 在菜单栏上,依次选择 查看工具箱

  2. 工具箱的标准节点,请执行以下步骤之一:

    • 打开 标签 项目的快捷菜单,选择 复制,打开行的快捷菜单在设计器中 PlaceHolderMain 内容控件下的,然后选择 粘贴

    • 从拖到 工具箱标签 项。PlaceHolderMain 内容控件的主体上。

  3. 重复上面的步骤添加 下拉列表 项目和 项。PlaceHolderMain 内容控件。

  4. 在设计器中,将标签控件的 Text 特性的值更改为“显示所有项”。

  5. 在设计器中,用以下 XML 替换 <asp:DropDownList> 元素。

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"
     OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        <asp:ListItem Text="Created by me" Value="Author"></asp:ListItem>
        <asp:ListItem Text="Modified by me" Value="Editor"></asp:ListItem>
    </asp:DropDownList>
    

处理该页上的控件的事件

按照任何 ASP.NET 页的控件的处理方式,处理应用程序页中的控件。在此过程中,您将处理下拉列表的 SelectedIndexChanged 事件。

处理该页上的控件的事件

  1. 查看 菜单中,选择 代码

    应用程序页代码文件随即在代码编辑器中打开。

  2. 将以下方法添加到 SearchItems 类中。此代码通过调用将在本演练的稍后部分中创建的方法,来处理 DropDownListSelectedIndexChanged 事件。

     Protected Sub DropDownList1_SelectedIndexChanged _
    (ByVal sender As Object, ByVal e As EventArgs) _
    Handles DropDownList1.SelectedIndexChanged
         SPSecurity.RunWithElevatedPrivileges(AddressOf GetItems)
     End Sub
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SPSecurity.RunWithElevatedPrivileges(GetItems);
    
    }
    
  3. 将以下语句添加到应用程序页代码文件的顶部。

    Imports System.Web.UI.WebControls
    Imports Microsoft.SharePoint.Administration
    Imports System.Collections
    
    using System.Web.UI.WebControls;
    using Microsoft.SharePoint.Administration;
    using System.Collections;
    
  4. 将以下方法添加到 SearchItems 类中。此方法循环访问服务器场中的所有网站,并搜索由当前用户创建或修改的项。

    Private Sub GetItems()
        Dim CurrentUser As New SPFieldUserValue _
            (Me.Web, Me.Web.CurrentUser.ID, Me.Web.CurrentUser.Name)
    
        Dim ResultsList As New ArrayList()
    
        Dim ThisFarm As SPFarm = SPFarm.Local
        Dim Service As SPWebService = _
            ThisFarm.Services.GetValue(Of SPWebService)("")
    
        Dim WebApp As SPWebApplication
        For Each WebApp In Service.WebApplications
            Dim SiteCollection As SPSite
            For Each SiteCollection In WebApp.Sites
                Dim Web As SPWeb
                For Each Web In SiteCollection.AllWebs
                    Dim Lists As SPListCollection = Web.Lists
                    Dim List As SPList
                    For Each List In Lists
                        Dim Item As SPListItem
                        Try
                            For Each Item In List.Items
                                If Item(DropDownList1.SelectedValue).ToString() = _
                                    CurrentUser.ToString() Then
                                    ResultsList.Add(Item)
                                End If
                            Next Item
                        Catch exception As Exception
                            'Error in list. Move on to the next list.
                        End Try
                    Next List
                Next Web
            Next SiteCollection
        Next WebApp
        CreateResultsTable(ResultsList)
    End Sub
    
    private void GetItems()
    {
         SPFieldUserValue currentUser = new SPFieldUserValue
         (this.Web, this.Web.CurrentUser.ID, this.Web.CurrentUser.Name);
    
        ArrayList resultsList = new ArrayList();
    
        SPFarm thisFarm = SPFarm.Local;
        SPWebService service = thisFarm.Services.GetValue<SPWebService>("");
    
        foreach (SPWebApplication webApp in service.WebApplications)
        {
            foreach (SPSite siteCollection in webApp.Sites)
            {
                foreach (SPWeb web in siteCollection.AllWebs)
                {
                    SPListCollection lists = web.Lists;
                    foreach (SPList list in lists)
                    {
                        try
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                if (item[DropDownList1.SelectedValue].ToString() ==
                                    currentUser.ToString())
                                {
                                    resultsList.Add(item);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            // An error with the list. Move onto the next list.
                        }
                    }
                }
            }
        }
        CreateResultsTable(resultsList);
    
    }
    
  5. 将以下方法添加到 SearchItems 类中。此方法显示表中由当前用户创建或修改的项。

    Private Sub CreateResultsTable(ByVal ResultsList As ArrayList)
        Dim CurrentList As String = ""
        Dim CurrentSite As String = ""
        Table1.Rows.Clear()
    
        Dim Item As SPListItem
        For Each Item In ResultsList
            If Item.ParentList.ParentWeb.Title <> CurrentSite Then
                CurrentSite = Item.ParentList.ParentWeb.Title
    
                Dim NewSiteCell As New TableCell()
                NewSiteCell.Text = CurrentSite
    
                Dim NewSiteRow As New TableRow()
                With NewSiteRow
                    .Cells.Add(NewSiteCell)
                    .Font.Bold = True
                    .Font.Size = FontUnit.Larger
                    .Font.Underline = True
                End With
                Table1.Rows.Add(NewSiteRow)
    
            End If
    
            If Item.ParentList.Title <> CurrentList Then
                CurrentList = Item.ParentList.Title
                Dim NewListCell As New TableCell()
                NewListCell.Text = CurrentList
    
                Dim NewListRow As New TableRow()
                With NewListRow
                    .Cells.Add(NewListCell)
                    .Font.Bold = True
                End With
                Table1.Rows.Add(NewListRow)
    
                Dim ItemHeading As New TableCell()
                With ItemHeading
                    .Text = "Item"
                    .Font.Italic = True
                End With
    
                Dim CreatedHeading As New TableCell()
                With CreatedHeading
                    .Text = "Created"
                    .Font.Italic = True
                End With
    
    
                Dim ModifiedHeading As New TableCell()
                With ModifiedHeading
                    .Text = "Last Modified"
                    .Font.Italic = True
                End With
    
                Dim HeadingRow As New TableRow()
                HeadingRow.Cells.Add(ItemHeading)
                HeadingRow.Cells.Add(CreatedHeading)
                HeadingRow.Cells.Add(ModifiedHeading)
    
                Table1.Rows.Add(HeadingRow)
            End If
    
            Dim ItemName As New TableCell()
            Dim ItemLink As New HyperLink()
            Try
                ItemLink.href = Item.ParentList.ParentWeb.Url & _
                    "/" & Item.ParentList.Forms(PAGETYPE.PAGE_DISPLAYFORM).Url & _
                    "?ID=" & Item.ID
            Catch exception As Exception
                ' Some items might not have a form page. Ignore the exception.
            End Try
            ItemLink.Text = Item.DisplayName
            ItemName.Controls.Add(ItemLink)
    
            Dim Created As New TableCell()
            Created.Text = Item("Created").ToString()
    
            Dim Modified As New TableCell()
            Modified.Text = Item("Modified").ToString()
    
            Dim DataRow As New TableRow()
            DataRow.Cells.Add(ItemName)
            DataRow.Cells.Add(Created)
            DataRow.Cells.Add(Modified)
    
            Table1.Rows.Add(DataRow)
        Next Item
    End Sub
    
    private void CreateResultsTable(ArrayList resultsList)
    {
        string currentList = "";
        string currentSite = "";
        Table1.Rows.Clear();
    
        foreach (SPListItem item in resultsList)
        {
            if (item.ParentList.ParentWeb.Title != currentSite)
            {
                currentSite = item.ParentList.ParentWeb.Title;
    
                TableCell newSiteCell = new TableCell();
                newSiteCell.Text = currentSite;
    
                TableRow newSiteRow = new TableRow();
                newSiteRow.Cells.Add(newSiteCell);
                newSiteRow.Font.Bold = true;
                newSiteRow.Font.Size = FontUnit.Larger;
                newSiteRow.Font.Underline = true;
    
                Table1.Rows.Add(newSiteRow);
            }
            if (item.ParentList.Title != currentList)
            {
                currentList = item.ParentList.Title;
                TableCell newListCell = new TableCell();
                newListCell.Text = currentList;
    
                TableRow newListRow = new TableRow();
                newListRow.Cells.Add(newListCell);
                newListRow.Font.Bold = true;
    
                Table1.Rows.Add(newListRow);
    
                TableCell itemHeading = new TableCell();
                itemHeading.Text = "Item";
                itemHeading.Font.Italic = true;
    
                TableCell createdHeading = new TableCell();
                createdHeading.Text = "Created";
                createdHeading.Font.Italic = true;
    
                TableCell modifiedHeading = new TableCell();
                modifiedHeading.Text = "Last Modified";
                modifiedHeading.Font.Italic = true;
    
                TableRow headingRow = new TableRow();
                headingRow.Cells.Add(itemHeading);
                headingRow.Cells.Add(createdHeading);
                headingRow.Cells.Add(modifiedHeading);
    
                Table1.Rows.Add(headingRow);
            }
    
            TableCell itemName = new TableCell();
            HyperLink itemLink = new HyperLink();
            try
            {
                itemLink.href = item.ParentList.ParentWeb.Url + "/" +
                    item.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url +
                    "?ID=" + item.ID;
            }
            catch (Exception)
            {
                // Some items might not have a form page. Ignore the exception.
            }
            itemLink.Text = item.DisplayName;
            itemName.Controls.Add(itemLink);
    
            TableCell created = new TableCell();
            created.Text = item["Created"].ToString();
    
            TableCell modified = new TableCell();
            modified.Text = item["Modified"].ToString();
    
            TableRow dataRow = new TableRow();
            dataRow.Cells.Add(itemName);
            dataRow.Cells.Add(created);
            dataRow.Cells.Add(modified);
    
            Table1.Rows.Add(dataRow);
        }
    }
    

测试应用程序页

在运行项目时,SharePoint 网站将打开,并显示应用程序页。

测试应用程序页

  1. 解决方案资源管理器,请打开应用程序页的快捷菜单,然后选择 设置为启动项

  2. 选择 F5 键。

    SharePoint 网站将打开。

  3. 在应用程序页,选择 修改由我 选项。

    此时将刷新该应用程序页在服务器场中的所有网站修改的所有项。

  4. 在应用程序页,请在列表中选择 由我创建

    此时将刷新该应用程序页,并显示您在服务器场中的所有网站中创建的所有项。

后续步骤

有关 SharePoint 应用程序页的更多信息,请参见为 SharePoint 创建应用程序页

通过使用以下主题中介绍的 Visual Web Designer,可以了解有关如何设计 SharePoint 页内容的更多内容:

请参见

任务

如何:创建应用程序页

其他资源

应用程序_layouts 页类型