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

[玩转系统] SharePoint Online:PowerShell 通过 URL 获取列表

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

SharePoint Online:PowerShell 通过 URL 获取列表


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

SharePoint Online:PowerShell 通过 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"

#Function to Get List from URL
Function Get-ListByUrl([Microsoft.SharePoint.Client.ClientContext]$Ctx, $ListServerRelativeURL)
{
    #Get the Web from Context
    $Web = $Ctx.Web
    #Get the Root Folder of the List from Relative URL
    $ListFolder = $web.GetFolderByServerRelativeUrl($ListServerRelativeURL)
    $Ctx.Load($ListFolder.Properties)
    $Ctx.ExecuteQuery()
    
    #Get List Name from Root Folder's Properties
    $ListId = [System.guid]::New($ListFolder.Properties["vti_listname"].ToString())
    $List = $Web.Lists.GetById($ListId)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()
    Return $List
}
     
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ListServerRelativeURL = "/sites/marketing/Project Documents"
 
#Setup Credentials to connect
$Cred = Get-Credential

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#call the function to get the List from URL
$List = Get-ListByUrl -Ctx $Ctx -ListServerRelativeURL $ListServerRelativeURL
Write-host $List.title

PnP PowerShell 从其 URL 获取列表

PnP PowerShell 的处理方式有点不同!我们只需要列表或库的站点相对 URL。


#Connect to Site
Connect-PnPOnline -Url "https://crescent.sharepoint.com/sites/marketing" -Interactive

#Get a List from site relative URL
$List = Get-PnPList -Identity "Lists/Projects"
Write-host $List.ItemCount

#Get a Document Library from its Site Relative URL
$Library = Get-PnPList -Identity "Branding"
Write-host $Library.ItemCount

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

取消回复欢迎 发表评论:

关灯