次の方法で共有


サブサイトをトップ リンク バーまたはサイド リンク バーのメニューに追加する

最終更新日: 2010年11月1日

適用対象: SharePoint Foundation 2010

Microsoft SharePoint Foundation のユーザー インターフェイスを使用してサブサイトを作成するときに、新しいサイトへのリンクを親サイトのサイド リンク バー領域とトップ リンク バーに表示するかどうかを指定できます。また、親 Web サイトの SPWeb.Navigation.TopNavigationBar プロパティとその SPWeb.Navigation.QuickLaunch プロパティを操作するコードを記述して、両方の場所にリンクを追加することもできます。

トップ リンク バーへのリンクの挿入

SharePoint Foundation オブジェクト モデルでは、トップ リンク バーのナビゲーション構成は、SPNavigationNodeCollection クラスのインスタンスによって表されます。コレクションを取得するには、最初に Web サイトの SPWeb.Navigation プロパティにアクセスします。このプロパティは SPNavigation オブジェクトを返します。次に、そのオブジェクトの TopNavigationBar プロパティにアクセスします。

注意注意

Web サイトが親サイトのトップ リンク バーを使用するように構成されている場合、TopNavigationBar プロパティは null を返します。

ナビゲーション ノードのコレクションを取得した後で、新しいリンクを表す SPNavigationNode オブジェクトを作成し、SPNavigationNodeCollection クラスのメソッドを呼び出して、トップ リンク バーにノードを追加します。

親サイトのトップ リンク バーにサブサイトへのリンクを追加するには

  1. サブサイトを表す SPWeb オブジェクトへの参照を取得します。

    SPWeb child = site.OpenWeb("parent/child");
    
    Dim child As SPWeb = site.OpenWeb("parent/child")
    
  2. IsRootWeb プロパティの値をテストして、そのサブサイトが本当のサブサイトであることを確認します。また、親サイトの UseShared プロパティの値をテストして、親サイトが共有トップ リンク バーを使用していないことを確認します。

    if (!child.IsRootWeb && !child.ParentWeb.Navigation.UseShared)
    
    If Not child.IsRootWeb AndAlso Not child.ParentWeb.Navigation.UseShared Then
    
  3. 親サイトのトップ リンク バーを表す SPNavigationNodeCollection オブジェクトを取得します。

    SPNavigationNodeCollection topnav = child.ParentWeb.Navigation.TopNavigationBar;
    
    Dim topnav As SPNavigationNodeCollection = child.ParentWeb.Navigation.TopNavigationBar
    
  4. コレクションにクエリを実行して、サブサイトの SPWeb.ServerRelativeUrl プロパティと同じ値を持つ Url プロパティが設定されている SPNavigationNode オブジェクトを検索し、サブサイトへの既存のリンクがあるかどうかを確認します。

    SPNavigationNode node = topnav
        .Cast<SPNavigationNode>()
        .FirstOrDefault(n => n.Url.Equals(child.ServerRelativeUrl));
    
    Dim node As SPNavigationNode = topnav.Cast(Of SPNavigationNode)().FirstOrDefault( _
        Function(n) n.Url.Equals(child.ServerRelativeUrl))
    
  5. リンクがない場合は、リンクを表す SPNavigationNode オブジェクトを作成し、SPNavigationNodeCollection クラスからメソッドを呼び出して、そのリンクを親サイトのトップ リンク バーに追加します。

    if (node == null)
    {
        node = new SPNavigationNode(child.Title, child.ServerRelativeUrl);
        node = topnav.AddAsLast(node);
    }
    
    If node Is Nothing Then
    
        node = New SPNavigationNode(child.Title, child.ServerRelativeUrl)
        node = topnav.AddAsLast(node)
    
    End If
    

次の例では、親サイトのナビゲーション構成でサブサイトへのリンクを追加する方法を示します。この例は、リンクを追加するために Web を対象範囲とするフィーチャーを使用している大きいプロジェクトの一部を抜粋したものです。フィーチャーは、SPFeatureReceiver クラスを実装するイベント ハンドラーを含んでおり、リンクを作成し、そのリンクを親サイトに追加するためのコードは、フィーチャー レシーバーの FeatureActivated メソッドにあります。

