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

[玩转系统] SharePoint Online:如何使用 PowerShell 获取列表?

作者:精品下载站 日期:2024-12-14 21:03:28 浏览:16 分类:玩电脑

SharePoint Online:如何使用 PowerShell 获取列表?


要求:SharePoint Online PowerShell 以获取列表或文档库。

SharePoint Online:从 PowerShell 获取列表

如果您正在管理 SharePoint Online,管理列表是您工作的常见要求。在这篇博文中,我们将了解如何使用 PowerShell 从 SharePoint Online 网站获取列表。我们还将向您展示如何从列表中获取属性。

以下是获取 SharePoint Online 列表的 PowerShell:


#sharepoint online get list powershell
Import-Module Microsoft.Online.SharePoint.Powershell -DisableNameChecking

#Config Variables for Site URL, List Name
$SiteURL= "https://crescent.sharepoint.com/sites/sales"
$ListName="Sales Contacts"
 
#Setup Credentials to connect
$Cred = Get-Credential
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #powershell sharepoint online get list
    $List = $Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    Write-host "Total Number of Items in the List:"$List.ItemCount
}
 Catch [Exception] {
      Write-host $_.Exception.Message -f Red
} 

此 PowerShell 按标题或名称从 SharePoint Online 获取列表。

用于获取 SharePoint Online 中的列表的 PnP PowerShell

让我们看看如何使用 PnP PowerShell cmdlet Get-PnPList 获取 SharePoint Online 列表:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/PMO"
$ListName = "Projects"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get a list from Sharepoint Online using PnP PowerShell
    $List = Get-PnPList -Identity $ListName

    #Get Number of Items in the List
    Write-host $List.ItemCount
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

该脚本获取给定的列表以及列表中的项目数。若要使用 PowerShell 获取 SharePoint Online 网站中的所有列表,请使用:SharePoint Online PowerShell 获取所有列表

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

取消回复欢迎 发表评论:

关灯