Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to upload a file from a local drive to a sharepoint doc lib, this much I have been able to find, however, I also want to update a column relative to the file, foe example, I have a company column that I want to populate along with the upload, how would I modify this powershell script to do so? Any help would be awesome!

 Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

 function UploadFile($Web, $DocLibName, $FilePath)
 {
   $List = $Web.GetFolder($DocLibName) 
   $Files = $List.Files
   $FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1) 
   $File= Get-ChildItem $FilePath
   $Files.Add($DocLibName +"/" + $FileName,$File.OpenRead(),$false)
}
$SiteUrl = "https://foo.foo.com"
$Web = Get-SPWeb $SiteUrl
UploadFile $Web "DocLib" "C:\x.pdf"
Write-Host "Documents Uploaded to $SiteUrl"
$web.Dispose()
share|improve this question
    
THank you, I get the following error: > Exception calling "GetFile" with "1" argument(s): "Value does not fall > within t he expected range." At line:9 char:53 > + [Microsoft.SharePoint.SPFile]$spFile = $Web.GetFile <<<< ("/" + $folder.Url > + "/" + $File.Name) > + CategoryInfo : NotSpecified: (:) [], MethodInvocationException > + FullyQualifiedErrorId : DotNetMethodException update file properties ... > Cannot index into a null array. At line:22 char:15 > + $spFile.Item[ <<<< "Title"] = "Reference Document" > + CategoryInfo : InvalidOperation: (Title:String) [], RuntimeExce ption > + FullyQua –  Josh Jones Mar 15 '13 at 14:57
    
I did, due to space limitations I did not include my script –  Josh Jones Mar 16 '13 at 2:42
    
These are the fields I updated with my own: –  Josh Jones Mar 16 '13 at 2:43
    
$spFile.Item["Title"] = "Reference Document" $SiteUrl = "dhaval-pc:2011/"; UploadFile $Web "Proposal" "C:\test.pdf" –  Josh Jones Mar 16 '13 at 2:44
add comment

1 Answer

For this, an understanding of Server Object Model (SSOM) is needed. When you upload a file, you get the reference to the SPFile object. The corresponding SPListItem can be accessed through its Item member

$spFile = $folder.Files.Add(...
$spFile.Item["SomeColumn"] = "SomeValue"
$spFile.Item.Update()

Try this complete script :

 Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

function UploadFile($Web, $DocLibName, $FilePath)
{

  $Web.AllowUnsafeUpdates = $true;
  $List = $web.Lists[$DocLibName]
  $folder = $List.RootFolder
  #$Files = $List.Files
  $FileName = $FilePath.Substring($FilePath.LastIndexOf("\")+1) 
  $File= Get-ChildItem $FilePath

 [Microsoft.SharePoint.SPFile]$spFile = $Web.GetFile("/" + $folder.Url + "/" + $File.Name)

if($spFile.Exists -eq $false)
   {
    #Open file
    $fileStream = ([System.IO.FileInfo] (Get-Item $File.FullName)).OpenRead()
    #Add file
    write-host "Copying file " $File.Name " to " $folder.ServerRelativeUrl "..."
    [Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.Url + "/" + $File.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"
    #Close file stream
    $fileStream.Close()
  }

 write-host "update file properties " $spFile.Name "..."
 $spFile.Item["Title"] = "Reference Document"
 $spFile.Item.Update()
 $Web.AllowUnsafeUpdates = $false;

 }
 $SiteUrl = "http://dhaval-pc:2011/"
 $Web = Get-SPWeb $SiteUrl
 UploadFile $Web "Proposal" "C:\test.pdf"
 Write-Host "Documents Uploaded to $SiteUrl"
 $Web.Dispose()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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