フィーチャーがアクティブ化されているサイトにサブサイトがある場合、このメソッドは "ourwiki" という名前のサブサイトを探します。このサブサイトが存在する場合、メソッドはそのサブサイトへの参照を取得します。サブサイトがまったくない場合、またはサブサイトは存在するが、"ourwiki" という名前のサブサイトは存在しない場合、メソッドはそのサブサイトを作成します。

次に、メソッドは、サブサイトへのナビゲーション リンクを追加します。現在のサイトに独自のトップ リンク バーがある (つまり、その親のバーを使用していない) 場合、リンクは現在のサイトのトップ リンク バーに追加されます。それ以外の場合は、現在のサイトのサイド リンク バーのメニューに追加されます。

サイド リンク バーとトップ リンク バーは両方とも SPNavigationNodeCollection オブジェクトによって表されます。このため、この時点では、ナビゲーション リンクの追加先のコレクションがどれでも、コードは同じです。この例では、最初に、サブサイトへのリンクがナビゲーション ノードのコレクションに既に存在するかどうかを確認します。存在しない場合、コードによってリンクを作成し、追加します。

注意

次のコード例では、修飾を持たない複数の型を使用しています。コードが正しくコンパイルされるようにするには、フィーチャー レシーバー クラスで次の名前空間をインポートする必要があります。

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{

    // Get the website where the feature is activated.
    SPWeb parentWeb = properties.Feature.Parent as SPWeb;
    if (parentWeb == null)
        return;

    SPWeb childWeb = null;
    string childName = "ourwiki";

    // If a subsite by that name exists, open it.
    string[] webs = parentWeb.Webs.Names;
    if (webs != null && Array.IndexOf(webs, childName) >= 0)
    {
        childWeb = parentWeb.Webs[childName];
    }

    // Otherwise, create the subsite.
    if (childWeb == null)
    {
        string title = "Wiki";
        string desc = "A place to capture knowledge.";
        uint lcid = parentWeb.Language;

        string templateName = "WIKI#0";

        childWeb = parentWeb.Webs.Add(childName, title, desc, lcid, templateName, false, false);
    }

    // Let the subsite use the parent site's top link bar.
    childWeb.Navigation.UseShared = true;

    // Get a collection of navigation nodes.
    SPNavigationNodeCollection nodes = null;
    if (parentWeb.Navigation.UseShared)
    {

        // Parent site does not have its own top link bar
        // so use the parent's Quick Launch.
        nodes = parentWeb.Navigation.QuickLaunch;
    }
    else
    {

        // Parent site has its own top link bar,
        // so use it.
        nodes = parentWeb.Navigation.TopNavigationBar;
    }

    // Check for an existing link to the subsite.
    SPNavigationNode node = nodes
        .Cast<SPNavigationNode>()
        .FirstOrDefault(n => n.Url.Equals(childWeb.ServerRelativeUrl));

    // No link, so add one.
    if (node == null)
    { 

        // Create the node.
        node = new SPNavigationNode(childWeb.Title, childWeb.ServerRelativeUrl);

        // Add it to the collection.
        node = nodes.AddAsLast(node);
    }

    childWeb.Dispose();
    parentWeb.Dispose();
}
Public Overrides Sub FeatureActivated(ByVal properties As SPFeatureReceiverProperties)

    'Get the website where the feature is activated.
    Dim parentWeb As SPWeb = TryCast(properties.Feature.Parent, SPWeb)
    If parentWeb Is Nothing Then
        Return
    End If

    Dim childWeb As SPWeb = Nothing
    Dim childName As String = "ourwiki"

    ' If a subsite by that name exists, open it.
    Dim webs As String() = parentWeb.Webs.Names
    If webs IsNot Nothing AndAlso Array.IndexOf(webs, childName) >= 0 Then
        childWeb = parentWeb.Webs(childName)
    End If

    ' Otherwise, create the subsite.
    If childWeb Is Nothing Then
        Dim title As String = "Wiki"
        Dim desc As String = "A place to capture knowledge."
        Dim lcid As UInteger = parentWeb.Language

        Dim templateName As String = "WIKI#0"

        childWeb = parentWeb.Webs.Add(childName, title, desc, lcid, _
                                        templateName, False, False)
    End If

    ' Let the subsite use the parent site's top link bar.
    childWeb.Navigation.UseShared = True

    ' Get a collection of navigation nodes.
    Dim nodes As SPNavigationNodeCollection = Nothing

    If parentWeb.Navigation.UseShared Then

        ' Parent site does not have its own top link bar
        ' so use the parent's Quick Launch.
        nodes = parentWeb.Navigation.QuickLaunch
    Else

        ' Parent site has its own top link bar,
        ' so use it.
        nodes = parentWeb.Navigation.TopNavigationBar
    End If

    ' Check for an existing link to the subsite.
    Dim node As SPNavigationNode = nodes.Cast(Of SPNavigationNode)().FirstOrDefault( _
        Function(n) n.Url.Equals(childWeb.ServerRelativeUrl))

    ' No link, so add one.
    If node Is Nothing Then

        ' Create the node.
        node = New SPNavigationNode(childWeb.Title, childWeb.ServerRelativeUrl)

        ' Add it to the collection.
        node = nodes.AddAsLast(node)
    End If

    childWeb.Dispose()
    parentWeb.Dispose()
