Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用powershell将文件添加到sharepoint子列表_Powershell_Sharepoint - Fatal编程技术网

使用powershell将文件添加到sharepoint子列表

使用powershell将文件添加到sharepoint子列表,powershell,sharepoint,Powershell,Sharepoint,我使用powershell将文件从本地驱动器上载到sharepoint站点 我有一个“文档”列表,他包含一个名为“X”的文件夹 我的问题是:如何将文件上载到X文件夹 这是我的代码: $SiteURL = "domain/sites/testing/" $libraryName="Documents/X" $SPOnlineUserName="user@domain" 当我上传到文档时,脚本工作正常($libraryName变量为“Documents”) 但是当$libraryName=“Doc

我使用powershell将文件从本地驱动器上载到sharepoint站点

我有一个“文档”列表,他包含一个名为“X”的文件夹

我的问题是:如何将文件上载到X文件夹

这是我的代码:

$SiteURL = "domain/sites/testing/"
$libraryName="Documents/X"
$SPOnlineUserName="user@domain"
当我上传到文档时,脚本工作正常(
$libraryName
变量为“Documents”)

但是当
$libraryName=“Documents/X”
文件夹中出现以下错误:

List 'Documents/X' does not exist at site with URL 'domain/sites/testing'
有什么办法解决这个问题吗

[编辑]

我可以通过浏览器访问“X”文件夹


最佳。

大型文件上传更新:

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteURL="https://tenant.sharepoint.com/sites/dev"
$User = "user@tenant.onmicrosoft.com"  
$Password = '*********************' 
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,(ConvertTo-SecureString $Password -AsPlainText -Force))
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$ctx.Credentials = $Credentials

$fileChunkSizeInMB = 9

# Each sliced upload requires a unique ID.
$UploadId = [GUID]::NewGuid()

# Get the name of the file.
$UniqueFileName = [System.IO.Path]::GetFileName("D:\\Yoyo.wmv")


# Get the folder to upload into. 
$Docs = $ctx.Web.Lists.GetByTitle("Documents")
$ctx.Load($Docs)
$ctx.Load($Docs.RootFolder)
$ctx.ExecuteQuery()

# Get the information about the folder that will hold the file.
$ServerRelativeUrlOfRootFolder = $Docs.RootFolder.ServerRelativeUrl

# File object.
[Microsoft.SharePoint.Client.File] $upload


# Calculate block size in bytes.
$BlockSize = $fileChunkSizeInMB * 1024 * 1024


# Get the size of the file.
$FileSize = (Get-Item $fileName).length

