演练:创建 SharePoint 应用程序页
应用程序页是 ASP.NET 页的专用窗体。 应用程序页包含与 SharePoint 母版页合并的内容。 有关详细信息,请参阅为 SharePoint 创建应用程序页。
本演练演示如何创建应用程序页,然后使用本地 SharePoint 站点对其进行调试。 此页显示每位用户在服务器场的所有站点中创建或修改的所有项。
本演练演示以下任务:
- 创建 SharePoint 项目。
- 将应用程序页添加到 SharePoint 项目。
- 将 ASP.NET 控件添加到应用程序页。
- 在 ASP.NET 控件后面添加代码。
- 测试应用程序。
注意
以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。 这些元素取决于你所使用的 Visual Studio 版本和你所使用的设置。 有关详细信息,请参阅个性化设置 Visual Studio IDE。
先决条件
- 支持的 Windows 和 SharePoint 版本。
创建 SharePoint 项目
首先,创建空的 SharePoint 项目。 然后,向此项目添加“应用程序页”项。
启动 Visual Studio。
在“新建项目”对话框中,展开要使用的语言下的“Office/SharePoint”节点,然后选择“SharePoint 解决方案”节点 。
在“Visual Studio 安装的模板”窗格中,选择“SharePoint 2010 - 空项目”模板 。 将项目命名为 MySharePointProject,然后选择“确定”按钮
SharePoint 自定义向导随即出现。 使用此向导可以选择用于调试项目的站点和解决方案的信任级别。
选择“部署为场解决方案”选项按钮,然后选择“完成”按钮,以接受默认本地 SharePoint 站点 。
创建应用程序页
若要创建应用程序页,请向该项目添加“应用程序页”项。
在解决方案资源管理器中,选择 MySharePointProject 项目 。
在菜单栏上,依次选择“项目”>“添加新项”。
在“添加新项”对话框中,选择“应用程序页(仅限场解决方案)”模板 。
将页面命名为 SearchItems,然后选择“添加”按钮 。
Visual Web 开发人员设计器在源视图中显示应用程序页,可在其中查看页面的 HTML 元素。 设计器显示多个 Content 控件的标记。 每个控件映射到默认应用程序母版页中定义的 ContentPlaceHolder 控件。
设计应用程序页的布局
使用“应用程序页”项,可以使用设计器将 ASP.NET 控件添加到应用程序页。 此设计器与 Visual Web 开发人员中使用的设计器相同。 将标签、单选按钮列表和表添加到设计器的源视图中,然后设置属性,就像设计任何标准 ASP.NET 时所操作的一样。
在菜单栏上,依次选择“视图”>“工具箱” 。
在工具箱的标准节点中,执行以下步骤之一:
打开“标签”项的快捷菜单,选择“复制”,打开设计器中 PlaceHolderMain 内容控件下行的快捷菜单,然后选择“粘贴” 。
将“标签”项从“工具箱”拖动到 PlaceHolderMain 内容控件的正文上 。
重复上一步,将“DropDownList”项和“表”项添加到 PlaceHolderMain 内容控件 。
在设计器上,将标签控件的
Text
特性的值更改为“显示所有项”。在设计器上,将
<asp:DropDownList>
元素替换为以下 XML。<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
事件。
在“视图”菜单上,选择“代码” 。
应用程序页代码文件会在代码编辑器中打开。
将以下方法添加到
SearchItems
类。 此代码通过调用在本演练中后面创建的方法来处理 DropDownList 的 SelectedIndexChanged 事件。将以下语句添加到应用程序代码文件的顶部。
将以下方法添加到
SearchItems
类。 此方法会循环访问服务器场上的所有站点,并搜索当前用户创建或修改的项。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); }
将以下方法添加到
SearchItems
类。 此方法显示表中当前用户创建或修改的项。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.NavigateUrl = 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 站点将打开,并会显示应用程序页。
在解决方案资源管理器中,打开应用程序页的快捷菜单,然后选择“设为启动项” 。
选择 F5。
SharePoint 网站随即打开。
在应用程序页上,选择“修改者是我”选项。
应用程序页将刷新并显示在服务器场的所有站点中修改的所有项。
在应用程序页上,选择列表中的“创建者是我”。
应用程序页会刷新并显示在服务器场的所有站点中修改的所有项。
后续步骤
有关 SharePoint 应用程序页的详细信息,请参阅为 SharePoint 创建应用程序页。
可以使用以下主题中的 Visual Web 设计器了解如何设计 SharePoint 页面内容: