3

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()
4
  • 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
    Commented Mar 15, 2013 at 14:57
  • I did, due to space limitations I did not include my script
    – Josh Jones
    Commented Mar 16, 2013 at 2:42
  • These are the fields I updated with my own:
    – Josh Jones
    Commented Mar 16, 2013 at 2:43
  • $spFile.Item["Title"] = "Reference Document" $SiteUrl = "dhaval-pc:2011" UploadFile $Web "Proposal" "C:\test.pdf"
    – Josh Jones
    Commented Mar 16, 2013 at 2:44

1 Answer 1

5

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()
1
  • how can this great script be modified to create and upload to subfolders? Regards Lars
    – user45668
    Commented Sep 3, 2015 at 19:46

Your Answer

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

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