【问题标题】:Download SharePoint 2010 Library Items using PowerShell V2 with CSOM使用 PowerShell V2 和 CSOM 下载 SharePoint 2010 库项目
【发布时间】:2017-02-08 01:54:42
【问题描述】:

我的目标是:在 SharePoint 2010 库的文件夹中获取项目。我正在努力尝试使用带有 PowerShell 的 CSOM 从 SharePoint 2010 库中获取一些项目。 我尝试了三种在互联网上找到的不同方法,但仍然没有成功。微软的文档在这方面也很糟糕,希望有人能帮助我。所以我们开始:

1.方法 A

            [Microsoft.SharePoint.Client.FileInformation]$fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx, $file.ServerRelativeUrl);
            [System.IO.FileStream]$writeStream = [System.IO.File]::Open("$($libraryTargetPath)\$($file.Name)", [System.IO.FileMode]::Create);
            $fileInfo.Stream.CopyTo($writeStream);
            $writeStream.Close();

使用 A 方法我得到这个错误:

方法调用失败,因为 [System.Net.ConnectStream] 不包含名为“CopyTo”的方法。
+ $fileInfo.Stream.CopyTo + CategoryInfo : InvalidOperation: (CopyTo:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

[System.Net.ConnectStream] 找不到方法 CopyTo
我在 System.Net 命名空间和“Microsoft.SharePoint.Client.FileInformation”类中查找了有关此的信息,但没有成功:(

2.方法 B

            $binary = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx, $file.ServerRelativeUrl)
            $Action = [System.IO.FileMode]::Create
            $new = "$($libraryTargetPath)\$($file.Name)"
            $stream = New-Object System.IO.FileStream $new, $Action
            $writer = New-Object System.IO.BinaryWriter($stream)
            $writer.write($binary)
            $writer.Close()

方法 B 不会给我一个错误,但不是下载项目,而是在目标文件夹中生成空文件。所以这个方法不是下载项目,只是制作新文件。

3.方法 C

            $binary = $file.OpenBinary()
            $stream = New-Object System.IO.FileStream("$($libraryTargetPath)\$($file.Name)"), Create
            $writer = New-Object System.IO.BinaryWriter($stream)
            $writer.write($binary)
            $writer.Close()

我不确定方法 C 是属于 CSOM 还是属于 SharPoint 内置的服务器端客户端,如果是,请告诉我。这是我得到的错误:

方法调用失败,因为 [Microsoft.SharePoint.Client.File] 不包含名为“OpenBinary”的方法。 在 C:\Users\Administrator\Desktop\SharePointOnPremisesBackUp\SharePointOnPremisesBackUp.ps1:77 char:31
+ $binary = $file.OpenBinary + CategoryInfo : InvalidOperation: (OpenBinary:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound

这里 PowerShell 在 Microsoft.SharePoint.Client.File 中找不到 OpenBinary() 方法,并且几乎没有关于此方法的信息。

这是我尝试使用的完整功能:

function GetDocumentLibs ($ctx, $web)
{
    Function  IterateFoldersRecursively([Microsoft.SharePoint.Client.Folder]$folder, [Microsoft.SharePoint.Client.ClientContext]$ctx)
    {
      # make sure that the "Web.Context.Url" is the current web url
      if ($web.Context.Url.StartsWith($SiteCollectionUrl) -eq $true)
      {
        $files = $folder.Files
        $ctx.Load($folder.Files)
        $ctx.Load($folder.Folders)
        $ctx.ExecuteQuery()

        foreach ($subFolder in $folder.Folders)
        {
            IterateFoldersRecursively $subFolder $ctx
        }

        # Check if folder Exist and Skip

        $libraryTargetPath = "$($TargetPath)\$($folder.ServerRelativeUrl.Replace('/', '\'))"
        New-Item -Path $libraryTargetPath -ItemType Directory -Force

            foreach ($file in $files)
            {
            # Method 1
            [Microsoft.SharePoint.Client.FileInformation]$fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx, $file.ServerRelativeUrl);
            [System.IO.FileStream]$writeStream = [System.IO.File]::Open("$($libraryTargetPath)\$($file.Name)", [System.IO.FileMode]::Create);
            $fileInfo.Stream.CopyTo($writeStream)
            $writeStream.Close()

            # Method 2
            $binary = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($ctx, $file.ServerRelativeUrl)
            $Action = [System.IO.FileMode]::Create
            $new = "$($libraryTargetPath)\$($file.Name)"
            $stream = New-Object System.IO.FileStream $new, $Action
            $writer = New-Object System.IO.BinaryWriter($stream)
            $writer.write($binary)
            $writer.Close()

            # Method 3
            $binary = $file.OpenBinary()
            $stream = New-Object System.IO.FileStream("$($libraryTargetPath)\$($file.Name)"), Create
            $writer = New-Object System.IO.BinaryWriter($stream)
            $writer.write($binary)
            $writer.Close()

            # delete folder
            }
       }
    }
    $folder = $web.GetFolderByServerRelativeUrl($web.ServerRelativeUrl)
    $ctx.Load($folder)
    $ctx.ExecuteQuery()

    IterateFoldersRecursively $folder $ctx
}                                               

我正在使用的工具:

  1. Sapien 的 PowerShell 工作室
  2. 带有 CSOM 的 PowerShell V2
  3. SharePoint 2010 本地

如果您有任何有用的解决方案、参考、文档或教程,请告诉我。提前致谢。

【问题讨论】:

    标签: sharepoint-2010 powershell-2.0 csom powershell-studio


    【解决方案1】:

    经过大量研究,我找到了解决方案并决定使用此方法:

    function GetDocumentLibs ($ctx, $web)
    {
        $site = $ctx.Site
        $ctx.Load($site)
        $ctx.ExecuteQuery()
        $siteUrl = $site.Url
    
        Function IterateFoldersRecursively([Microsoft.SharePoint.Client.Folder]$folder, [Microsoft.SharePoint.Client.ClientContext]$ctx)
        {
            if ($web.Context.Url.StartsWith($SiteCollectionUrl) -eq $true)
            {
                $files = $folder.Files
                $ctx.Load($folder.Files)
                $ctx.Load($folder.Folders)
                $ctx.ExecuteQuery()
    
                foreach ($subFolder in $folder.Folders)
                {
                    IterateFoldersRecursively $subFolder $ctx
                }
    
                $targetPath = "$($TargetPath)\$($folder.ServerRelativeUrl.Replace('/', '\'))"
                New-Item -Path $targetPath -ItemType Directory -Force
    
                foreach ($file in $files)
                {
                    $client = new-object System.Net.WebClient
                    $client.UseDefaultCredentials = $true
                    $client.DownloadFile("$($siteUrl)$($file.ServerRelativeUrl)", "$($targetPath)\$($file.Name)")
                }
            }
        }
        $folder = $web.GetFolderByServerRelativeUrl($web.ServerRelativeUrl)
        $ctx.Load($folder)
        $ctx.ExecuteQuery()
    
        IterateFoldersRecursively $folder $ctx
    }
    

    记得实现一些异常处理。 我希望这对遇到同样问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2017-11-23
      • 2022-07-14
      • 2023-03-09
      • 2023-03-19
      • 2018-01-03
      • 2020-09-22
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      相关资源
      最近更新 更多