Link to home
Start Free TrialLog in
Avatar of garberj76
garberj76

asked on

Uploading File to Sharepoint 2010 using PS

Ok, I'm a novice to both Sharepoint and Powershell.  Have looked up many ways to script uploading a file to Sharepoint.  So here's where I am now stuck.  Here is the script:


if((Get-PSSnapin "Microsoft.SharePoint.PowerShell") -eq $null)
{
    Add-PSSnapin Microsoft.SharePoint.PowerShell
}
 
#Script settings
 
#SiteName
$webUrl = "http://sharepointserver
 
#SharePoint Library Name
$SPLibraryName = "Documents"
 
#library URL  
$SPLibraryUrl = "http://sharepointserver/sites/ost/documents
 
#Location where from you want to upload file to SharePoint lib.
$localFolderPath = "C:\Dockets"
 
 
 
#Open web and library
 #Site URL
$web = Get-SPWeb $webUrl
 #Libray Name
$docLibrary = $web.Lists[$SPLibraryName]
 
 #Getting All Files
 $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles()
 
ForEach($file in $files)
{
 
    #Open file
    $fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
 
    #Add file
    $folder =  $web.getfolder($SPLibraryUrl)
 
    write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
    $spFile = $folder.Files.Add($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    write-host "Success"
 
    #Close file stream
    $fileStream.Close();
}
 
#Dispose web
 
$web.Dispose()

Here is the error I get:  

Exception calling "Add" with "3" argument(s): "<nativehr>0x80070003</nativehr><nativestack></nativestack>There is no file with URL 'http://sharepointserver/sites/ost/documents/<filename>,pdf' in this
Web."
At C:\Scripts\CopyFilesSP.ps1:41 char:32
+     $spFile = $folder.Files.Add <<<< ($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStream, $true)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
Avatar of garberj76
garberj76

ASKER

I should clarify this is Sharepoint 2010 and I'm running this on the server where Sharepoint is hosted.  I apology in advance, I am very new to Sharepoint.
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, Bob.  Sorry for the delay.  For now, this seems to have fixed my problem.