向快速启动中添加链接

上次修改时间: 2015年3月9日

适用范围: SharePoint Foundation 2010

本文内容
导航菜单结构
在内置标题下添加项目
在菜单上指定位置
添加自定义标题

在 Microsoft SharePoint Foundation 网站上,"快速启动"是大多数内容页左侧的静态菜单。SharePoint Foundation 对象模型将"快速启动"菜单表示为 SPNavigationNode 对象的层次结构,其中每个对象均表示该菜单上的一个超文本链接。

向菜单中添加新链接就是构造表示链接的对象,然后将它添加到层次结构中的适当位置。

导航菜单结构

"快速启动"菜单的顶层是标题。您可以通过以下方式获得标题的集合:首先获取网站的 Navigation 属性返回的对象,然后获取该对象的 QuickLaunch 属性。QuickLaunch 属性返回 SPNavigationNodeCollection 对象,其中包含表示标题的 SPNavigationNode 对象集。

SPNavigationNodeCollection ql = web.Navigation.QuickLaunch;
Dim ql As SPNavigationNodeCollection = web.Navigation.QuickLaunch

下一层是标题下的项目。表示标题的每个 SPNavigationNode 对象都具有 Children 属性,该属性可以包含表示标题下的项目的节点集合。如果标题下没有任何项目,则标题节点的 Children 属性将返回空集合。

foreach (SPNavigationNode heading in web.Navigation.QuickLaunch)
{
    foreach (SPNavigationNode item in heading.Children)
    {

        // Do something.
    }
}
For Each heading As SPNavigationNode In web.Navigation.QuickLaunch
    For Each item As SPNavigationNode In heading.Children

        ' Do something.
    Next
Next

标题及其下的每个项目是表示超文本链接的 SPNavigationNode 对象。每个链接的显示文本封装在对象的 Title 属性中,链接的 URL 位于对象的 Url 属性中。这些属性的值独立于链接的目标。如果链接指向网站,则在网站的标题或 URL 更改时,导航节点中的标题和 URL 不会自动进行同样的更改。

默认情况下,"快速启动"菜单配置为仅显示两层。您可以自定义"快速启动",以便其他层次可以显示在静态菜单或弹出菜单上。有关详细信息,请参阅如何:自定义快速启动显示

在内置标题下添加项目

若要在某一标准"快速启动"标题(例如"列表"或"库")下插入链接,请调用 AddToQuickLaunch 方法。此方法接受两个参数:表示新链接的 SPNavigationNode 对象和指定应接收链接的标题的 SPQuickLaunchHeading 枚举值。

AddToQuickLaunch 方法位于 SPNavigation 类中,SPWeb 类的 Navigation 属性返回该类的实例。

在内置标题下添加项目

  1. (可选)通过调用网站的 Navigation 属性返回的对象的 GetNodeByUrl 方法,检查是否有指向相同目标的现有链接。

    如果网站的导航结构没有包括指向 URL 的链接,GetNodeByUrl 方法将返回 null。

  2. 通过调用 SPNavigationNode 类的构造函数之一,创建表示链接的节点。

    该类具有两个构造函数:

    • 第一个构造函数接受两个参数:包含链接的显示文本的字符串以及包含链接应解析的 URL 的另一个字符串。仅对内部链接(指向相同网站集中的内容的链接)使用此构造函数。URL 应相对于服务器。

    • 第二个构造函数接受三个参数。前两个参数与另一个构造函数的参数相同。第三个参数是指示链接是外部链接还是内部链接的 boolean 值。可为指向外部网站的链接传递 true,并在第二个参数中传递绝对 URL。如果链接是内部链接,请传递 false 和相对于服务器的 URL。

    备注

    仅在将 SPNavigationNode 对象添加到集合中后它才会完全初始化。如果该对象还不是集合的成员,只读属性(例如 Id 属性和 ParentId 属性)将返回 null。

  3. 调用 AddToQuickLaunch 方法,以便将新节点作为第一个参数传递,将 SPQuickLaunchHeading 枚举值作为第二个参数传递。

    AddToQuickLaunch 方法返回 SPNavigationNode 类的初始化实例。您可以使用此对象来获取和设置节点的其他属性。您必须在更改对象的属性后调用 Update 方法。

