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

[玩转系统] SharePoint Online:使用 PowerShell 脚本获取所有列表项

作者:精品下载站 日期:2024-12-14 14:04:13 浏览:11 分类:玩电脑

SharePoint Online:使用 PowerShell 脚本获取所有列表项


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

[玩转系统] SharePoint Online:使用 PowerShell 脚本获取所有列表项

SharePoint Online:使用 PowerShell 获取所有列表项

PowerShell 是一种功能强大的脚本语言,可用于自动执行任务。在这篇博文中,我们将向您展示如何使用 PowerShell 查询 SharePoint Online 列表中的项目。我们将介绍如何入门的基础知识,并提供一些有关如何在 SharePoint Online 中查询列表项的 PowerShell 示例。让我们开始吧!

如何使用 PowerShell 从 SharePoint Online 列表中获取项目?

您是否正在寻找一种快速、简单的方法来获取 SharePoint Online 中的列表项?好吧,这里是一个示例 PowerShell CSOM 脚本,用于递归地获取给定列表或库中的所有项目,包括所有子文件夹中的项目。


#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"
$ListName="Projects"

$UserName="[email protected]"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
 
#Set up the context
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
$Context.Credentials = $credentials
  
#Get the List
$List = $Context.web.Lists.GetByTitle($ListName)

#sharepoint online get list items powershell
$ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()) 
$Context.Load($ListItems)
$Context.ExecuteQuery()       

write-host "Total Number of List Items found:"$ListItems.Count

#Loop through each item
$ListItems | ForEach-Object {
    #Get the Title field value
    write-host $_["Title"]
}

SharePoint Online:PowerShell 按 ID 获取列表项

如果您想通过 ID 获取单个列表项,请使用以下命令:


#Get the List Item
$List = $Context.web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById(15)
$Context.Load($ListItem)
$Context.ExecuteQuery()

获取项目后,您可以获得列表项属性,如下所示:


#sharepoint online powershell get list item properties
write-host $ListItem["Title"]

PowerShell 使用 CAML 筛选和获取列表项:

使用 CAML 查询过滤和查询列表项怎么样?以下是如何使用 PowerShell 从 SharePoint Online 列表中获取项目。

SharePoint Online:PowerShell 按标题获取列表项
以下是如何在 SharePoint Online 中使用 PowerShell 获取按 CAML 查询筛选的列表项:


#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"
$ItemTitle = "Project Kickoff"
  
#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)
    
    #Get the web and List
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($ListName)
   
    #Get a List item by its Title
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml = "@
    <View>
        <Query>
            <Where>
                <Eq>
                    <FieldRef Name='Title'/><Value Type='Text'>$ItemTitle</Value>
                </Eq>
            </Where>
        </Query>
    </View>"
 
    #Get All List Items matching the query
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    Write-host "Total List Items:" $ListItems.count
   
    #Loop through each file in the library
    Foreach($Item in $ListItems)
    { 
        Write-host -f Green $Item["Title"]
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

此 PowerShell 在 SharePoint Online 中按标题获取列表项。同样,您可以过滤任何其他数据类型,例如下拉列表等。


#Filter and Get the List Items using CAML
$List = $Context.web.Lists.GetByTitle($ListName)
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery;
$Query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='Priority' /><Value Type='Choice'>(3) High</Value></Eq></Where></Query></View>"

#sharepoint online powershell query list items
$ListItems = $list.GetItems($Query);
$Context.Load($ListItems)
$Context.ExecuteQuery()

#sharepoint online powershell loop through list items
$ListItems | ForEach-Object {
    #Get the Title field value
    write-host $_["Title"]
}

PowerShell 过滤文件夹并获取所有列表项

让我们从“ListItems”集合中过滤所有文件夹对象并仅获取文件。


#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"
$LibraryName="Project Docs"
 
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
   
    #Get the web and Library
    $Web=$Ctx.Web
    $List=$web.Lists.GetByTitle($LibraryName)
  
    #Get all List items from the library - Exclude "Folder" objects
    $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
    $Query.ViewXml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType'/><Value Type='Integer'>0</Value></Eq></Where></Query></View>"
    $ListItems = $List.GetItems($Query)
    $Ctx.Load($ListItems)
    $Ctx.ExecuteQuery()

    Write-host "Total List Items:" $ListItems.count
  
    #Loop through each file in the library
    Foreach($Item in $ListItems)
    { 
        Write-host -f Green $Item["Title"]
    }
}
Catch {
    write-host -f Red "Error deleting versions!" $_.Exception.Message
}

