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

[玩转系统] 使用 PowerShell 从 CSV 文件更新 SharePoint 列表项

作者:精品下载站 日期:2024-12-14 14:20:31 浏览:16 分类:玩电脑

使用 PowerShell 从 CSV 文件更新 SharePoint 列表项


要求:从 CSV 文件更新 SharePoint 列表项。

一些背景知识:我们有一个包含 1000 行组织范围专家的列表。后来我们添加了一个新字段“部门”。现在的要求是更新特定用户列表的部门字段。虽然快速编辑(或数据表视图)可用于批量编辑,但过滤和编辑 100 行将非常耗时。因此,解决方案是拥有 CSV 文件格式的用户名及其部门列表,并将其提供给 PowerShell!

[玩转系统] 使用 PowerShell 从 CSV 文件更新 SharePoint 列表项

用于读取 CSV 文件并更新 SharePoint 列表项的 PowerShell 脚本:

您是否正在寻找从 CSV 文件更新 SharePoint 列表项的方法? PowerShell 就可以做到这一点!本指南将向您展示如何使用 PowerShell 从 CSV 文件更新 SharePoint 列表项。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Read the CSV file
$CSVData = Import-CSV -path "C:\UserData.csv" 
 
#Get the Web
$web = Get-SPWeb -identity "https://portal.crescent.com"
 
#Get the Target List
$List = $web.Lists["ExpertProfiles"]
 
#Iterate through each Row in the CSV
foreach ($Row in $CSVData) 
{
    #Get the List Item matching "Name" field in the CSV    
    $Item = $List.Items | Where-Object { $_["Title"] -eq $Row.Name }

    if($item -ne $null)
    {
        #Update List Item - Internal Name!
        $item["Department"] = $row.Department
        $item.Update()
        Write-Host "Updated:"$row.Name -ForegroundColor Green
    }
    else
    {
        write-host "No matching Item Found for:"$row.Name -f Red
    }
}

虽然上面的代码达到了目的,但让我们使用 CAML-SPQuery 对其进行优化。

PowerShell 从 CSV 更新 SharePoint 列表:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Read the CSV file
$CSVData = Import-CSV -path "C:\UserData.csv" 
 
#Get the Web
$web = Get-SPWeb -identity "https://portal.crescent.com"
 
#Get the Target List
$List = $web.Lists["ExpertProfiles"]
 
#Iterate through each Row in the CSV
foreach ($Row in $CSVData) 
{
    #Filter using CAML Query
    $CAMLQuery="<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>$($Row.Name)</Value></Eq></Where>"
    $SPQuery=New-Object Microsoft.SharePoint.SPQuery
    $SPQuery.ViewAttributes = "Scope='Recursive'"  #Get all items including Items in Sub-Folders!
    $SPQuery.Query=$CAMLQuery
    $SPQuery.RowLimit = 1 
     
    #Get the List item based on Filter 
    $Item=$List.GetItems($SPQuery)[0]

    If($Item -ne $null)
    {
        #Update List Item
        $Item["Department"] = $Row.Department
        $item.Update()
        Write-Host "Updated:"$row.Name -ForegroundColor Green
    }
    else
    {
        write-host "No matching Item Found for:"$row.Name -f Red
    }
}

从 CSV 文件更新人员选取器字段值:

在另一种情况下,我必须更新人员选择器字段值:


 #If the matching project found
    If($Item -ne $null)
    {
        #Update Team members Multi-People picker field
        $TeamMembersList = new-object Microsoft.SharePoint.SPFieldUserValueCollection
        $TeamMembers = $row."TeamMembers" -split ';'
        foreach ($TeamMember in $TeamMembers)
        {
           If($TeamMember -ne $null) 
           {
               #Prepre the user to add
               $User = $Web.EnsureUser($TeamMember)
               $NewUser = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $User.ID, $User.LoginName)
               $TeamMembersList.Add($NewUser)
           }
        }
        #Update Team members field
        $item["Team Members"] = $TeamMembersList
        $item.Update()
        Write-Host "Updated:"$Row.ProjectName -ForegroundColor Green
    }
    else
    {
        write-host "No matching Item Found for:"$row.ProjectName -f Red
    }

相关文章:

  • SharePoint Online:使用 PowerShell-CSOM 将 CSV 文件导入 SharePoint 列表
  • 使用 Powershell 从 CSV 文件导入 SharePoint 列表
  • 使用 PowerShell 将 SharePoint 列表项导出到 CSV

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

取消回复欢迎 发表评论:

关灯