备注

向"快速启动"菜单中添加现有列表或文档库的最快方法是获取表示该列表或库的 SPList 对象,然后将 OnQuickLaunch 属性设置为 true。列表或库的项目将自动添加到相应标题之下。有关"快速启动"标题的信息,请参阅 SPQuickLaunchHeading 枚举。如果"快速启动"菜单上尚未显示标题,则会添加标题。

示例

以下控制台应用程序在"快速启动"中的"列表"标题下添加一个项目。

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

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    // Get the Links list or create it if it does not exist.
                    SPList list = web.Lists.TryGetList("Links");

                    if (list == null || list.BaseTemplate != SPListTemplateType.Links)
                    {
                        // Create the list.
                        Guid listId = web.Lists.Add("Links", "Interesting hyperlinks", SPListTemplateType.Links);
                        list = web.Lists.GetList(listId, false);
                    }

                    // Check for an existing link to the list.
                    SPNavigationNode listNode = web.Navigation.GetNodeByUrl(list.DefaultViewUrl);
 
                    // No link, so create one.
                    if (listNode == null)
                    {
                        // Create the node.
                        listNode = new SPNavigationNode(list.Title, list.DefaultViewUrl);

                        // Add it to Quick Launch.
                        listNode = web.Navigation.AddToQuickLaunch(listNode, SPQuickLaunchHeading.Lists);
                    }
                }
            }
            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()

                ' Get the Links list or create it if it does not exist.
                Dim list As SPList = web.Lists.TryGetList("Links")

                If list Is Nothing OrElse list.BaseTemplate <> SPListTemplateType.Links Then

                    ' Create the list.
                    Dim listId As Guid = web.Lists.Add("Links", "Interesting hyperlinks", SPListTemplateType.Links)
                    list = web.Lists.GetList(listId, False)
                End If

                ' Check for an existing link to the list.
                Dim listNode As SPNavigationNode = web.Navigation.GetNodeByUrl(list.DefaultViewUrl)

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

                    ' Create the node.
                    listNode = New SPNavigationNode(list.Title, list.DefaultViewUrl)

                    ' Add it to Quick Launch.
                    listNode = web.Navigation.AddToQuickLaunch(listNode, SPQuickLaunchHeading.Lists)
                End If

            End Using

        End Using

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

End Module

在菜单上指定位置

上一节中介绍的 AddToQuickLaunch 方法将新链接添加为"快速启动"菜单上指定标题下的最后一项。您可以通过使用 SPNavigationNodeCollection 类中的多种方法之一来更加精细地控制位置。下表简单介绍了这些方法。

方法

说明

Add

将新节点添加到集合中的指定节点之后。

AddAsFirst

将新节点添加为集合中的第一个节点。

AddAsLast

将新节点添加为集合中的最后一个节点。

以下过程说明如何使用 Add 方法将链接插入到紧接"快速启动"菜单上的指定项之后。

