我正在使用Microsoft.SharePoint.Client
DLL在我的MVC C#
项目中上传文件。
现在我使用下面的代码下载版本文件,但是找不到文件错误。
using (var clientContext = new ClientContext(_SharePointSiteURL))
{
var onlineCredentials = new SharePointOnlineCredentials(_SharePointSiteUser, securePassword);
clientContext.RequestTimeout = 10000000;
clientContext.Credentials = onlineCredentials;
var spFile = clientContext.Web.GetFileByServerRelativeUrl(System.IO.Path.Combine("/Shared Documents/ABC", "Test.pdf"));
clientContext.Load(spFile);
FileVersionCollection versions = spFile.Versions;
clientContext.Load(versions);
var oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.ExecuteQuery();
if (oldVersions != null)
{
foreach (Microsoft.SharePoint.Client.FileVersion _version in oldVersions)
{
if (_version.VersionLabel == "2.0")
{
clientContext.Load(_version, item => item.Url);
clientContext.ExecuteQuery();
string vURL = _version.Url;
Microsoft.SharePoint.Client.File oldFile = clientContext.Web.GetFileByServerRelativeUrl("/" + vURL);
clientContext.Load(oldFile);
clientContext.Load(oldFile, item => item.ListItemAllFields);
ClientResult<System.IO.Stream> data = oldFile.OpenBinaryStream();
clientContext.ExecuteQuery();
using (System.IO.MemoryStream mStream = new System.IO.MemoryStream())
{
if (data != null)
{
data.Value.CopyTo(mStream);
fileBytes = mStream.ToArray();
}
}
}
}
}
}
答案 0 :(得分:0)
我们可以使用以下代码从SharePoint Online使用CSOM(C#)下载文件版本。
string _SharePointSiteURL = @"https://lz.sharepoint.com/sites/lz";
var _SharePointSiteUser = "lz@lz.onmicrosoft.com";
var password = "Password";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
using (var clientContext = new ClientContext(_SharePointSiteURL))
{
var onlineCredentials = new SharePointOnlineCredentials(_SharePointSiteUser, securePassword);
clientContext.RequestTimeout = 10000000;
clientContext.Credentials = onlineCredentials;
Web web = clientContext.Web;
clientContext.Load(web, website => website.ServerRelativeUrl, website => website.Url);
clientContext.ExecuteQuery();
var spFile = clientContext.Web.GetFileByServerRelativeUrl((web.ServerRelativeUrl.EndsWith("/") ? web.ServerRelativeUrl : web.ServerRelativeUrl + "/") + "Shared Documents/ABC/Test.pdf");
clientContext.Load(spFile);
FileVersionCollection versions = spFile.Versions;
clientContext.Load(versions);
var oldVersions = clientContext.LoadQuery(versions.Where(v => v != null));
clientContext.ExecuteQuery();
if (oldVersions != null)
{
foreach (Microsoft.SharePoint.Client.FileVersion _version in oldVersions)
{
if (_version.VersionLabel == "2.0")
{
var localPath = @"c:\test\";
if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
using (var wc = new System.Net.WebClient())
{
wc.Credentials = onlineCredentials;
wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC)";
wc.DownloadFile(web.Url + "/" + _version.Url, localPath+"Test.pdf");
}
}
}
}
}
答案 1 :(得分:0)
试试这段代码:
var onlineCredentials = new SharePointOnlineCredentials(_SharePointSiteUser, securePassword);
clientContext.RequestTimeout = 10000000;
clientContext.Credentials = onlineCredentials;
Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByServerRelativeUrl(_SharePointSiteURL + "/Shared Documents/ABC/Test.pdf");
FileVersionCollection fileVersions = file.Versions;
clientContext.Load(file);
clientContext.Load(fileVersions);
clientContext.ExecuteQuery();
// Download all versions of specific file as individual docs
int index = 0;
foreach (var version in fileVersions)
{
if (version.VersionLabel == "2.0")
{
var str = version.OpenBinaryStream();
clientContext.ExecuteQuery();
string filename = string.Format("d:\\downloaded\\doc-{0}.docx", index);
using (var fs = new FileStream(filename, FileMode.OpenOrCreate))
{
str.Value.CopyTo(fs);
}
index++;
}
}
确保您使用的是最新版本的CSOM或更改2017年9月更新后的版本。
建议您使用Nuget获取最新的SharePoint在线csom包。