チュートリアル: アプリケーション ページの作成

アプリケーション ページとは、特殊なフォームの 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 アプリケーション ライフサイクル管理 (ALM) のエディション

SharePoint プロジェクトの作成

まず、空の SharePoint プロジェクトを作成します。後で、このプロジェクトにアプリケーション ページ項目を追加します。

SharePoint プロジェクトを作成するには

  1. Visual Studio を起動します。

  2. [新しいプロジェクト] のダイアログ ボックスを開き、使用する配置し、を [SharePoint ソリューション] のノードを選択する言語のノードで [Office/SharePoint] のノードを返します。

  3. [Visual Studio にインストールされたテンプレート] のペインで、SharePoint 2010 プロジェクトを空に– テンプレートを選択します。プロジェクトに" MySharePointProject "という名前を付け、をに [OK] のボタンをクリックします。

    SharePoint カスタマイズ ウィザードが表示されます。このウィザードを使用すると、プロジェクトのデバッグに使用するサイトや、ソリューションの信頼レベルを選択できます。

  4. [ファーム ソリューションとして配置する] のオプション ボタンを選択し、既定のローカル SharePoint サイトを受け入れるように [完了] のボタンをクリックします。

アプリケーション ページの作成

アプリケーション ページを作成するには、プロジェクトにアプリケーション ページ項目を追加します。

アプリケーション ページを作成するには

  1. [ソリューション エクスプローラー] では、[MySharePointProject] のプロジェクトを選択します。

  2. メニュー バーで、プロジェクト[新しい項目の追加] を選択します。

  3. [新しい項目の追加] のダイアログ ボックスで、[Application Page (Farm Solution Only] テンプレートを選択します。

  4. ページに" SearchItems "という名前を付け、をに [追加] のボタンをクリックします。

    Visual Web Developer デザイナーでそのページの HTML 要素を確認できます。[ソース] ビューのアプリケーション ページを表示します。デザイナーにはいくつかの Content コントロールのマークアップが表示されます。各コントロールは、既定のアプリケーション マスター ページに定義されている ContentPlaceHolder コントロールにマップされます。

アプリケーション ページのレイアウトのデザイン

アプリケーション ページ項目を使用すると、デザイナーで ASP.NET コントロールをアプリケーション ページに追加できます。このデザイナーは、Visual Web Developer で使用するデザイナーと同じです。標準的な ASP.NET ページをデザインする場合と同様に、デザイナーと、設定されたプロパティの [ソース] の概要にラベル、オプション ボタン リスト、およびテーブルを追加します。

Visual Web Developer のデザイナーの使用方法については、「Web コンテンツのマップの Visual Studio 11 の Beta」を参照してください。

アプリケーション ページのレイアウトをデザインするには

  1. メニュー バーで、[ビュー][ツールボックス] を選択します。

  2. [ツールボックス] の標準的なノードでは、次の手順のいずれかを実行します: 1

    • [ラベル] の項目に対するショートカット メニューを開き、[コピー] を選択してデザイナー [PlaceHolderMain] のコンテンツ コントロールの下に行のショートカット メニューを開き、を [貼り付け] を選択します。

    • [PlaceHolderMain] のコンテンツ コントロールの本体に [ツールボックス] から [ラベル] に項目をドラッグします。

  3. [PlaceHolderMain] のコンテンツ コントロールに [DropDownList] の項目と [テーブル] の項目を追加するには、前の手順を繰り返します。

  4. デザイナーで、ラベル コントロールの Text 属性の値を「Show All Items」に変更します。

  5. デザイナーで、<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 イベントを処理します。

ページ上のコントロールのイベントを処理するには

  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. アプリケーション ページで、[Modified by me] のオプションを選択します。

    アプリケーション ページでは、サーバー ファーム上のすべてのサイトで変更したすべての項目が更新され、表示されます。

  4. アプリケーション ページで、一覧の [現在のユーザーが作成] を選択します。

    アプリケーション ページが更新され、サーバー ファーム上のすべてのサイトで自分が作成したすべての項目が表示されます。

次の手順

SharePoint のアプリケーション ページの詳細については、「SharePoint のアプリケーション ページの作成」を参照してください。

Visual Web Designer を使用して、SharePoint ページの内容をデザインする方法の詳細については、以下のトピックを参照してください。

参照

処理手順

方法: アプリケーション ページを作成する

その他の技術情報

アプリケーションの_layouts ページの種類