将链接插入到菜单上的现有项之后

  1. 通过调用 SPNavigation 类的 GetNodeById 方法,获取要在其下放置链接的标题的节点。

    // Get the Libraries heading.
    SPNavigationNode heading = web.Navigation.GetNodeById((int)SPQuickLaunchHeading.Documents);
    
    ' Get the Libraries heading.
    Dim heading As SPNavigationNode = web.Navigation.GetNodeById(CInt(SPQuickLaunchHeading.Documents))
    
  2. 通过查询标题节点的 Children 属性返回的集合,获取要在其后放置链接的项目的节点。

    // If a node for Shared Documents exists, the new node will go after it.
    SPList sharedDocs = web.Lists.TryGetList("Shared Documents");
    SPNavigationNode sharedDocsNode = null;
    if (sharedDocs != null)
        sharedDocsNode = librariesHeading
            .Children
            .Cast<SPNavigationNode>()
            .FirstOrDefault(n => n.Url == sharedDocs.DefaultViewUrl);
    
    ' If a node for Shared Documents exists, the new node will go after it.
    Dim sharedDocs As SPList = web.Lists.TryGetList("Shared Documents")
    Dim sharedDocsNode As SPNavigationNode = Nothing
    If sharedDocs IsNot Nothing Then
        sharedDocsNode = librariesHeading.Children.Cast(Of SPNavigationNode)().FirstOrDefault( _
            Function(n) n.Url = sharedDocs.DefaultViewUrl)
    End If
    
  3. 构造表示新链接的 SPNavigationNode 对象。

    SPNavigationNode newNode = new SPNavigationNode(list.Title, list.DefaultViewUrl);
    
    Dim newNode As SPNavigationNode = New SPNavigationNode(list.Title, list.DefaultViewUrl)
    
  4. 通过以下方法添加链接:调用 Add 方法,以及将新节点作为第一个参数传递,将要在其后添加该新节点的节点作为第二个参数传递。

    newNode = heading.Children.Add(newNode, sharedDocsNode);
    
    newNode = heading.Children.Add(newNode, sharedDocsNode)
    

备注

仅在将 SPNavigationNode 对象添加到集合中后它才会完全初始化。如果该对象还不是集合的成员,只读属性(例如 Id 属性和 ParentId 属性)将返回 null。

示例

下面的示例演示如何将链接添加到"快速启动"菜单上的特定位置。此示例是使用 Web 范围的功能创建名为 Meeting Notes 的文档库的较大项目的一部分。功能包括实现 SPFeatureReceiver 类的事件处理程序。在功能接收器的 FeatureActivated 方法中,包含用于创建 Meeting Notes 库并将指向它的链接添加到"库"标题下的代码。如果菜单具有指向 Shared Documents 库的链接,则新链接将插入到紧接它之后。如果菜单没有指向 Shared Documents 的链接,则代码使指向 Meeting Notes 的链接成为"库"标题下的第一项。

备注

