I have a ton of documents that I have to upload into SharePoint from a file share using PowerShell, along with metadata that is in a csv (or excel) file. Is it possible to have a general code that can read/match the columns listed on the .csv file with the columns in the SharePoint library? I was wondering if it is possible to create a script that can be used on any library, without having to list the column names within the code. Am trying to come up with a script that can just be used on any library. Am currently doing something like this, but I would like to use something more generic...where I don't have to specify the column names. Is this even possible?
$SiteUrl = "URL OF THE SITE COLLECTION"
$web = Get-SPWeb $SiteUrl
$csvFile = "C:\\CSVFILEWITHMETADATA.csv"
foreach($i in Import-CSV $csvFile)
{
$Library = $i.DocumentLibrary
$docLibrary = $web.Lists[$Library]
$rootFolder = $docLibrary.RootFolder
$sourceFile = Get-ChildItem $i.DocumentLink
$destinationFolderPath = $rootFolder.Url
if($i.DestinationFolder -ne "")
{
$destinationFolderPath += "/" + $i.DestinationFolder.replace("\","/")
$folders = $i.DestinationFolder.Split("\")
$subFolder = $rootFolder
foreach($f in $folders)
{
$testFolder = $subFolder.SubFolders[$f];
if($testFolder -eq $null)
{
$subFolder = $subFolder.SubFolders.Add($f)"created new folder " + $f
}
else
{
$subFolder = $testFolder
}
}
}
$destinationFolderPath += "/" + $sourceFile.Name"copying " + $destinationFolderPath
$itemType = $docLibrary.ContentTypes[$i.ContentType]
$newFile = $docLibrary.RootFolder.Files.Add($destinationFolderPath,$sourceFile.OpenRead(), $true)
$theItem = $newFile.Item
$theItem["ContentTypeId"] = $itemType.Id
#Write success to log file
$logMsg = "File name " + $i.FileName + " with content type " + $i.ContentType + " successfully added" + "|Success"
Add-Content $logFile $logMsg
# UPDATING METADATA
$theItem["Description"] = $i.Description
$theItem["Title"] = $i.Title
$theItem["Seller"] = $i.Seller
$theItem["Buyer"] = $i.Buyer
$theItem["PurchaseDate"] = $i.PDate
$theItem["ExpDate"] = $i.ExpirationDate
# Prepare and Populated Taxonomy field for Status
$myStatus = $i.Status
$termST = $terms | ?{$_.Name -eq $myStatus}
$tFieldStatus = $docLibrary.Fields["Status"]
$tItemFieldStatus = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$theItem.Fields[$tFieldStatus]
$tItemFieldStatus.SetFieldValue($theItem, $termST)
# Prepare and Populated Taxonomy field for Sales Type
$mySalesType = $i.SalesType
$termSalesType = $terms | ?{$_.Name -eq $mySalesType}
$tFieldSalesType = $docLibrary.Fields["SalesType"]
$tItemFieldSalesType = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$theItem.Fields[$tFieldSalesType]
$tItemFieldSalesType.SetFieldValue($theItem, $termSalesType)
$theItem.Update()
}
$web.Dispose()