Tìm kiếm trang web

SharePoint Online: Nhận tất cả các mục danh sách bằng tập lệnh PowerShell


Yêu cầu: SharePoint Online PowerShell để lấy mục danh sách.

SharePoint Online: PowerShell để lấy tất cả mục danh sách

PowerShell là ngôn ngữ kịch bản mạnh mẽ có thể được sử dụng để tự động hóa các tác vụ. Trong bài đăng blog này, chúng tôi sẽ chỉ cho bạn cách sử dụng PowerShell để truy vấn các mục trong danh sách SharePoint Online. Chúng tôi sẽ đề cập đến những kiến thức cơ bản về cách bắt đầu và cung cấp một số ví dụ về PowerShell về cách truy vấn các mục danh sách trong SharePoint Online. Bắt đầu nào!

Làm cách nào để lấy các mục từ danh sách SharePoint Online bằng PowerShell?

Bạn đang tìm kiếm một cách nhanh chóng và dễ dàng để tải mục danh sách trong SharePoint Online? Vâng, đây là một ví dụ về tập lệnh PowerShell CSOM để cung cấp cho bạn tất cả các mục trong danh sách hoặc thư viện nhất định, bao gồm các mục từ tất cả các thư mục con, theo cách đệ quy.


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com"
$ListName="Projects"

$UserName="admin@crescent.com"
$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 để lấy mục danh sách theo ID

Nếu bạn muốn lấy một mục danh sách theo ID của nó, hãy sử dụng cách sau:


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

Khi bạn nhận được Mục, Bạn có thể nhận các thuộc tính của mục danh sách dưới dạng:


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

PowerShell để lọc và lấy các mục danh sách bằng CAML:

Làm cách nào để lọc và truy vấn Mục danh sách bằng Truy vấn CAML? Đây là cách lấy các mục từ danh sách SharePoint Online bằng PowerShell.

SharePoint Online: PowerShell để lấy mục danh sách theo tiêu đề
Dưới đây là cách lấy mục danh sách bằng PowerShell trong SharePoint Online được lọc theo truy vấn CAML:


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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 này nhận mục danh sách theo tiêu đề trong SharePoint Online. Tương tự, bạn có thể lọc bất kỳ loại dữ liệu nào khác như danh sách thả xuống, v.v.


#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 để lọc thư mục và lấy tất cả các mục trong danh sách

Hãy lọc tất cả các đối tượng Thư mục từ bộ sưu tập “ListItems” và chỉ lấy các tệp.


#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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 để lấy các mục danh sách bằng lệnh ghép ngắn Get-PnPListItem

Làm cách nào để lấy các mục từ danh sách SharePoint Online trong PowerShell? Đây là PnP PowerShell để lấy tất cả các mục danh sách từ danh sách SharePoint Online bằng lệnh ghép ngắn Get-PnPListItem. Đảm bảo Mô-đun PowerShell PnP được cài đặt trong hệ thống của bạn và bạn có quyền truy cập trang SharePoint trước khi thực thi tập lệnh PowerShell này.


#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"] 
}

Tập lệnh này lấy tất cả các tệp và thư mục dưới dạng các mục danh sách từ danh sách “Tài liệu” trong tuyển tập trang SharePoint Online đã cho. Trên các danh sách lớn hơn, hãy đảm bảo bạn sử dụng thông số “PageSize”. Nếu không, bạn có thể gặp phải thông báo “Get-PnPListItem: Thao tác đã thử bị cấm vì nó vượt quá ngưỡng xem danh sách”. thông báo lỗi. Ngoài ra, lệnh ghép ngắn Get-PnPListItem với tham số "Truy vấn" với điều kiện "Ở đâu" hoặc "FolderServerRelativeurl" không thành công trên các danh sách lớn hơn. Tính đến thời điểm hiện tại, “PageSize” không có tác dụng đối với các thông số này.


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

Nhận các mục danh sách bằng PnP PowerShell trong SharePoint Online

Tương tự, Bạn có thể sử dụng tham số -Query để lọc và lấy các mục danh sách trong SharePoint Online bằng PowerShell:


#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"]
}

Sử dụng Khối tập lệnh để truy xuất các mục danh sách từ SharePoint Online

Đây là cách sử dụng khối tập lệnh và thanh tiến trình để truy xuất các mục từ danh sách 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

Tương tự, Bạn có thể lấy Mục danh sách với các trường cụ thể bằng cách chỉ định phần tử 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

Các câu hỏi thường gặp:

Làm cách nào để tôi có được hơn 5000 mục từ danh sách SharePoint?

Giả sử bạn có một danh sách lớn với> 5000 mục. Trong trường hợp đó, Bạn có thể phải xử lý hàng loạt các mục danh sách để vượt qua giới hạn ngưỡng: “Thao tác đã cố gắng bị cấm vì nó vượt quá ngưỡng xem danh sách do quản trị viên thực thi”. PowerShell để lấy tất cả các mục danh sách từ danh sách lớn ( >5000 mục) trong SharePoint Online

Bạn cũng có thể sử dụng Get-PnPListItem với tham số -PageSize.

Làm cách nào để xuất danh sách SharePoint từ PowerShell?

Để xuất danh sách SharePoint Online sang tệp CSV trong PowerShell, hãy sử dụng tập lệnh PowerShell này: SharePoint Online PowerShell để xuất các mục danh sách sang CSV

Làm cách nào để lấy số lượng mục trong danh sách SharePoint bằng PowerShell?

Sử dụng thuộc tính “List.ItemCount” để truy xuất tổng số mục trong danh sách hoặc thư viện tài liệu SharePoint Online. Đây là tập lệnh PowerShell mẫu để lấy số lượng mục của danh sách SharePoint Online: SharePoint Online PowerShell để lấy số lượng mục trong danh sách

Làm cách nào để có danh sách tệp từ thư viện tài liệu trong SharePoint bằng PowerShell?

Để truy xuất tất cả các tệp từ thư viện tài liệu SharePoint Online, hãy sử dụng lệnh ghép ngắn PnP PowerShell:”Get-PnPListItem -List “Tên thư viện”. Đây là tập lệnh mẫu: SharePoint Online: Lấy danh sách tệp trong thư viện tài liệu bằng PowerShell

Bài viết liên quan