当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 添加顶部导航链接

作者:精品下载站 日期:2024-12-14 14:57:34 浏览:12 分类:玩电脑

SharePoint Online:使用 PowerShell 添加顶部导航链接


要求:使用 PowerShell 添加指向 SharePoint Online 顶部导航的链接。

如何在 SharePoint Online 的顶部导航栏中添加链接?

创建或自定义 SharePoint Online 网站时,您可能需要添加到顶部导航的链接。此博文将向您展示如何在 SharePoint Online 的顶部导航栏中添加链接。我们还将向您展示如何使用 PowerShell 在 SharePoint 网站的顶部导航栏中添加链接。

在现代团队和通信网站上,它提供了一个简单的界面来添加到顶部菜单的链接。若要将新链接添加到 SharePoint Online 中的顶部导航,请按照以下步骤操作:

  1. 导航到 SharePoint Online 现代网站,然后单击菜单末尾的“编辑”链接。

    [玩转系统] SharePoint Online:使用 PowerShell 添加顶部导航链接

  2. 将鼠标悬停在编辑导航窗格上,直到看到 + 号出现。
  3. 单击+号添加新链接。
  4. 输入 URL 并在相应字段中显示新链接的名称。

    [玩转系统] SharePoint Online:使用 PowerShell 添加顶部导航链接

  5. 单击“保存”将新链接添加到顶部导航栏。

就是这样!新链接现在应显示在 SharePoint Online 新式网站的顶部导航栏中。

添加指向经典网站顶部导航的链接

  1. 以管理员或具有完全控制权的用户身份登录您的 SharePoint Online 网站。
  2. 单击“设置”齿轮>>“站点设置”
  3. 单击“外观和感觉”组下的“顶部链接栏”链接
  4. 现在,您可以通过输入新链接的名称和 URL 将新链接添加到顶部导航栏。

    [玩转系统] SharePoint Online:使用 PowerShell 添加顶部导航链接

如果您的网站模板是发布网站(或启用了发布功能的网站!),您将在网站设置下获得“导航”链接。您可以在“全局导航”下添加链接。

如何在 SharePoint Online 中添加顶部导航栏?
在现代团队网站中,您可以将左侧导航菜单移至顶部导航!将 SharePoint Online 中的快速启动切换为顶部导航

PowerShell 将链接添加到 SharePoint Online 中的顶部导航:

下面是用于添加 SharePoint Online 网站全局导航链接的 PowerShell。


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Add new Link in Top Navigation
Function Add-SPOTopNavigationLink()
{
    Param(
        [String]$SiteURL,
        [parameter(Mandatory=$false)][String]$ParentNodeTitle,
        [String]$Title,
        [String]$URL
    )

    #Setup Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the Top Navigation of the web
    $TopNavigationBar = $Ctx.Web.Navigation.TopNavigationBar
    $Ctx.load($TopNavigationBar)
    $Ctx.ExecuteQuery()

    #Populate New node data
    $NavigationNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation 
    $NavigationNode.Title = $Title
    $NavigationNode.Url = $URL
    $NavigationNode.AsLastNode = $true

    #Get the Parent Node
    $ParentNode = $TopNavigationBar | Where-Object {$_.Title -eq $ParentNodeTitle}
    
    #Add New node to the navigation
    If($ParentNode -eq $null)
    {
        #Check if the Link with Title exists already
        $Node = $TopNavigationBar | Where-Object {$_.Title -eq $Title}
        If($Node -eq $Null)
        { 
            #Add Link to Root node of the Navigation
            $Ctx.Load($TopNavigationBar.Add($NavigationNode))
            $Ctx.ExecuteQuery()
            Write-Host -f Green "New Link '$Title' Added to the Navigation Root!"
        }
        Else
        {
            Write-Host -f Yellow "Navigation Link '$Title' Already Exists in Root!"
        }
    }
    else
    {
        #Get the Parent Node
        $Ctx.Load($ParentNode)
        $Ctx.Load($ParentNode.Children)
        $Ctx.ExecuteQuery()
 
        #Check if the Link with given title exists
        $Node = $ParentNode.Children | Where-Object {$_.Title -eq $Title}
        If($Node -eq $Null)
        { 
            #Add Link to Parent Node
            $Ctx.Load($ParentNode.Children.Add($NavigationNode))
            $Ctx.ExecuteQuery()
            Write-Host -f Green "New Navigation Link '$Title' Added to the Parent '$ParentNodeTitle'!"
        }
        Else
        {
            Write-Host -f Yellow "Navigation Link '$Title' Already Exists in Parnet Node '$ParentNodeTitle'!"
        }
    }
}
 
#Config Parameters
$SiteURL="https://Crescent.sharepoint.com/unitedstates"

#Call the function to Add a New Link in Root of the navigation
Add-SPOTopNavigationLink -SiteURL $SiteURL -Title "Support Center" -URL "https://support.crescent.com"

#Call the function to Add a link in "Support Center" node of the navigation
Add-SPOTopNavigationLink -SiteURL $SiteURL -ParentNodeTitle "Support Center" -Title "Application Support" -URL "https://suppport.crescent.com/apps"

脚本的结果:

[玩转系统] SharePoint Online:使用 PowerShell 添加顶部导航链接

SharePoint Online:PnP PowerShell 在全局导航菜单栏中添加链接

使用 PnP PowerShell 中的 Add-PnPNavigationNode cmdlet 将链接添加到 SharePoint Online 中的顶部导航或快速启动:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred
    
    #Add a Link to Top Navigation Bar
    Add-PnPNavigationNode -Title "Support Center" -Url "https://support.crescent.com" -Location TopNavigationBar

    #Get the Navigation node "Support Center"
    $ParentID = Get-PnPNavigationNode -Location TopNavigationBar | Where {$_.Title -eq "Support Center"}  | Select -ExpandProperty ID
    #Add a link under "Support Center
    Add-PnPNavigationNode -Title "Application Support" -Url "https://support.crescent.com/apps" -Location TopNavigationBar -Parent $ParentID
 
    Write-host "Quick Launch Links Added Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