PowerShell kullanarak CSV Dosyasındaki SharePoint Listesi Verilerini Eşitle
Gereksinim: Kuruluş çapındaki kişilerin Uzmanlığını yakalamak için çeşitli alanları içeren bir SharePoint listemiz var. Bu listenin "Ekip" adında özel bir alanı vardır ve 100'den fazla satırdan oluşan bir CSV dosyasından güncellenecektir. Evet, Veri Sayfası görünümü yardımcı olabilir ancak sorun şu: SharePoint listesi ve CSV dosyasının "HesapAdı" alanıyla eşleşen öğelerde güncellenecek Ekip alanı değeri.
Çözüm:
CSV dosyasından SharePoint listesine eşleşen satırı seçip güncellemek için bir PowerShell betiği yazalım.
SharePoint Listesi verilerini CSV Dosyasından senkronize etmek için PowerShell Komut Dosyası:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Read the CSV file
$CSVData = Import-CSV -path "C:\Expertdata.csv"
#Get the Web
$web = Get-SPWeb -identity "https://portal.crescent.com"
#Get the Target List
$list = $web.Lists["ExpertProfiles"]
$TeamsLookupList = $web.Lists["Teams"]
#Iterate through each Row in the CSV
foreach ($row in $CSVData)
{
#Get the matching Row from Experts List in SharePoint
$item = $list.Items | Where-Object {$_["AccountName"] -eq $row.AccountName}
if($item -ne $null)
{
Write-Host "Found Matching User in the Experts List:"$row.AccountName
#If the CSV data has valid Team data
if( ( $row.Team -ne $null) -and ( $row.Team -ne [string]::Empty) )
{
#Get Team Field Value (Lookup column) from the Expert Item
$ExpertTeam = New-Object Microsoft.SharePoint.SPFieldLookupValue($Item["Team"])
#If the current Team Value is empty or Null
if ( ($ExpertTeam.LookupValue -eq $null) -or ( $ExpertTeam.LookupValue -eq [string]::Empty))
{
#Get the Team from Parent Lookup List
$LookupItem = $TeamsLookupList.Items | where {$_["Team"] -eq $row.Team} | Select-Object -First 1
$item["Team"] = $LookupItem.ID
$item.Update()
Write-Host "Updated Team in:"$row.AccountName -ForegroundColor Green
}
}
}
}
Şunlar gibi ek doğrulamalar ekledim: Mevcut Ekip alanı değerinin iş gereksinimlerine göre boş olup olmadığının kontrol edilmesi.
