Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, July 9, 2014 8:56 AM
Hi,
I am trying to upload some images to a sub-folder in a library.
However, the folder path does not seem to be correct, and the image is not getting added.
The actual url will be : http://server/Lists/LibraryName/subfolder
But when using the powershell, the path is coming as : http://Server/LibraryName/subfolder/
Below is the powershell code used:
function UploadImages($weburl)
{
$docLibraryName = "TestLibrary"
$localFolderPath = "C:\Users\Test\test_image"
Add-PsSnapin Microsoft.SharePoint.PowerShell -erroraction silentlycontinue
$web = Get-SPWeb -Identity $webUrl
$docLibrary = $web.Lists[$docLibraryName]
$subFolderName="test_image"
#Attach to local folder and enumerate through all files
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object {
#Create file stream object from file
$fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
$contents = new-object byte[] $fileStream.Length
$fileStream.Read($contents, 0, [int]$fileStream.Length);
$fileStream.Close();
write-host "Copying" $_.Name "to" $docLibrary.Title "in" $web.Title "..."
#Add file
$folder = $web.getfolder($docLibrary.Title + "/" + $subFolderName)
write-host "folder is " $folder
write-host "whole url is " $folder.Url + "/" + $_.Name
[Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
$spItem = $spFile.Item
}
Write-Host -f Green "Added Images to Library !!!"
$web.Dispose()
}
The below line is throwing error:
[Microsoft.SharePoint.SPFile]$spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true)
The write host output is showing the below:
Copying ab_small_gif.gif to testLibrary in testLab ...
folder is testLibrary/test_image
whole url is testLibrary/test_image + / + ab_small_gif.gif
864
ForEach-Object : Exception calling "Add" with "3" argument(s):
"<nativehr>0x80070003</nativehr><nativestack></nativestack>There is no file
with URL 'http://server/testLibrary/test_image/ab_small_gif.gif' in this Web."
How to fix this?
Thanks
All replies (8)
Thursday, July 10, 2014 11:56 AM ✅Answered
Your mistake is in
$folder = $web.getfolder($docLibrary.Title + "/" + $subFolderName)
The $docLibrary.Title value doesn't give you the URL of the library in a reliable manner. For example the title of a SP2013 Shared Documents library is actually 'Documents' but the URL is 'Shared Documents'.
If there's any variation in that then the script will get the wrong URL and fail to upload the document.
Try using this instead:
$folder = $web.getfolder($docLibrary.rootFolder.URL + "/" + $subFolderName)
Also you can improve the script dramatically by only getting the library once, you'll also get less silly mistakes if you create all your strings only once rather than re-creating them (as seen in the quoted line) each time you need them.
Thursday, July 10, 2014 3:14 AM | 1 vote
Hi ,
According to your description, my understanding is that you encountered the error “There is no file with URL 'http://server/testLibrary/test_image/ab_small_gif.gif' in this Web” when you try to upload some images to a sub-folder in a library using PowerShell script.
Per my knowledge, the URL of a SharePoint library should be as this : http://servername/LibraryName
Such as : http://sp2013/Shared%20Documents
The URL of a SharePoint list should be as this: http://servername/Lists/ListName
Such as : http://sp2013/Calendar
So please make sure the location you want to upload the images to is a library.
Also you can try to create a new Document Library and upload your images.
Thanks,
Eric
Forum Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact [email protected].
Eric Tao
TechNet Community Support
Thursday, July 10, 2014 5:48 AM | 1 vote
Hi Venkatzeus,
Hi Venkatzeus,
One finding, i believe you are trying to upload image in List, your actual url is pointing to list path:
The actual url will be : http://server/Lists/LibraryName/subfolder
In this case you cannot upload the image in list subfolder, you can attach the image/files to the list item
your code will work only with document/Picture library in short for all library not with list
Thanks, ShankarSingh
Thursday, July 10, 2014 8:22 AM
Hi,
Thanks for the reply.
If using a list, how should the above code be modified. I want to add the images to the sub-folder inside the list.
Thanks
Thursday, July 10, 2014 9:14 AM
You don't add documents to a list, you might attach them to an item within a list but that's a different thing.
Is this a list or a library? Are you trying to upload a document or attach a document to a list item?
Thursday, July 10, 2014 11:07 AM
HI,
Thanks for the reply.
I am using a Publishing site. It is a library only. The reason is , when I open the library in UI with the URL:
http://SITE/Lists/TestLibrary/Forms/AllItems.aspx
I can see the option as "Add Document" and not "Add Item".
Thursday, July 10, 2014 12:41 PM
Hi,
Thank you very much for the reply.
That worked.
Friday, August 18, 2017 8:58 AM
Ploughing in very late to this thread, but not sure "Url" is a property of RootFolder. Comes back blank for me. This page doesn't document it either:
https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.folder_members.aspx
It does mention ServerRelativeUrl which can be used instead with a bit of string concatenation.