无法在sharepoint上获取以前版本的文件 [英] Cannot get previous version of a file on sharepoint
问题描述
我尝试在sharepoint上下载文件的历史版本但总是得到"远程服务器返回错误:(403 )禁止。"运行到"client.OpenRead(curFileVerUrl);"时出错如下。有人可以提供建议吗?谢谢。
I tried to download history versions of a file on sharepoint but always get "The remote server returned an error: (403) Forbidden." error when run to "client.OpenRead(curFileVerUrl);" as following. Can anybody advise on it? Thank you.
使用(var context = new ClientContext(" https://test.sharepoint.com/CorpSrv/TestSite/"))
            {
              context.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(strUser,password);
              string fileUrl =" https://test.sharepoint.com/CorpSrv/TestSite/TestDocLib/123.xlsx" ;;
              Uri fileuri = new Uri(fileUrl);
              Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(fileuri.AbsolutePath);
              context.Load(file);
              context.ExecuteQuery();
$
              FileVersionCollection版本= file.Versions;
              context.Load(版本);
              context.ExecuteQuery();
$
              foreach(file.Versions中的Microsoft.SharePoint.Client.FileVersion _version)
              {
$
                  Uri filename = new Uri(context.Url + _version.Url);
                 使用(var client = new WebClient())
                  {
                      client.Credentials = new NetworkCredential(strUser,password);
                      string curFileVerUrl = context.Url + _version.Url;
                      System.IO.Stream data = client.OpenRead(curFileVerUrl);
                  }¥b $ b               }¥b $ b           }
using (var context = new ClientContext("https://test.sharepoint.com/CorpSrv/TestSite/"))
{
context.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(strUser, password);
string fileUrl = "https://test.sharepoint.com/CorpSrv/TestSite/TestDocLib/123.xlsx";
Uri fileuri = new Uri(fileUrl);
Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(fileuri.AbsolutePath);
context.Load(file);
context.ExecuteQuery();
FileVersionCollection versions = file.Versions;
context.Load(versions);
context.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.FileVersion _version in file.Versions)
{
Uri filename = new Uri(context.Url + _version.Url);
using (var client = new WebClient())
{
client.Credentials = new NetworkCredential(strUser, password);
string curFileVerUrl = context.Url + _version.Url;
System.IO.Stream data = client.OpenRead(curFileVerUrl);
}
}
}
推荐答案
尝试检查您的密码是否已设置正确,下面的代码适用于我:
using (var context = new ClientContext("https://xxx.sharepoint.com/sites/dev"))
{
Console.ForegroundColor = ConsoleColor.Green;
string password = "pw";
SecureString sec_pass = new SecureString();
Array.ForEach(password.ToArray(), sec_pass.AppendChar);
sec_pass.MakeReadOnly();
context.Credentials = new SharePointOnlineCredentials("user@xxx.onmicrosoft.com", sec_pass);
string fileUrl = "https://o365e3w15.sharepoint.com/sites/dev/Shared%20Documents/Document.docx";
Uri fileuri = new Uri(fileUrl);
Microsoft.SharePoint.Client.File file = context.Web.GetFileByServerRelativeUrl(fileuri.AbsolutePath);
context.Load(file);
context.ExecuteQuery();
FileVersionCollection versions = file.Versions;
context.Load(versions);
context.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.FileVersion _version in file.Versions)
{
Console.WriteLine(_version.VersionLabel + _version.Url);
}
Console.ReadKey();
}
最好的问候,
Lee
这篇关于无法在sharepoint上获取以前版本的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!