SharePoint Online: PowerShell เพื่อรับค่าฟิลด์ของรายการ
ข้อกำหนด: PowerShell เพื่อรับค่าฟิลด์ของรายการใน SharePoint Online
SharePoint Online PowerShell เพื่อรับค่าฟิลด์รายการ
ในฐานะผู้ดูแลระบบ SharePoint Online จะมีบางครั้งที่คุณต้องการรับค่าฟิลด์ของรายการผ่าน PowerShell ในโพสต์บล็อกนี้ ฉันจะแสดงวิธีใช้ PowerShell เพื่อรับค่าฟิลด์ของรายการ
#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]
สคริปต์ข้างต้นส่งคืนข้อมูลตามประเภทฟิลด์ เช่น เนื่องจากฟิลด์ "ผู้จัดการ" คือ "บุคคลหรือการค้นหา" สคริปต์ PowerShell นี้จะส่งคืนชนิดข้อมูล Microsoft.SharePoint.Client.FieldUserValue รับเฉพาะค่าข้อความจากฟิลด์เป็น:
#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 เพื่อรับค่าฟิลด์ของรายการ
การเรียกค่าเขตข้อมูลสำหรับรายการใน SharePoint Online อาจเป็นวิธีที่มีประโยชน์ในการแยกข้อมูลและทำให้งานเป็นแบบอัตโนมัติ ด้วย PnP PowerShell คุณสามารถรับรายการค่าฟิลด์สำหรับรายการใน SharePoint Online ได้อย่างง่ายดายโดยใช้ Get-PnPListItem cmdlet
ด้วย PnP PowerShell เราสามารถรับค่าของฟิลด์จากรายการเป็น:
#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
หากต้องการรับค่าฟิลด์ทั้งหมด ให้ใช้ดังต่อไปนี้:
#Get All Field Values from the List Item
$FieldValues = $ListItem.FieldValues
ForEach ($Field in $FieldValues.Keys)
{
Write-Host "$Field : $($FieldValues[$Field])"
}
ด้วยสคริปต์นี้ คุณสามารถรับรายการค่าฟิลด์สำหรับรายการ SharePoint Online ได้โดยใช้ PowerShell นี่อาจเป็นวิธีที่มีประโยชน์ในการดึงข้อมูลจากรายการ SharePoint ของคุณและทำให้งานเป็นแบบอัตโนมัติ ต่อไปนี้เป็นสคริปต์ PowerShell อื่นๆ ของฉันเพื่อรับข้อมูลจากรายการใน SharePoint Online:
- SharePoint Online: รับค่าฟิลด์ช่องทำเครื่องหมายใช่/ไม่ใช่โดยใช้ PowerShell
- SharePoint Online: รับค่าฟิลด์การค้นหาโดยใช้ PowerShell
- SharePoint Online: รับค่าฟิลด์บุคคลหรือกลุ่มโดยใช้ PowerShell
