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

[玩转系统] SharePoint Online:PowerShell 循环浏览列表项

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

SharePoint Online:PowerShell 循环浏览列表项


要求: SharePoint Online PowerShell 循环访问列表项。

在运行脚本之前,请确保您已在计算机中下载并安装了 SharePoint Online 客户端 SDK:https://www.microsoft.com/en-us/download/details.aspx?id=42038

[玩转系统] SharePoint Online:PowerShell 循环浏览列表项

PowerShell 循环访问 SharePoint Online 中的列表项

作为 SharePoint Online 管理员,有时您可能需要循环浏览 SharePoint Online 中的项目列表。 PowerShell 提供了一种方便的方法来执行此操作。如果您需要执行诸如从列表导出数据或对列表中的每个项目执行批量操作等任务,这会很有用。这篇文章将探讨如何使用 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"
$ListName="Projects"
  
#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 List
$List=$Ctx.Web.Lists.GetByTitle($ListName)

#Get All List items
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()

Write-host "Total Number of List Items found:"$ListItems.count

#sharepoint online powershell loop through list items
Foreach ($Item in $ListItems)
{
    #Get the List Item's Title
    Write-host $Item["Title"]
}

此 PowerShell 脚本获取给定列表中的所有项目,循环遍历每个项目,并获取列表项的标题。

使用 PnP PowerShell 迭代 SharePoint Online 中的列表项

当您需要对列表中的每个项目执行特定操作时,使用 PowerShell 循环浏览 SharePoint Online 中的列表项目可能非常有用。使用 PnP PowerShell 脚本循环访问 SharePoint Online 列表项要简单得多:


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

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

#Get all list items
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000

ForEach($ListItem in $ListItems)
{
    #Get List Item ID
    Write-host $ListItem.ID
}

同样,您可以使用 PowerShell 循环访问文档库中的每个文档:SharePoint Online:PowerShell 迭代文档库中的所有文件

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

取消回复欢迎 发表评论:

关灯