简体   繁体   English

使用 PowerShell V2 和 CSOM 下载 SharePoint 2010 库项目

[英]Download SharePoint 2010 Library Items using PowerShell V2 with CSOM

my goal is: Get the items within the folders in a SharePoint 2010 library .我的目标是:获取 SharePoint 2010 库中文件夹内的项目 I'm struggling trying to get some Items from a SharePoint 2010 Library using CSOM with PowerShell.我正在努力尝试使用 CSOM 和 PowerShell 从 SharePoint 2010 库中获取一些项目。 I tried three different methods that i found in the internet but still without success.我尝试了三种在互联网上找到的不同方法,但仍然没有成功。 Also the Microsoft's Documentation is really sh*t in this aspect, hope somebody can help me.另外微软的文档在这方面真的很糟糕,希望有人能帮助我。 So here we go:所以我们开始:

1.Method A 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();

With the A method I get this error:使用A方法,我收到此错误:

Method invocation failed because [System.Net.ConnectStream] doesn't contain a method named 'CopyTo'.方法调用失败,因为 [System.Net.ConnectStream] 不包含名为“CopyTo”的方法。
+ $fileInfo.Stream.CopyTo <<<< ($writeStream); + $fileInfo.Stream.CopyTo <<<< ($writeStream);
+ CategoryInfo : InvalidOperation: (CopyTo:String) [], RuntimeException + CategoryInfo : InvalidOperation: (CopyTo:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound + FullQualifiedErrorId : MethodNotFound

The [System.Net.ConnectStream] can't find the method CopyTo [System.Net.ConnectStream] 找不到方法 CopyTo
I was lookign information about this, in the System.Net namespace and in the "Microsoft.SharePoint.Client.FileInformation" class but without success :(我正在 System.Net 命名空间和“Microsoft.SharePoint.Client.FileInformation”类中查找有关此的信息,但没有成功:(

2.Method B 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()

Method B don't give me a error, but in stead of downloading the Items, it makes empty files in the destination folder.方法B不会给我一个错误,但它不是下载项目,而是在目标文件夹中生成空文件。 So this method isn't downloading the items, just making new files.所以这种方法不是下载项目,只是制作新文件。

3.Method C 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()

I'm not sure if method C belongs to CSOM or to the SharPoint built-in Server side client, if it's so please let me know.我不确定方法C 是属于 CSOM 还是属于 SharPoint 内置服务器端客户端,如果是这样,请告诉我。 This is the error i'm getting:这是我得到的错误:

Method invocation failed because [Microsoft.SharePoint.Client.File] doesn't contain a method named 'OpenBinary'.方法调用失败,因为 [Microsoft.SharePoint.Client.File] 不包含名为“OpenBinary”的方法。 At C:\\Users\\Administrator\\Desktop\\SharePointOnPremisesBackUp\\SharePointOnPremisesBackUp.ps1:77 char:31在 C:\\Users\\Administrator\\Desktop\\SharePointOnPremisesBackUp\\SharePointOnPremisesBackUp.ps1:77 char:31
+ $binary = $file.OpenBinary <<<< () + $binary = $file.OpenBinary <<<< ()
+ CategoryInfo : InvalidOperation: (OpenBinary:String) [], RuntimeException + CategoryInfo : InvalidOperation: (OpenBinary:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound + FullQualifiedErrorId : MethodNotFound

And here PowerShell can't find the OpenBinary() method in Microsoft.SharePoint.Client.File and there is nearly not information about this method.在这里,PowerShell 在Microsoft.SharePoint.Client.File 中找不到OpenBinary()方法,并且几乎没有关于此方法的信息。

Here is the complete function I'm trying to use:这是我尝试使用的完整功能:

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
}                                               

the tools I'm using:我正在使用的工具:

  1. Sapien's PowerShell Studio Sapien 的 PowerShell 工作室
  2. PowerShell V2 with CSOM带有 CSOM 的 PowerShell V2
  3. SharePoint 2010 OnPremises SharePoint 2010 本地

Please if you have any Solution, Reference, Documentation or tutorial that can be useful tell me.如果您有任何有用的解决方案、参考资料、文档或教程,请告诉我。 Thanks in advance.提前致谢。

After a lot of research i found a solution and decide to use this method:经过大量研究,我找到了一个解决方案并决定使用这种方法:

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
}

Remember to implement some exception handling.记住要实现一些异常处理。 I hope this is helpful for somebody with the same problem.我希望这对有同样问题的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM