Tìm kiếm trang web

SharePoint Online: PowerShell để lấy giá trị trường mục danh sách


Yêu cầu: PowerShell để lấy giá trị trường mục danh sách trong SharePoint Online.

SharePoint Online PowerShell để lấy giá trị trường mục danh sách

Với tư cách là quản trị viên SharePoint Online, sẽ có lúc bạn cần lấy giá trị trường của một mục danh sách thông qua PowerShell. Trong bài đăng trên blog này, tôi sẽ chỉ cho bạn cách sử dụng PowerShell để lấy giá trị trường của một mục danh sách.


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

#Parameters
$SiteURL = "https://crescent.sharepoint.com"
$ListName = "Projects"
$FieldName = "Manager"
$ItemId="1"

#Set Authentication Parameters
$UserName = "Salaudeen@Crescent.com"
$Password = "Password1"
$SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force

#Setup the Context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
 
#Get List Item
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ItemId)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()    

#sharepoint online powershell to get list item field value
$ListItem[$FieldName]

#Also does the same: Get the Field Value
#$ListItem.FieldValues[$FieldName]

Đoạn script trên trả về dữ liệu theo loại trường. Ví dụ: vì trường “Người quản lý” là “Người hoặc Tra cứu”, tập lệnh PowerShell này trả về loại dữ liệu Microsoft.SharePoint.Client.FieldUserValue . Hãy chỉ lấy giá trị văn bản từ một trường dưới dạng:


#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"
  
#Parameters
$SiteURL = "https://Crescent.sharepoint.com"
$ListName = "Projects"
$FieldName = "Manager"
$ItemId="1"

#Get credentials to connect
$Cred = Get-Credential

#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 Item
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$ListItem = $List.GetItemById($ItemId)
$Ctx.Load($ListItem)
$Ctx.ExecuteQuery()

#Get List Item Field Values as Text
$ListItemFieldValues = $ListItem.FieldValuesAsText
$Ctx.Load($ListItemFieldValues)
$Ctx.ExecuteQuery()

#Get the Text value of "Manager" field from the list Item
$ListItemFieldValues[$FieldName]

PnP PowerShell để nhận giá trị trường của mục danh sách

Truy xuất các giá trị trường cho một mục danh sách trong SharePoint Online có thể là một cách hữu ích để trích xuất dữ liệu và tự động hóa các tác vụ. Với PnP PowerShell, bạn có thể dễ dàng lấy danh sách giá trị trường cho một mục danh sách trong SharePoint Online bằng lệnh ghép ngắn Get-PnPListItem.

Với PnP PowerShell, chúng ta có thể nhận giá trị của một trường từ một mục danh sách dưới dạng:


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

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

#Get a Most recent List Item created
$ListItem = Get-PnPListItem -List $ListName -PageSize 500 | Select -Last 1
 
#Get All Field Values from the List Item
$ListItem.FieldValues

Để có được tất cả các giá trị trường, hãy sử dụng như sau:


#Get All Field Values from the List Item
$FieldValues = $ListItem.FieldValues

ForEach ($Field in $FieldValues.Keys)
{
    Write-Host "$Field : $($FieldValues[$Field])"
}

Với tập lệnh này, bạn có thể nhận danh sách giá trị trường cho mục danh sách SharePoint Online bằng PowerShell. Đây có thể là một cách hữu ích để trích xuất dữ liệu từ danh sách SharePoint của bạn và tự động hóa các tác vụ. Dưới đây là các tập lệnh PowerShell khác của tôi để lấy dữ liệu từ các mục danh sách trong SharePoint Online:

  • SharePoint Online: Nhận Giá trị Trường Hộp kiểm Có/Không bằng PowerShell
  • SharePoint Online: Nhận giá trị trường tra cứu bằng PowerShell
  • SharePoint Online: Nhận Giá trị Trường của Người hoặc nhóm bằng PowerShell

Bài viết liên quan