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

[玩转系统] SharePoint Online:用于获取列表 URL 的 PowerShell

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

SharePoint Online:用于获取列表 URL 的 PowerShell


要求: PowerShell 获取 SharePoint Online 列表 URL。

SharePoint Online PowerShell 获取列表 URL

有时您可能需要获取 SharePoint Online 列表或文档库的 URL。本博文将向您展示如何使用 PowerShell 获取 SharePoint Online 中列表的 URL。

以下是如何使用 PowerShell 在 SharePoint Online 中获取列表 URL:


#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"

#Variables for Processing
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$ListName = "Documents"

#Get 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 Web and List Objects
$Web = $Ctx.Web
$Ctx.Load($Web)
$List = $Web.Lists.GetByTitle($ListName)
$Ctx.Load($List.RootFolder)
$Ctx.ExecuteQuery()

#List Relative URL
Write-host "List Relative URL:"$List.RootFolder.ServerRelativeUrl

#Get List Full URL
If($Ctx.Web.ServerRelativeUrl -eq "/")
{
    $ListURL = $("{0}{1}" -f $Ctx.Web.Url, $List.RootFolder.ServerRelativeUrl)
}
Else
{
    $ListURL = $("{0}{1}" -f $Ctx.Web.Url.Replace($Ctx.Web.ServerRelativeUrl,''), $List.RootFolder.ServerRelativeUrl)
}
Write-host "List Full URL:"$ListURL

PnP PowerShell 获取 SharePoint Online 中列表的绝对 URL

我们可以使用 PnP PowerShell 获取 SharePoint Online 中列表或库的完整 URL,如下所示:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/pmo"
$ListName= "Projects"
  
#Connect to SharePoint Online site
Connect-PnPOnline $SiteURL -Interactive

Try {
    #Get the List
    $List = Get-PnPList $ListName -Includes ParentWeb -ThrowExceptionIfListNotFound -ErrorAction Stop
    
    #Get the absolute URL of the List
    If($List.ParentWeb.ServerRelativeUrl -eq "/")
    {
        $ListURL = [String]::Concat($List.ParentWeb.Url,$List.DefaultViewUrl)
    }
    Else
    {
        $ListURL = [String]::Concat( $List.ParentWeb.Url,$List.DefaultViewUrl.Replace($List.ParentWeb.ServerRelativeUrl,[string]::Empty))
    }
    
    Write-host $ListURL
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

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

取消回复欢迎 发表评论:

关灯