End Sub

サイド リンク バーのメニューへのリンクの挿入

SharePoint Foundation オブジェクト モデルでは、サイド リンク バーのメニューの各ナビゲーション リンクは、SPNavigationNode クラスのインスタンスによって表されます。サイド リンク バーのメニュー構造の最上位レベルは、SPNavigationNodeCollection クラスのインスタンスによって表される SPNavigationNode オブジェクトのコレクションです。メニューのこの第 1 レベルにあるノードは、[リスト]、[ライブラリ]、[サイト] などの見出しです。各見出しには、その見出しの下にあるリンクを表すノードのコレクションを返す SPNavigationNode.Children プロパティがあります。

[サイト] 見出しの下にリンクを挿入するには、AddToQuickLaunch メソッドを呼び出して、[サイト] 見出しを指定します。このメソッドは、2 つの引数を受け入れます。それは、新しいリンクを表す SPNavigationNode オブジェクトと、そのリンクを受け取る見出しを指定する SPQuickLaunchHeading 列挙値です。

SPNavigation nav = web.ParentWeb.Navigation;
SPNavigationNode node = new SPNavigationNode(web.Title, web.ServerRelativeUrl);
node = nav.AddToQuickLaunch(node, SPQuickLaunchHeading.Sites);
Dim nav As SPNavigation = web.ParentWeb.Navigation
Dim node As SPNavigationNode = New SPNavigationNode(web.Title, web.ServerRelativeUrl)
node = nav.AddToQuickLaunch(node, SPQuickLaunchHeading.Sites)

サブサイトへのリンクが [サイト] 見出しの下に存在しないことを確認できます。確認する方法の 1 つを次の手順で説明します。

親サイトのサイド リンク バーにサブサイトへのリンクを追加するには

  1. サブサイトを表す SPWeb オブジェクトへの参照を取得します。

    SPWeb child = site.OpenWeb("parent/child");
    
    Dim child As SPWeb = site.OpenWeb("parent/child")
    
  2. サブサイトが本当のサブサイトであり、コレクションのルート Web サイトでないことを確認します。

    if (!child.IsRootWeb)
    
    If Not child.IsRootWeb Then
    
  3. 親 Web サイトの SPNavigation オブジェクトを取得します。

    SPNavigation nav = web.ParentWeb.Navigation;
    
    Dim nav As SPNavigation = web.ParentWeb.Navigation
    
  4. GetNodeById メソッドを呼び出し、SPQuickLaunchHeading.Sites に相当する整数を渡して、サイド リンク バーのメニューにある [サイト] 見出しを取得します。

    SPNavigationNode sitesHeading = nav.GetNodeById((int)SPQuickLaunchHeading.Sites);
    
    Dim sitesHeading As SPNavigationNode = nav.GetNodeById(CInt(SPQuickLaunchHeading.Sites))
    

    注意

    サイド リンク バーのメニューに [サイト] 見出しがない場合は、GetNodeById を呼び出すと null が返されます。

  5. [サイト] 見出しの子にクエリを実行して、サブサイトの SPWeb.ServerRelativeUrl プロパティと同じ値を持つ Url プロパティが設定されている SPNavigationNode オブジェクトを検索し、サブサイトへの既存のリンクがあるかどうかを確認します。

    SPNavigationNode newNode = null;
    if (sitesHeading != null)
    {
        newNode = sitesHeading
            .Children
            .Cast<SPNavigationNode>()
            .FirstOrDefault(n => n.Url == web.ServerRelativeUrl);
    }
    
    Dim newNode As SPNavigationNode = Nothing
    If sitesHeading IsNot Nothing Then
        newNode = sitesHeading.Children.Cast(Of SPNavigationNode)().FirstOrDefault( _
            Function(n) n.Url = web.ServerRelativeUrl)
    End If
    
  6. リンクがない場合は、リンクを表す SPNavigationNode オブジェクトを作成し、AddToQuickLaunch メソッドを呼び出して、そのリンクを親サイトのサイド リンク バーのメニューにある [サイト] 見出しの下に追加します。

    if (newNode == null)
    {
    
        // Create the link.
        newNode = new SPNavigationNode(web.Title, web.ServerRelativeUrl);
    
        // Add it to the parent site's Quick Launch.
        newNode = nav.AddToQuickLaunch(newNode, SPQuickLaunchHeading.Sites);
    }
    
    If newNode Is Nothing Then
    
        ' Create the link.
        newNode = New SPNavigationNode(web.Title, web.ServerRelativeUrl)
    
        ' Add it to the parent site's Quick Launch.
        newNode = nav.AddToQuickLaunch(newNode, SPQuickLaunchHeading.Sites)
    End If
    

