This script grabs data from a CSV file and uploads it to a SharePoint list. It also writes an error log in case the try catch fails.
<#
.SYNOPSIS
Uploads a CSV file to a SharePoint list
.DESCRIPTION
This script grabs data from a CSV file and uploads it to a SharePoint list. It also writes an error log
in case the try catch fails.
.NOTES
┌─────────────────────────────────────────────────────────────────────────────────────────────┐
│ ORIGIN STORY │
├─────────────────────────────────────────────────────────────────────────────────────────────┤
│ DATE : 2020-04-29
│ AUTHOR : Mike Gritton
│ DESCRIPTION : Initial Draft
└─────────────────────────────────────────────────────────────────────────────────────────────┘
#>
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
cls
#variables
$listDisplayName = "CompanyFunding"
$webUrl = "http://spdevserver"
$csvPath = "C:\CodeFiles\TechCrunchcontinentalUSA.csv"
$errorPath = "C:\CodeFiles\DataMigrationErrors.txt"
# CSV path/File name
$contents = Import-Csv $csvPath
# Web URL
$web = Get-SPWeb -Identity $webUrl
# SPList name
$list = $web.Lists[$listDisplayName]
# Iterate for each list column
$i = 0
foreach ($row in $contents) {
write-host "Processing item # $i"
$i++
$item = $list.Items.Add()
$item["Company"] = $row."company";
$item["Permalink"] = $row."permalink" -replace "[\W]", "" #Replaces the whitespace
$item["Num of Employees"] = $row."numEmps"
$item["Category"] = $row."category"
$item["City"] = $row."city" -replace "[\W]", ""
$item["State"] = $row."state"
$item["Fund Date"] = $row."fundedDate"
$item["Raised Amount"] = $row."raisedAmt"
$item["Raised Currency"] = $row."raisedCurrency"
$item["Round"] = $row."round"
try
{
$item.Update()
}
catch
{
$exception = $_.Exception.Message
write-host "Error in the script: " + $_.Exception.Message -BackgroundColor Red -ForegroundColor White
"Item Number: $i - $exception" | out-file $errorPath -append
}
}