SharePoint Online:PnP PowerShell 使用 Get-PnPListItem cmdlet 获取列表项

如何在 PowerShell 中从 SharePoint Online 列表获取项目?下面是使用 Get-PnPListItem cmdlet 从 SharePoint Online 列表获取所有列表项的 PnP PowerShell。确保您的系统中已安装 PnP PowerShell 模块,并且在执行此 PowerShell 脚本之前您有权访问 SharePoint 站点。


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

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online pnp powershell get list items 
$ListItems = Get-PnPListItem -List $ListName -Fields "Title" -PageSize 2000

#Loop through each Item
foreach($ListItem in $ListItems)
{  
    Write-Host "Title:" $ListItem["Title"] 
}

此脚本从给定 SharePoint Online 网站集中的“文档”列表中获取所有文件和文件夹作为列表项。在较大的列表中,请确保使用“PageSize”参数。否则,您可能会遇到“Get-PnPListItem:尝试的操作被禁止,因为它超出了列表视图阈值。”错误信息。此外,带有“Query”参数和“Where”条件或“FolderServerRelativeurl”的 Get-PnPListItem cmdlet 在较大的列表上失败。截至目前,“PageSize”对这些参数没有影响。


Get-PnPListItem -List $ListName -Fields "Title" -PageSize 2000

在 SharePoint Online 中使用 PnP PowerShell 获取列表项

同样,您可以使用 -Query 参数通过 PowerShell 筛选和获取 SharePoint Online 中的列表项:


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Projects"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#CAML Query to Filter List Items
$Query = "<View><Query><Where><Eq><FieldRef Name='ProjectStatus' /><Value Type='Choice'>Active</Value></Eq></Where></Query></View>"

#get all list items using powershell sharepoint online - matching given query
$ListItems = Get-PnPListItem -List $ListName -Query $Query

#Loop through each Item
foreach($ListItem in $ListItems)
{  
    Write-Host "Id :" $ListItem["ID"] 
    Write-Host "Title :" $ListItem["Title"]
}

使用脚本块从 SharePoint Online 检索列表项

以下是如何使用脚本块和进度栏从 SharePoint Online 列表中检索项目。


$List= Get-PnPList $ListName
$global:counter = 0;
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000 -Fields "Title" -ScriptBlock { Param($items) $global:counter += $items.Count; Write-Progress -PercentComplete ($global:Counter / ($List.ItemCount) * 100) -Activity "Getting List Items of '$($List.Title)'" -Status "Processing Items $global:Counter to $($List.ItemCount)";}

Write-host $ListItems.Count

同样,您可以通过指定 ViewFields 元素来获取具有特定字段的列表项。


#CAML Query to Get List Items with Specific fields
$Query = "<View><ViewFields><FieldRef Name='Title'/></ViewFields></View>"

#sharepoint online pnp powershell get list items 
$ListItems = Get-PnPListItem -List $ListName -PageSize 2000 -Query $Query

经常问的问题:

如何从 SharePoint 列表中获取超过 5000 个项目?

假设您有一个包含 > 5000 个项目的大型列表。在这种情况下,您可能必须对列表项进行批处理以克服阈值限制:“尝试的操作被禁止,因为它超出了管理员强制执行的列表视图阈值。” PowerShell 从 SharePoint Online 中的大型列表(>5000 个项目)中获取所有列表项目

您还可以使用带有 -PageSize 参数的 Get-PnPListItem。

如何从 PowerShell 导出 SharePoint 列表?

若要在 PowerShell 中将 SharePoint Online 列表导出到 CSV 文件,请使用以下 PowerShell 脚本:SharePoint Online PowerShell 将列表项导出到 CSV

如何使用 PowerShell 获取 SharePoint 列表中的项目数?

使用“List.ItemCount”属性可检索 SharePoint Online 列表或文档库中的项目总数。以下是用于获取 SharePoint Online 列表的项目计数的示例 PowerShell 脚本:用于获取列表项目计数的 SharePoint Online PowerShell

如何使用 PowerShell 从 SharePoint 文档库中获取文件列表?

要从 SharePoint Online 文档库检索所有文件,请使用 PnP PowerShell cmdlet:“Get-PnPListItem -List “库名称”。以下是示例脚本:SharePoint Online:使用 PowerShell 获取文档库中的文件列表

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

取消回复欢迎 发表评论:

关灯