if ($FileSize -le $BlockSize)
{
    # Use regular approach.
    $FileStream = New-Object IO.FileStream($fileName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $UniqueFileName
    $Upload = $Docs.RootFolder.Files.Add($FileCreationInfo)
    $ctx.Load($Upload)
    $ctx.ExecuteQuery()
    return $Upload
}
else
{

    # Use large file upload approach.
    $BytesUploaded = $null

    $Fs = $null

    Try {

        $Fs = [System.IO.File]::Open($fileName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)

        $br = New-Object System.IO.BinaryReader($Fs)

        $buffer = New-Object System.Byte[]($BlockSize)
        $lastBuffer = $null
        $fileoffset = 0
        $totalBytesRead = 0
        $bytesRead
        $first = $true
        $last = $false

        # Read data from file system in blocks. 
        while(($bytesRead = $br.Read($buffer, 0, $buffer.Length)) -gt 0) {

            $totalBytesRead = $totalBytesRead + $bytesRead

            # You've reached the end of the file.
            if($totalBytesRead -eq $FileSize) {
                $last = $true
                # Copy to a new buffer that has the correct size.
                $lastBuffer = New-Object System.Byte[]($bytesRead)
                [array]::Copy($buffer, 0, $lastBuffer, 0, $bytesRead)
            }

            If($first)
            {
                $ContentStream = New-Object System.IO.MemoryStream

                # Add an empty file.
                $fileInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
                $fileInfo.ContentStream = $ContentStream
                $fileInfo.Url = $UniqueFileName
                $fileInfo.Overwrite = $true
                $Upload = $Docs.RootFolder.Files.Add($fileInfo)
                $ctx.Load($Upload)

                # Start upload by uploading the first slice.

                $s = [System.IO.MemoryStream]::new($buffer) 

                # Call the start upload method on the first slice.
                $BytesUploaded = $Upload.StartUpload($UploadId, $s)
                $ctx.ExecuteQuery()

                # fileoffset is the pointer where the next slice will be added.
                $fileoffset = $BytesUploaded.Value

                # You can only start the upload once.
                $first = $false

            }
            Else
            {
                # Get a reference to your file.
                $Upload = $ctx.Web.GetFileByServerRelativeUrl($Docs.RootFolder.ServerRelativeUrl + [System.IO.Path]::AltDirectorySeparatorChar + $UniqueFileName);

                If($last) {

                    # Is this the last slice of data?
                    $s = [System.IO.MemoryStream]::new($lastBuffer)


                    # End sliced upload by calling FinishUpload.
                    $Upload = $Upload.FinishUpload($UploadId, $fileoffset, $s)
                    $ctx.ExecuteQuery()


                    Write-Host "File upload complete"
                    # Return the file object for the uploaded file.
                    return $Upload

                }
                else {

                    $s = [System.IO.MemoryStream]::new($buffer)

                    # Continue sliced upload.
                    $BytesUploaded = $Upload.ContinueUpload($UploadId, $fileoffset, $s)
                    $ctx.ExecuteQuery()

                    # Update fileoffset for the next slice.
                    $fileoffset = $BytesUploaded.Value

                }
            }

        }
    }
    Catch 
    {
        Write-Host $_.Exception.Message -ForegroundColor Red
    }
    Finally 
    {

        if ($Fs -ne $null)
        {
            $Fs.Dispose()
        }

    }
}

大文件上载更新:

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$SiteURL="https://tenant.sharepoint.com/sites/dev"
$User = "user@tenant.onmicrosoft.com"  
$Password = '*********************' 
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,(ConvertTo-SecureString $Password -AsPlainText -Force))
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$ctx.Credentials = $Credentials

$fileChunkSizeInMB = 9

# Each sliced upload requires a unique ID.
$UploadId = [GUID]::NewGuid()

# Get the name of the file.
$UniqueFileName = [System.IO.Path]::GetFileName("D:\\Yoyo.wmv")


# Get the folder to upload into. 
$Docs = $ctx.Web.Lists.GetByTitle("Documents")
$ctx.Load($Docs)
$ctx.Load($Docs.RootFolder)
$ctx.ExecuteQuery()

# Get the information about the folder that will hold the file.
$ServerRelativeUrlOfRootFolder = $Docs.RootFolder.ServerRelativeUrl

# File object.
[Microsoft.SharePoint.Client.File] $upload


# Calculate block size in bytes.
$BlockSize = $fileChunkSizeInMB * 1024 * 1024


# Get the size of the file.
$FileSize = (Get-Item $fileName).length

if ($FileSize -le $BlockSize)
{
    # Use regular approach.
    $FileStream = New-Object IO.FileStream($fileName,[System.IO.FileMode]::Open)
    $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
    $FileCreationInfo.Overwrite = $true
    $FileCreationInfo.ContentStream = $FileStream
    $FileCreationInfo.URL = $UniqueFileName
    $Upload = $Docs.RootFolder.Files.Add($FileCreationInfo)
    $ctx.Load($Upload)
    $ctx.ExecuteQuery()
    return $Upload
}
else
{

    # Use large file upload approach.
    $BytesUploaded = $null

    $Fs = $null

    Try {

        $Fs = [System.IO.File]::Open($fileName, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::ReadWrite)

        $br = New-Object System.IO.BinaryReader($Fs)

        $buffer = New-Object System.Byte[]($BlockSize)
        $lastBuffer = $null
        $fileoffset = 0
        $totalBytesRead = 0
        $bytesRead
        $first = $true
        $last = $false

        # Read data from file system in blocks. 
        while(($bytesRead = $br.Read($buffer, 0, $buffer.Length)) -gt 0) {

            $totalBytesRead = $totalBytesRead + $bytesRead

            # You've reached the end of the file.
            if($totalBytesRead -eq $FileSize) {
                $last = $true
                # Copy to a new buffer that has the correct size.
                $lastBuffer = New-Object System.Byte[]($bytesRead)
                [array]::Copy($buffer, 0, $lastBuffer, 0, $bytesRead)
            }

            If($first)
            {
                $ContentStream = New-Object System.IO.MemoryStream

                # Add an empty file.
                $fileInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
                $fileInfo.ContentStream = $ContentStream
                $fileInfo.Url = $UniqueFileName
                $fileInfo.Overwrite = $true
                $Upload = $Docs.RootFolder.Files.Add($fileInfo)
                $ctx.Load($Upload)

                # Start upload by uploading the first slice.

                $s = [System.IO.MemoryStream]::new($buffer) 

                # Call the start upload method on the first slice.
                $BytesUploaded = $Upload.StartUpload($UploadId, $s)
                $ctx.ExecuteQuery()

                # fileoffset is the pointer where the next slice will be added.
                $fileoffset = $BytesUploaded.Value

                # You can only start the upload once.
                $first = $false

            }
            Else
            {
                # Get a reference to your file.
                $Upload = $ctx.Web.GetFileByServerRelativeUrl($Docs.RootFolder.ServerRelativeUrl + [System.IO.Path]::AltDirectorySeparatorChar + $UniqueFileName);

                If($last) {

                    # Is this the last slice of data?
                    $s = [System.IO.MemoryStream]::new($lastBuffer)


                    # End sliced upload by calling FinishUpload.
                    $Upload = $Upload.FinishUpload($UploadId, $fileoffset, $s)
                    $ctx.ExecuteQuery()


                    Write-Host "File upload complete"
                    # Return the file object for the uploaded file.
                    return $Upload

                }
                else {

                    $s = [System.IO.MemoryStream]::new($buffer)

                    # Continue sliced upload.
                    $BytesUploaded = $Upload.ContinueUpload($UploadId, $fileoffset, $s)
                    $ctx.ExecuteQuery()

                    # Update fileoffset for the next slice.
                    $fileoffset = $BytesUploaded.Value

                }
            }

        }
    }
    Catch 
    {
        Write-Host $_.Exception.Message -ForegroundColor Red
    }
    Finally 
    {

        if ($Fs -ne $null)
        {
            $Fs.Dispose()
        }

    }
}

谢谢。您可以修改此脚本以在脚本中输入用户名和密码吗?您遇到了什么错误?用户名和密码将来自Get Credential。如果您想嵌入硬代码用户名和密码,我将稍后更改,请检查。@KivenWanda,请同时将url中的租户名称、站点名称以及本地文件路径、库子文件夹相对url替换为您在上面的脚本中的url。此脚本可以上载大文件吗?(2Gb以上)?文件创建信息不支持大文件,如果要使用csom powershell上载大文件,请查看此演示:文章中的代码片段演示了如何根据原始问题将文件上载到库中的子文件夹中。谢谢。您可以修改此脚本以在脚本中输入用户名和密码吗?用户名和密码将来自Get Credential。如果您想嵌入硬代码用户名和密码,我将稍后更改,请检查。@KivenWanda,请同时将url中的租户名称、站点名称以及本地文件路径、库子文件夹相对url替换为您在上面的脚本中的url。此脚本可以上载大文件吗?(更多2Gb)?FileCreationInformation不支持大文件,如果您想使用csom powershell上载大文件,请查看此演示:文章中的代码片段演示了如何根据原始问题将文件上载到库中的子文件夹。请尝试修改后的代码,如果有任何详细信息,请发回。请尝试修改后的代码,如果有任何更详细的信息,请发回。