How to upload the file into the document library and insert/update the metadata columns.I am having 3 people picker data types/user group columns and one choice field and 2 single line of text columns. I have written the PowerShell to upload the document in the document library but how to get the item which is inserted and update the metadata. Do I need to use the getitembyid()? Also when I ran the script it inserted successfully and its checked-out.
[System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.SharePoint")
if((Get-PSSnapin | Where {$_.Name -
eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
#Site Collection where you want to upload files
$siteCollUrl
= "http://srvr1:123/sites/R-Engineering/"
#Document Library where you want to upload files
$libraryName = "KG"
#Physical/Network location of files
$reportFilesLocation = "E:\Maddy\Scripts\FileUpload"
$spSourceWeb = Get-SPWeb $siteCollUrl;
$spSourceList = $spSourceWeb.Lists[$libraryName];
if($spSourceList -eq $null)
{
Write-Host "The Library $libraryName could not be found."
return;
}
$files = ([System.IO.DirectoryInfo] (Get-Item
$reportFilesLocation)).GetFiles()
foreach($file in $files)
{
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
#Add file
$folder = $spSourceWeb.getfolder($libraryName)
Write-Host "Copying file $file to $libraryName..."
$spFile = $folder.Files.Add($folder.Url + "/" + $file.Name,
[System.IO.Stream]$fileStream, $true)
#Close file stream
$fileStream.Close();
}
$spSourceWeb.dispose();
Write-Host "Files have been uploaded to $libraryName."