【问题标题】:Unable to use SaveBinaryDirect with Sharepoint online always return 403无法在线使用带有 Sharepoint 的 SaveBinaryDirect 总是返回 403
【发布时间】:2018-04-29 07:21:54
【问题描述】:

我是 SharePoint 新手,

我正在尝试通过 c# 代码在文档库中上传图片。

这是我的代码

string webUrl = "https://mysharepointesite.sharepoint.com/";



  string targetSite = webUrl;
  using (ClientContext ctx = ClaimClientContext.GetAuthenticatedContext(targetSite))
  {
    if (ctx != null)
    {
      var lib = ctx.Web.Lists.GetByTitle("mesdocuments");
      ctx.Load(lib);
      ctx.Load(lib.RootFolder);
      ctx.ExecuteQuery();
      string sourceUrl = "C:\\temp\\picture.jpg";          
      using (FileStream fs = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read))
      {
        Microsoft.SharePoint.Client.File.SaveBinaryDirect (ctx, lib.RootFolder.ServerRelativeUrl + "/myPicture.jpg", fs, true);
      }
    }

但我总是收到 403 错误。

我也验证了这是否不是这篇文章中提到的同一个问题,但我认为不是。

https://razirais.wordpress.com/2011/05/19/how-to-fix-sharepoint-online-403-forbidden-error-while-downloading-files-using-client-object-model/

当我这样做时,我收到了 405 错误,提琴手日志显示该请求正在尝试将上下文重定向到身份验证页面。

我需要启用一个设置吗?知道我有什么问题吗?

【问题讨论】:

    标签: .net sharepoint shared-libraries sharepoint-online sharepoint-clientobject


    【解决方案1】:

    由于您已在 SharePoint Online 中对其进行了标记,因此我假设您仅使用 SharePoint Online。

    您需要将用户名和密码传递给ClientContext。所以,修改你的代码如下:

    string webUrl = "https://mysharepointesite.sharepoint.com/";
    
    string userName = "user.name@tenantname.onmicrosoft.com";
    string password = "password";
    
    using (ClientContext ctx = new ClientContext(webUrl))
    {
        SecureString securePassword = new SecureString();
        foreach (char c in password.ToCharArray())
        {
            securePassword.AppendChar(c);
        }   
    
        ctx.AuthenticationMode = ClientAuthenticationMode.Default;
        ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);
    
        if (ctx != null)
        {
          var lib = ctx.Web.Lists.GetByTitle("mesdocuments");
          ctx.Load(lib);
          ctx.Load(lib.RootFolder);
          ctx.ExecuteQuery();
          string sourceUrl = "C:\\temp\\picture.jpg";          
          using (FileStream fs = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read))
          {
            Microsoft.SharePoint.Client.File.SaveBinaryDirect (ctx, lib.RootFolder.ServerRelativeUrl + "/myPicture.jpg", fs, true);
          }
        }
    
    }
    

    另外,请确保您使用的是正确的 CSOM dll,即使用 SharePoint 在线 CSOM SDK。查看您当前的实现,我认为您错误地使用了导致错误的客户端上下文。

    您可以从下面提到的链接或使用 Nuget(首选)下载最新的 SDK。

    如果您下载 SDK,那么您可能需要从安装它的文件夹中手动引用 dll。

    通常,如果安装在C:驱动器中,位置是:

    C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI.

    从那里,您需要在项目中引用 Microsoft.SharePoint.Client.dllMicrosoft.SharePoint.Client.Runtime.dll 文件。

    下载链接 - SharePoint Online Client Components SDK

    Nuget 包 - Microsoft.SharePointOnline.CSOM

    另外,如果您不喜欢传递用户名和密码,您可以使用客户端 ID 和客户端密码。要使用它,你可以看看我的回答here

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 2017-09-28
      • 2012-05-26
      • 2015-12-23
      • 2018-08-15
      • 2021-03-23
      • 2015-10-23
      • 2018-08-28
      • 1970-01-01
      相关资源
      最近更新 更多