0

I have a weekly export in CSV format that contains employee information from another system.

I would like to loop through the CSV and upload X number of documents to a permanent document library on a default template from a template library elsewhere with each new document having a unique name i.e. Employee1, Employee2 that I then need mapped to the corresponding row on the CSV, amount depents on total rows.

I then need to update the metadata of each new document to the corresponding row on the CSV file.

The script below only works if there is on row on the CSV.

Thanks to http://www.sharepointdiary.com/2012/10/bulk-upload-files-to-sharepoint-using-powershell.html

#Configuration Variables
$SiteURL="http://sharepointdev/eregister"
$LibraryName="employees"
$FilesPath="\\sharepointdev\DavWWWRoot\Lists\Templates\"
$TemplateName="Employee Agreement"
$CSVFilePath="e:\weeklyexport\employees.csv"

#Get the Web
$Web = Get-SPWeb $SiteURL

#Get the Target Document Library to upload
$Library = $Web.GetFolder($LibraryName)

#Get CSV file contents
$CSVData = Import-CSV -path $CSVFilePath

#Count CSV rows
[int]$LinesInFile = 0
$reader = New-Object IO.StreamReader $CSVFilePath
 while($reader.ReadLine() -ne $null){ $LinesInFile++ }

    write-host "Number of records to process:"$LinesInFile

#Iterate through each Row in the CSV
Foreach ($Row in $CSVData)
{

    write-host "Uploading File:"$Row.Name

    #Get the File from local disk 
    $SourcePath= Join-Path $FilesPath -ChildPath  $Row.Name
    $SourceFile = (Get-ChildItem $SourcePath).OpenRead()

    #Set Metadata Hashtable For the file
    $Metadata = @{"FullName" = $Row.Full_Name;"Department" = $Row.Department;"Position" = $Row.Position;"StartDate" = $Row.Start_Date}

    #upload the file              
    $uploaded = $Library.Files.Add($Row.Name+$LinesInFile, $SourceFile, $Metadata, $TRUE)
}

Can anyone help to further progress this script to handle multiple records?

Any help is appreciated

Thanks

1
  • Can anyone suggest anything please?
    – Lorenzo
    Mar 13, 2017 at 14:25

1 Answer 1

0

Well, I dont actually know why it is not working for you on multiple lines.

I tested following and it iterates as expected:

$config=import-csv C:\Temp\anything.csv

foreach ($cfgLine in $config) {

 write-host $cfgLine.test1
 write-host $cfgLine.test2
 write-host $cfgLine.test3

}

Have you got any error messages?

Try to comment out this part of code:

[int]$LinesInFile = 0

$reader = New-Object IO.StreamReader $CSVFilePath

while($reader.ReadLine() -ne $null){ $LinesInFile++ }

write-host "Number of records to process:"$LinesInFile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.