【问题标题】:How to download FileVersion content from Sharepoint Online using CSOM如何使用 CSOM 从 Sharepoint Online 下载 FileVersion 内容
【发布时间】:2017-11-16 10:53:29
【问题描述】:

问题

如何读取特定版本的文件内容?

我尝试过的

  1. 我试过了:

    var webClient = new WebClient() {Credentials = Context.Credentials })
    webClient.DownloadString($"{Context.Url}{fileVersion.Url}");
    

    Credentials 为空。我也尝试过UseDefaultCredential = true,但在这两种情况下我都被 403 禁止:

    System.Net.WebException: '远程服务器返回错误:(403) Forbidden。'

  2. 我也试过了:

    File.OpenBinaryDirect(sharepoint.Context, fileVersion.Url)
    //fileVersion.Url == "_vti_history/512/ListName/filename.xml"
    
    > Specified argument was out of the range of valid values.
    > Parameter name: serverRelativeUrl
    
  3. 也以/开头:

     File.OpenBinaryDirect(sharepoint.Context, "/" + fileVersion.Url)
    

    System.Net.WebException: '远程服务器返回错误:(404) Not Found.'

如何创建 ClientContext:

我使用 sharepoint PnP 创建和验证我的 ClientContext:

var authentication = new OfficeDevPnP.Core.AuthenticationManager();
Context = authentication.GetWebLoginClientContext(siteUrl);

我可以检索 FileVersion 列表:

var items = list.GetItems(camlQuery);
Context.Load(items);
Context.ExecuteQuery();
foreach (ListItem item in items)
{
    sharepoint.Context.Load(item.Versions);
    sharepoint.Context.Load(item.File.Versions);
    sharepoint.Context.Load(item.File);

    sharepoint.Context.ExecuteQuery();

    foreach (var fileVersion in item.File.Versions)
    {
         ReadFile(fileVersion);
    }
}

这是我的库的版本:

<package id="Microsoft.SharePointOnline.CSOM" version="16.1.7018.1200" targetFramework="net461" />
<package id="SharePointPnP.IdentityModel.Extensions" version="1.2.2" targetFramework="net461" />
<package id="SharePointPnPCoreOnline" version="2.19.1710.2" targetFramework="net461" />

【问题讨论】:

    标签: c# sharepoint sharepoint-online csom


    【解决方案1】:

    我通过从 ClientContext 窃取 cookie 破解了 webclient 身份验证:

    var authentication = new OfficeDevPnP.Core.AuthenticationManager();
    context = authentication.GetWebLoginClientContext(siteUrl);
    webClient = new WebClient();
    context.ExecutingWebRequest += StealCookieOnExecutingWebRequest;
    void StealCookieOnExecutingWebRequest(object sender, WebRequestEventArgs e)
    {
        var cookies = e.WebRequestExecutor.WebRequest.CookieContainer;
        webClient.Headers[HttpRequestHeader.Cookie] = cookies.GetCookieHeader(new Uri(SiteUrl));
        context.ExecutingWebRequest -= StealCookieOnExecutingWebRequest;
    }
    

    我不会将此标记为答案,因为可能会有更好的解决方案。

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 2019-04-12
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2020-09-22
      • 2013-09-22
      • 1970-01-01
      相关资源
      最近更新 更多