Quantcast
Channel: SharePoint 2010 - Setup, Upgrade, Administration and Operations forum
Viewing all articles
Browse latest Browse all 13778

Fetching full folder URL in Powershell

$
0
0

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


Viewing all articles
Browse latest Browse all 13778

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>