使用 PowerShell V2 和 CSOM 下载 SharePoint 2010 库项目
Download SharePoint 2010 Library Items using PowerShell V2 with CSOM
我的目标是:获取 SharePoint 2010 库中文件夹中的项目。我正在尝试使用 CSOM 和 PowerShell 从 SharePoint 2010 库中获取一些项目。
我尝试了三种在互联网上找到的不同方法,但仍然没有成功。另外微软的文档在这方面真的很糟糕,希望有人能帮助我。所以我们开始吧:
1.Method 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 <<<< ($writeStream);
+ CategoryInfo:InvalidOperation:(CopyTo:String)[],RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
[System.Net.ConnectStream]找不到方法CopyTo
我在 System.Net 命名空间和 "Microsoft.SharePoint.Client.FileInformation" class 中查看有关此的信息,但没有成功:(
2.Method 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.Method 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
+ $二进制= $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
}
我使用的工具:
- Sapien 的 PowerShell Studio
- 带有 CSOM 的 PowerShell V2
- SharePoint 2010 内部部署
如果您有任何有用的解决方案、参考、文档或教程,请告诉我。提前致谢。
经过大量研究,我找到了解决方案并决定使用此方法:
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
}
记得实现一些异常处理。
我希望这对遇到同样问题的人有所帮助。
我的目标是:获取 SharePoint 2010 库中文件夹中的项目。我正在尝试使用 CSOM 和 PowerShell 从 SharePoint 2010 库中获取一些项目。 我尝试了三种在互联网上找到的不同方法,但仍然没有成功。另外微软的文档在这方面真的很糟糕,希望有人能帮助我。所以我们开始吧:
1.Method 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 <<<< ($writeStream);
+ CategoryInfo:InvalidOperation:(CopyTo:String)[],RuntimeException
+ FullyQualifiedErrorId : MethodNotFound
[System.Net.ConnectStream]找不到方法CopyTo
我在 System.Net 命名空间和 "Microsoft.SharePoint.Client.FileInformation" class 中查看有关此的信息,但没有成功:(
2.Method 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.Method 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
+ $二进制= $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
}
我使用的工具:
- Sapien 的 PowerShell Studio
- 带有 CSOM 的 PowerShell V2
- SharePoint 2010 内部部署
如果您有任何有用的解决方案、参考、文档或教程,请告诉我。提前致谢。
经过大量研究,我找到了解决方案并决定使用此方法:
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
}
记得实现一些异常处理。 我希望这对遇到同样问题的人有所帮助。