【问题标题】:Trying to upload files to subfolder in Sharepoint Online via Powershell尝试通过 Powershell 将文件上传到 Sharepoint Online 中的子文件夹
【发布时间】:2016-08-05 04:13:03
【问题描述】:

我是 Powershell 的新手,我正在尝试将文件从本地文件夹上传到在线 Sharepoint。我似乎将文件放入库中,但没有放入我想要它们的第二个子文件夹中。

到目前为止的脚本:

#Specify tenant admin and site URL
$User = "admin@contoso.no"
$SiteURL = "https://contoso.sharepoint.com"
$Folder = "E:\LocalFolder"
$DocLibName = "Libraryname"
$FolderName = "Folder/SubFolder"

#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = "password"  | ConvertTo-SecureString -AsPlainText -Force

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
$Context.ExecuteQuery()

#Retrieve folder
$FolderToBindTo = $List.RootFolder.Folders
$Context.Load($FolderToBindTo)
$Context.ExecuteQuery()
$FolderToUpload = $FolderToBindTo | Where {$_.Name -eq $FolderName}

#Upload file
Foreach ($File in (dir $Folder -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 = $FolderToUpload.Files.Add($FileCreationInfo)
    $Context.Load($Upload)
    $Context.ExecuteQuery()
}

如果我使用 &FolderName = "Folder",则脚本运行良好。但是我该怎么做才能将文件放入子文件夹中?如果我将文件路径设置为 FolderName 它不起作用。我收到以下错误:

You cannot call a method on a null-valued expression.

+ $Upload = $FolderToUpload.Files.Add($FileCreationInfo)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Cannot find an overload for "Load" and the argument count: "1".

+ $Context.Load($Upload)
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
    enter code here

被困在这几天:(

【问题讨论】:

    标签: powershell sharepoint powershell-4.0 sharepoint-online


    【解决方案1】:

    错误You cannot call a method on a null-valued expression. 发生是因为$FolderToUpload 对象未在以下行初始化:

    $FolderToUpload = $FolderToBindTo | Where {$_.Name -eq $FolderName}
    

    $FolderName 对象指向子文件夹名称时

    关键是$List.RootFolder.Folders 仅返回位于列表或库下一层 的文件夹,因此无法以这种方式引用子文件夹。

    相反,您可以考虑使用以下选项来引用子文件夹以添加文件。

    使用FileCreationInformation.Url 属性

    使用FileCreationInformation.Url property 指定上传文件的文件夹网址。

    #Retrieve list
    $List = $Context.Web.Lists.GetByTitle($DocLibName)
    $Context.Load($List.RootFolder)
    $Context.ExecuteQuery()
    
    
    #Upload file(s)
    Foreach ($File in (dir $Folder -File))
    {
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName)
        $FileCreationInfo.URL = $List.RootFolder.ServerRelativeUrl + "/" + $FolderName + "/" + $File.Name
        $UploadFile = $List.RootFolder.Files.Add($FileCreationInfo)
        $Context.Load($UploadFile)
        $Context.ExecuteQuery()
    }
    

    使用Web.GetFolderByServerRelativeUrl方法

    使用Web.GetFolderByServerRelativeUrl method 检索必须上传文件的文件夹:

    #Retrieve list
    $List = $Context.Web.Lists.GetByTitle($DocLibName)
    $Context.Load($List.RootFolder)
    $Context.ExecuteQuery()
    
    $TargetFolder = $Context.Web.GetFolderByServerRelativeUrl($List.RootFolder.ServerRelativeUrl + "/" + $FolderName);
    
    
    #Upload file(s)
    Foreach ($File in (dir $Folder -File))
    {
        $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
        $FileCreationInfo.Overwrite = $true
        $FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName)
        $FileCreationInfo.URL = $File.Name
        $UploadFile = $TargetFolder.Files.Add($FileCreationInfo)
        $Context.Load($UploadFile)
        $Context.ExecuteQuery()
    }
    

    【讨论】:

      【解决方案2】:

      您还可以使用 Office 365 导入服务将文件批量上传到在线共享点,而不是使用 CSOM 脚本。

      这使用了新的 Sharepoint API,它使用可扩展的 Azure 基础架构,提供更快的速度而没有任何限制。

      https://support.office.com/en-us/article/Use-network-upload-to-import-SharePoint-data-to-Office-365-ed4a43b7-c4e3-45c8-94c8-998153407b8a

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-22
        • 2021-09-29
        • 2016-04-29
        • 1970-01-01
        相关资源
        最近更新 更多