此示例代码使用多种未限定的类型。为使此代码正确编译,功能接收器类必须导入以下命名空间:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    // Get the website where the feature is activated.
    SPWeb web = properties.Feature.Parent as SPWeb;
    if (web == null)
        return;

    // Get the Meeting Notes document library.
    SPList meetingNotes = web.Lists.TryGetList("Meeting Notes");
    if (meetingNotes == null)
    {
        // Create the library if it does not exist.
        Guid listId = web.Lists.Add("Meeting Notes", "An archive for meeting notes.", SPListTemplateType.DocumentLibrary);
        meetingNotes = web.Lists.GetList(listId, false);
    }

    // Check for an existing Quick Launch node for Meeting Notes.
    SPNavigationNode meetingNotesNode = web.Navigation.GetNodeByUrl(meetingNotes.DefaultViewUrl);

    // If a Meeting Notes node exists on Quick Launch, nothing more to do.
    if (meetingNotesNode != null)
        return;

    // Still here, so create a node for Meeting Notes.
    meetingNotesNode = new SPNavigationNode(meetingNotes.Title, meetingNotes.DefaultViewUrl);

    // Get the Libraries heading.
    SPNavigationNode librariesHeading = web.Navigation.GetNodeById((int)SPQuickLaunchHeading.Documents);

    // If the Libraries heading does not exist or it exists but has no items below it,
    // then Meeting Notes will be the first item.
    if (librariesHeading == null || librariesHeading.Children.Count == 0)
    {
        web.Navigation.AddToQuickLaunch(meetingNotesNode, SPQuickLaunchHeading.Documents);
        return;
    }

    // The Libraries heading exists. Now check for an item linking to Shared Documents.
    // If a node for Shared Documents exists, Meeting Notes will go after it.
    SPList sharedDocs = web.Lists.TryGetList("Shared Documents");
    SPNavigationNode sharedDocsNode = null;
    if (sharedDocs != null)
        sharedDocsNode = librariesHeading
            .Children
            .Cast<SPNavigationNode>()
            .FirstOrDefault(n => n.Url == sharedDocs.DefaultViewUrl);

    // A node for Shared Documents does not exist. Make Meeting Notes the first item.
    if (sharedDocsNode == null)
    {
        librariesHeading.Children.AddAsFirst(meetingNotesNode);
        return;
    }

    // A node for Shared Documents exists. Place Meeting Notes after it.
    librariesHeading.Children.Add(meetingNotesNode, sharedDocsNode);

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

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

    ' Get the Meeting Notes document library.
    Dim meetingNotes As SPList = web.Lists.TryGetList("Meeting Notes")
    If meetingNotes Is Nothing Then

        ' Create the library if it does not exist.
        Dim listId As Guid = web.Lists.Add("Meeting Notes", "An archive for meeting notes.", SPListTemplateType.DocumentLibrary)
        meetingNotes = web.Lists.GetList(listId, False)

    End If

    ' Check for an existing Quick Launch node for Meeting Notes.
    Dim meetingNotesNode As SPNavigationNode = web.Navigation.GetNodeByUrl(meetingNotes.DefaultViewUrl)

    ' If a Meeting Notes node exists on Quick Launch, nothing more to do.
    If meetingNotesNode IsNot Nothing Then
        Return
    End If

    ' Still here, so create a node for Meeting Notes.
    meetingNotesNode = New SPNavigationNode(meetingNotes.Title, meetingNotes.DefaultViewUrl)

    ' Get the Libraries heading.
    Dim librariesHeading As SPNavigationNode = web.Navigation.GetNodeById(CInt(SPQuickLaunchHeading.Documents))

    ' If the Libraries heading does not exist or it exists but has no items below it,
    ' then Meeting Notes will be the first item.
    If librariesHeading Is Nothing OrElse librariesHeading.Children.Count = 0 Then
        web.Navigation.AddToQuickLaunch(meetingNotesNode, SPQuickLaunchHeading.Documents)
        Return
    End If

    ' The Libraries heading exists. Now check for an item linking to Shared Documents.
    ' If a node for Shared Documents exists, Meeting Notes will go after it.
    Dim sharedDocs As SPList = web.Lists.TryGetList("Shared Documents")
    Dim sharedDocsNode As SPNavigationNode = Nothing
    If sharedDocs IsNot Nothing Then
        sharedDocsNode = librariesHeading.Children.Cast(Of SPNavigationNode)().FirstOrDefault( _
            Function(n) n.Url = sharedDocs.DefaultViewUrl)
    End If

    ' A node for Shared Documents does not exist. Make Meeting Notes the first item.
    If sharedDocsNode Is Nothing Then
        librariesHeading.Children.AddAsFirst(meetingNotesNode)
        Return
    End If

    ' A node for Shared Documents exists. Place Meeting Notes after it.
    librariesHeading.Children.Add(meetingNotesNode, sharedDocsNode)

    web.Dispose()
End Sub

添加自定义标题

"快速启动"菜单上的标题是 SPNavigationNode 对象。所有标题的集合由网站的 Navigation 对象的 QuickLaunch 属性返回。您可以通过以下方式添加自定义标题:构造一个新导航节点,然后通过调用 SPNavigationNodeCollection 类的方法(例如 AddAsLast 方法)将它添加到集合中。

