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.Powe rShell") -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.Power Shell
}
#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)).GetFile s()
ForEach($file in $files)
{
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead( )
#Add file
$folder = $web.getfolder($SPLibraryU rl)
write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
$spFile = $folder.Files.Add($folder. Url + "/" + $file.Name, [System.IO.Stream]$fileStr eam, $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</nat ivehr><nat ivestack>< /nativesta ck>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]$fileStr eam, $true)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
if((Get-PSSnapin "Microsoft.SharePoint.Powe
{
Add-PSSnapin Microsoft.SharePoint.Power
}
#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]
ForEach($file in $files)
{
#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead(
#Add file
$folder = $web.getfolder($SPLibraryU
write-host "Copying file " $file.Name " to " $folder.ServerRelativeUrl "..."
$spFile = $folder.Files.Add($folder.
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</nat
Web."
At C:\Scripts\CopyFilesSP.ps1
+ $spFile = $folder.Files.Add <<<< ($folder.Url + "/" + $file.Name, [System.IO.Stream]$fileStr
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, Bob. Sorry for the delay. For now, this seems to have fixed my problem.
ASKER