Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Upload SharePoint Online with Errors: File Not Found, and URL is not web relative

$
0
0
Function Upload-FilesFromServer()
{
    param

    (
        [Parameter(Mandatory=$true)] [ClientContext] $Ctx,
        
        [Parameter(Mandatory=$true)] [string] $SourceFiles,

        [Parameter(Mandatory=$true)] [string] $TargetDocLib
    )

    #Retrieve list
    $List = $Ctx.Web.Lists.GetByTitle($TargetDocLib)

    $Ctx.Load($List)

    $Ctx.ExecuteQuery()

    $TargetFolder = $Ctx.Web.GetFolderByServerRelativeUrl("Documents" + "/" + "Numbers" + "/" + "SubNumbers")

    #Upload file
    Foreach ($File in (Dir $SourceFiles -File))
    {
        $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)

        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation

        $FileCreationInfo.Overwrite = $true

        $FileCreationInfo.ContentStream = $FileStream

        $FileCreationInfo.URL = $File

        $Upload = $TargetFolder.Files.Add($FileCreationInfo)

        $Ctx.Load($Upload)
        
        $Ctx.ExecuteQuery()
    }

}






When I run the code I get:

Exception calling "ExecuteQuery" with "0" argument(s): "File Not Found."

At line:27 char:9

+         $Ctx.ExecuteQuery()

+         ~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ServerException

When I checked the variable $FileCreationInfo, it has all the file information.

Sometimes I would get this running the same code above:

Exception calling "ExecuteQuery" with "0" argument(s): "Access denied. You do not have

permission to perform this action or access this resource."

At line:28 char:9

+         $Ctx.ExecuteQuery()

+         ~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : ServerUnauthorizedAccessException

But I have global admin access.

I'm trying to upload files to a folder that's several folders down such as: Documents/Numbers/SubNumbers.



Viewing all articles
Browse latest Browse all 7589

Trending Articles