我的目标是:获取SharePoint 2010库中文件夹内的项目。我努力从使用CSOM和PowerShell的SharePoint 2010库中获取一些项目。 我尝试了三种不同的方法,我发现在互联网上,但仍然没有成功。此外,微软的文档在这方面真的很不错,希望有人能帮助我。所以在这里,我们去:使用PowerShell V2和CSOM下载SharePoint 2010库项目
1.Method一个
[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();
随着一个方法,我得到这个错误:
方法调用失败,因为[System.Net.ConnectStream]不包含名为“CopyTo”的方法。
+ $ fileInfo.Stream.CopyTo < < < <($ writeStream);
+ CategoryInfo:InvalidOperation:(复制:字符串)[],的RuntimeException
+ FullyQualifiedErrorId:MethodNotFound
的[System.Net.ConnectStream]找不到方法CopyTo
我lookign关于信息这一点,在System.Net命名空间中,并在 “Microsoft.SharePoint.Client.FileInformation” 类,但没有成功:(
2.Method乙
$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()
方法乙不给我一个错误,但在下载项目代替,这使得在目标文件夹中的空文件。所以这种方法不会下载项目,只是创建新文件。
3.MethodÇ
$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()
我不知道,如果方法Ç属于CSOM或内置的服务器端客户端SharPoint,如果是这样,请让我知道。这是我得到的错误:
方法调用失败,因为[Microsoft.SharePoint.Client.File]不包含名为'OpenBinary'的方法。 在C:\用户\管理员\桌面\ SharePointOnPremisesBackUp \ SharePointOnPremisesBackUp.ps1:77字符:31
+ $二进制= $ file.OpenBinary < < < <()
+ CategoryInfo:InvalidOperation:(OpenBinary:字符串) ],RuntimeException的
+ FullyQualifiedErrorId:MethodNotFound
而这里的PowerShell找不到OpenBinary()在Microsoft.SharePoint.Client.File方法有将近不是此方法的信息。
下面是完整的功能,我想使用:
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的工作室
- 的PowerShell V2与CSOM
- SharePoint 2010 OnPremises
如果您有任何有用的解决方案,参考资料,文档或教程,请告诉我。提前致谢。