SharePoint PowerShell 从CSV文件导入数据到列表

  最近,因为新系统要上线,总是需要准备数据,把文档里的数据导入到SharePoint 列表当中,复制吧快速编辑功能还不给力,就想到写个PowerShell 扔给BA去玩。

  这里就举个简单的列表,结构如下:

  我们需要的CSV文件结构如下:

  导入CSV到List的PowerShell 命令如下:

Add-PSSnapin Microsoft.SharePoint.PowerShell
  
#Read CSV file
$CSVData = Import-CSV -path "D:\Data.csv"
  
#Get SPWeb
$web = Get-SPWeb -identity "http://sitecollection"
  
#Get SPList
$List = $web.Lists["ListName"]
  
#Loop every Row in the CSV
foreach ($Row in $CSVData)
{
    #New SPListItem 
    $Item = $List.Items.add()

    #Add SPColumn Value
    $item["Title"] = $row.Title
    $item["Name"] = $row.Name
    $item["Mark"] = $row.Mark
    $item.Update()
    Write-Host "Added: "$row.Name -ForegroundColor Green

}

  PowerShell 执行结果:

  结束语

  其实,整个代码思路是很简单的,读取CSV文件里的所有行,然后循环插入到列表就好了。

  如果运维或者BA的命令行能力比较强,自己都能够搞定,因为语法是标准的PowerShell 语法。

  更多常用PowerShell脚本,请关注 https://github.com/linyus

猜你喜欢

转载自www.cnblogs.com/jianyus/p/12369525.html
今日推荐