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

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

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

SharePoint Online:使用 PowerShell 获取列表


要求:使用 PowerShell 在 SharePoint Online 中获取列表。

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

使用 PowerShell 获取 SharePoint Online 列表

如果您正在寻找一种使用 PowerShell 在 SharePoint Online 中获取列表的方法,这篇博文将向您展示如何做到这一点。您可以使用 PowerShell 按名称或 GUID 检索列表。以下是如何使用 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"
    
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
$ListName="Project Tasks"
  
#Get 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)
    
    #get list sharepoint online powershell
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)
    $Ctx.ExecuteQuery()

    Write-host "Total Number of List Items:" $List.ItemCount
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

将变量替换为实际的 SharePoint Online 网站 URL 和列表名称,并确保您已安装 SharePoint Online CSOM SDK 或 SharePoint Online PowerShell 模块。

SharePoint Online 使用 PowerShell 获取列表

同样,您可以从 SharePoint Online 网站获取所有列表,如下所示:


#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"
    
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com/sites/marketing"
  
#Get 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)
    
    #Get All Lists from the Web
    $Lists =$Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    #Iterate through each list
    ForEach($List in $Lists)
    {
        #get all list namses in sharepoint online site using powershell
        Write-host $List.Title
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

此脚本检索站点中的所有列表,包括隐藏列表。

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

要使用 PnP PowerShell 获取列表,您首先需要连接到您的 SharePoint 网站。您可以通过运行以下命令来执行此操作:Connect-PnPOnline -Url https://YourSite.sharepoint.com。连接后,您可以运行 Get-PnPList 命令来获取站点上所有列表的列表。如果要获取特定列表,可以使用 Get-PnPList -Identity 命令。这将返回具有指定名称的列表。

以下是如何使用 PnP PowerShell cmdlet Get-PnPList 在 SharePoint Online 中获取列表:


#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"
$ListName = "Migration Documents"

#Connect to the site
Connect-PnPOnline $SiteURL -Credentials (Get-Credential)

#powershell get sharepoint online list
$List = Get-PnPList -Identity $ListName

Write-host $List.ItemCount

要使用 PnP PowerShell 获取所有列表,请使用:


#Set Parameter
$SiteURL="https://crescent.sharepoint.com/sites/marketing"

#Connect to the site
Connect-PnPOnline -Url $SiteURL -Interactive

#Get all sharepoint online lists
$Lists = Get-PnPList

#Get the Title of All Lists
$Lists | Select Title

使用 PnP PowerShell 获取网站集中的所有列表

遍历网站上的所有列表怎么样?


#Parameters
$SiteURL="https://crescent.sharepoint.com/sites/Marketing"

#Connect to the site
Connect-PnPOnline -Url $SiteURL -UseWebLogin

#Get All Webs of the site collection
$WebsColl = Get-PnPSubWebs -IncludeRootWeb 

ForEach($Web in $WebsColl)
{
    Write-host "Processing Web:"$Web.Url -f Yellow
    
    #get All lists of the web - Exclude the hidden
    $AllLists = Get-PnPProperty -ClientObject $Web -Property Lists
    $Lists = $AllLists | Where {$_.Hidden -eq $false}

    #Loop through each list
    ForEach($List in $Lists)
    {
        Write-host "`t$($List.Title) has $($List.itemCount) items" -f Green
    }
}
  • 如何使用 PowerShell 在 SharePoint 中添加新列表项?
  • 如何使用 PowerShell 获取 SharePoint 站点中的所有列表?
  • SharePoint Online:使用 PowerShell 创建列表
  • 如何使用 CSOM-PowerShell 删除 SharePoint Online 中的列表?

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

取消回复欢迎 发表评论:

关灯