次の例は、親サイトのサイド リンク バーのメニューにサブサイトへのリンクを挿入する手順を示しているコンソール アプリケーションです。サブサイトを作成するフィーチャー レシーバーの FeatureActivated メソッドに、(適切に編集した) 同様のコードを追加できます。

using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {

                // Get the subsite.
                using (SPWeb web = site.OpenWeb("parent/child"))
                {
                    if (!web.IsRootWeb)
                    { 

                        // Get the parent site's navigator.
                        SPNavigation nav = web.ParentWeb.Navigation;

                        // Get the Sites heading.
                        SPNavigationNode sitesHeading = nav.GetNodeById((int)SPQuickLaunchHeading.Sites);

                        // Check if a link to the child already exists.
                        SPNavigationNode newNode = null;
                        if (sitesHeading != null)
                        {
                            newNode = sitesHeading
                                .Children
                                .Cast<SPNavigationNode>()
                                .FirstOrDefault(n => n.Url == web.ServerRelativeUrl);
                        }

                        // No link, so create one.
                        if (newNode == null)
                        {

                            // Create the link.
                            newNode = new SPNavigationNode(web.Title, web.ServerRelativeUrl);

                            // Add it to the parent site's Quick Launch.
                            newNode = nav.AddToQuickLaunch(newNode, SPQuickLaunchHeading.Sites);
                        }

                        Console.WriteLine("A link to {0} is on the Quick Launch for {1}", newNode.Title, nav.Web.Title);
                    }
                }
            }
            Console.Write("\nPress ENTER to continue....");
            Console.ReadLine();
        }
    }
}
Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Navigation

Module ConsoleApp

    Sub Main()

        Using site As New SPSite("https://localhost")

            Using web As SPWeb = site.OpenWeb("parent/child")

                If Not web.IsRootWeb Then

                    ' Get the parent site's navigator.
                    Dim nav As SPNavigation = web.ParentWeb.Navigation

                    ' Get the Sites heading.
                    Dim sitesHeading As SPNavigationNode = nav.GetNodeById(CInt(SPQuickLaunchHeading.Sites))

                    ' Check if a link to the child already exists.
                    Dim newNode As SPNavigationNode = Nothing
                    If sitesHeading IsNot Nothing Then

                        newNode = sitesHeading.Children.Cast(Of SPNavigationNode)().FirstOrDefault( _
                            Function(n) n.Url = web.ServerRelativeUrl)
                    End If

                    ' No link, so create one.
                    If newNode Is Nothing Then

                        ' Create the link.
                        newNode = New SPNavigationNode(web.Title, web.ServerRelativeUrl)

                        ' Add it to the parent site's Quick Launch.
                        newNode = nav.AddToQuickLaunch(newNode, SPQuickLaunchHeading.Sites)
                    End If

                    Console.WriteLine("A link to {0} is on the Quick Launch for {1}", newNode.Title, nav.Web.Title)
                End If

            End Using

        End Using

        Console.Write(vbCrLf & "Press ENTER to continue....")
        Console.Read()
    End Sub

End Module

関連項目

タスク

[方法] サイド リンク バーの表示をカスタマイズする

[方法] サイド リンク バーを表示または非表示にする

概念

リンクをトップ リンク バーに追加する

[方法] サイト間でトップ リンク バーを共有する