将自定义标题添加到"快速启动"中

  1. 构造一个表示新标题的导航节点的 SPNavigationNode 对象。

    string headingTitle = "Resources";
    string headingUrl = web.Navigation.Home.Url;
    SPNavigationNode heading = new SPNavigationNode(headingTitle, headingUrl);
    
    Dim headingTitle As String = "Resources"
    Dim headingUrl As String = web.Navigation.Home.Url
    Dim heading As SPNavigationNode = New SPNavigationNode(headingTitle, headingUrl)
    
  2. 获取由网站的 SPWeb.Navigation 属性所返回对象的 QuickLaunch 属性所返回导航节点的集合。

    SPNavigationNodeCollection quickLaunch = web.Navigation.QuickLaunch;
    
    Dim quickLaunch As SPNavigationNodeCollection = web.Navigation.QuickLaunch
    
  3. 通过调用 SPNavigationNodeCollection 类的方法将新标题节点添加到集合中。

    heading = quickLaunch.AddAsLast(heading);
    
    heading = quickLaunch.AddAsLast(heading)
    

    该示例调用 AddAsLast 方法。也可使用其他两个方法:AddAsFirst 方法和 Add 方法。

  4. (可选)通过以下方式将项目添加到新标题下:获取新标题的 Children 属性返回的集合,然后再次调用 SPNavigationNodeCollection 类的方法。

    SPNavigationNode item = new SPNavigationNode(itemTitle, itemUrl);
    item = heading.Children.AddAsLast(item);
    
    Dim item As SPNavigationNode = New SPNavigationNode(itemTitle, itemUrl)
    item = heading.Children.AddAsLast(item)
    

示例

以下示例是一个为"资源"创建新的"快速启动"菜单标题并将项目添加到该新标题下的控制台应用程序。

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"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string headingTitle = "Resources";
                    string headingUrl = web.Navigation.Home.Url;
                    string itemTitle = "SharePoint Developer Center";
                    string itemUrl = "https://msdn.microsoft.com/sharepoint";

                    // Get the Quick Launch headings.
                    SPNavigationNodeCollection ql = web.Navigation.QuickLaunch;

                    // If a Resources heading exists, get it.
                    SPNavigationNode heading = ql
                        .Cast<SPNavigationNode>()
                        .FirstOrDefault(n => n.Title == headingTitle);

                    // If the Resources heading does not exist, create it.
                    if (heading == null)
                    {
                        heading = new SPNavigationNode(headingTitle, headingUrl);
                        heading = ql.AddAsLast(heading);
                    }

                    // If the heading has a SharePoint Dev Center item, get it.
                    SPNavigationNode item = heading
                        .Children
                        .Cast<SPNavigationNode>()
                        .FirstOrDefault(n => n.Url == itemUrl);

                    // If the item does not exist, create it.
                    if (item == null)
                    {
                        item = new SPNavigationNode(itemTitle, itemUrl, true);
                        item = heading.Children.AddAsLast(item);
                    }
                 }
            }
            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()

                Dim headingTitle As String = "Resources"
                Dim headingUrl As String = web.Navigation.Home.Url
                Dim itemTitle As String = "SharePoint Developer Center"
                Dim itemUrl As String = "https://msdn.microsoft.com/sharepoint"

                ' Get the Quick Launch headings.
                Dim ql As SPNavigationNodeCollection = web.Navigation.QuickLaunch

                ' If a Resources heading exists, get it.
                Dim heading As SPNavigationNode = ql.Cast(Of SPNavigationNode)().FirstOrDefault( _
                    Function(n) n.Title = headingTitle)

                ' If the Resources heading does not exist, create it.
                If heading Is Nothing Then
                    heading = New SPNavigationNode(headingTitle, headingUrl)
                    heading = ql.AddAsLast(heading)
                End If

                ' If the heading has a SharePoint Dev Center item, get it.
                Dim item As SPNavigationNode = heading.Children.Cast(Of SPNavigationNode)().FirstOrDefault( _
                    Function(n) n.Url = itemUrl)

                ' If the item does not exist, create it.
                If item Is Nothing Then
                    item = New SPNavigationNode(itemTitle, itemUrl, True)
                    item = heading.Children.AddAsLast(item)
                End If

            End Using
        End Using

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

End Module

请参阅

任务

如何:显示或隐藏快速启动

如何:自定义快速启动显示

如何:向顶部链接栏或快速启动菜单添加子网站