逐步解說:建立應用程式頁面
「應用程式頁面」(Application Page) 是 ASP.NET 網頁的一種特殊形式,應用程式頁面與 SharePoint 主版頁面合併的內容。如需詳細資訊,請參閱建立 SharePoint 的應用程式頁面。
使用本機 SharePoint 網站,本逐步解說將示範如何建立應用程式頁面然後進行偵錯。這個頁面表示所有項目,每個使用者在伺服器陣列上的所有網站建立或修改。
這個逐步解說將說明下列工作:
建立 SharePoint 專案。
將應用程式頁面加入至 SharePoint 專案。
將 ASP.NET 控制項加入至應用程式頁面。
加入 ASP.NET 控制項的程式碼。
測試應用程式頁面。
![]() |
---|
在下列指示的某些 Visual Studio 使用者介面項目中,您的電腦可能會顯示不同的名稱或位置:您所擁有的 Visual Studio 版本和使用的設定決定了這些項目。如需詳細資訊,請參閱Visual Studio 設定。 |
必要條件
您需要下列元件才能完成此逐步解說:
支援的 Windows 和 SharePoint 版本。如需詳細資訊,請參閱開發 SharePoint 方案的要求。
Visual Studio Professional 或某個 Visual Studio Application Lifecycle Management (ALM) 版本。
建立 SharePoint 專案
首先,請建立 [空的 SharePoint 專案]。稍後,您會將 [應用程式頁面] 項目加入至這個專案。
若要建立 SharePoint 專案
啟動 Visual Studio。
開啟 [新的專案] 對話方塊中,展開 [Office SharePoint/] 節點在您想要使用之語言下,然後選取 [SharePoint 方案] 節點。
在 [Visual Studio 安裝範本] 窗格中,選取 [SharePoint 2010 –空專案] 範本。將專案命名為 MySharePointProject,然後選擇 [OK] 按鈕。
[SharePoint 自訂精靈] 隨即出現。這個精靈可讓您選取用來偵錯專案的網站以及方案的信任層級。
選取 [部署為陣列方案] 選項按鈕,然後選擇 [結束] 按鈕以接受預設的本機 SharePoint 網站。
建立應用程式頁面
若要建立應用程式頁面,請將 [應用程式頁面] 項目加入至專案。
若要建立應用程式頁面
在 [方案總管] 中,選取 [MySharePointProject] 專案。
在功能表列上,選擇 [專案]],則 [加入新項目。]。
在 [加入新項目。] 對話方塊中,選取 [應用程式頁面 (僅限陣列方案] 範本。
將網頁 SearchItems,然後選擇 [加入] 按鈕。
Visual Web Developer 設計工具會顯示您可以在其中檢視網頁的 HTML 項目的 [來源] 檢視中的應用程式頁面。設計工具會顯示多個 Content 控制項的標記。每一個控制項都會對應至預設應用程式主版頁面中定義的 ContentPlaceHolder 控制項。
設計應用程式頁面的配置
[應用程式頁面] 項目可讓您使用設計工具,將 ASP.NET 控制項加入至應用程式頁面。此設計工具與您在 Visual Web Developer 中使用的設計工具相同。將標籤、一份選項按鈕清單和資料表到設計工具中設定屬性的 [來源] 檢視就像,當您設計任何標準 ASP.NET 網頁時。
如需如何在 Visual Web Developer 中使用設計工具的詳細資訊,請參閱Visual Studio Web 程式開發環境內容對應。
若要設計應用程式頁面的配置
在功能表列上,選擇 [檢視]],則 [工具箱]。
在 [工具箱] 的標準節點,請執行下列其中一個步驟:
開啟 [標籤] 項目的捷徑功能表,選擇 [複製]],開啟行的捷徑功能表在設計工具中的 [PlaceHolderMain] 內容控制下,然後選取 [貼上]。
將 [工具箱] 從 [標籤] 拖曳項目在 [PlaceHolderMain] 內容控制項的主體中。
重複以上步驟來加入 [DropDownList] 項目和 [表] 項目加入至 [PlaceHolderMain] 內容控制項。
在設計工具上,將 [Label] 控制項的 Text 屬性值變更為 [顯示所有項目]。
在設計工具上,以下列 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 事件。
若要處理頁面上的控制項事件
在 [檢視] 功能表上,選擇 [程式碼]。
應用程式頁面程式碼檔案隨即在 [程式碼編輯器] 中開啟。
將下列方法加入至 SearchItems 類別。這個程式碼會呼叫您稍後將在本逐步解說中建立的方法,以處理 DropDownList 的 SelectedIndexChanged 事件。
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); }
在應用程式頁面程式碼檔案上方加入下列陳述式。
Imports System.Web.UI.WebControls Imports Microsoft.SharePoint.Administration Imports System.Collections
using System.Web.UI.WebControls; using Microsoft.SharePoint.Administration; using System.Collections;
將下列方法加入至 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); }
將下列方法加入至 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 網站會開啟,然後顯示應用程式頁面。
若要測試應用程式頁面
在 [方案總管] 中,開啟應用程式頁面的捷徑功能表,然後選擇 [做為起始的項目集合。]。
選擇 F5 鍵。
SharePoint 網站隨即開啟。
在應用程式頁面,請選取 [修改由我] 選項。
應用程式頁面隨即重新整理並顯示您在伺服器陣列上的所有網站任何已變更的項目。
在應用程式頁面,請選取清單中的 [建立由我] 。
應用程式頁面隨即重新整理,並顯示您在伺服器陣列上全部的網站中建立的所有項目。
後續步驟
如需 SharePoint 應用程式頁面的詳細資訊,請參閱建立 SharePoint 的應用程式頁面。
您可以透過下列主題,進一步了解如何使用 Visual Web 設計工具來設計 SharePoint 頁面內容: