【问题标题】:Cannot list files/folders with the SharePoint Client API无法使用 SharePoint 客户端 API 列出文件/文件夹
【发布时间】:2020-09-22 09:51:31
【问题描述】:

我正在尝试使用 C# 中的官方 SharePoint 客户端库 (Microsoft.SharePoint.Client) 列出 SharePoint 在线网站中的文件和文件夹。

这是网站文件的外观:

基于 URL(你可以在图片中看到它),这就是我正在尝试的方式:

using (ClientContext ctx = new ClientContext("https://*******.sharepoint.com/sites/cms"))
{
    //Setup authentication
    SecureString passWord = new SecureString();
    foreach (char c in "mypassword".ToCharArray())
    {
        passWord.AppendChar(c);
    }

    //Connect
    ctx.Credentials = new SharePointOnlineCredentials("myuser", passWord);
    Web web = ctx.Web;

    //Retrieve folder
    var folder = web.GetFolderByServerRelativeUrl("/doc/projects/C07725");
    ctx.Load(folder);
    ctx.ExecuteQuery(); //This seems to work

    //List subfolders
    ctx.Load(folder.Folders);
    ctx.ExecuteQuery(); //This throws Microsoft.SharePoint.Client.ServerException: 'File Not Found.'
}

但是最后一行,如注释中所示,抛出

Microsoft.SharePoint.Client.ServerException: 'File Not Found.'

如果我尝试使用 Files 属性而不是文件夹,也会发生这种情况。

我在这里缺少什么? folder 变量似乎已正确加载(其中的 ItemsCount 属性在调用第一个 .ExecuteQuery() 后设置为 11),但如果不引发异常,我将无法进一步了解。我在这里做错了什么?

【问题讨论】:

    标签: c# sharepoint sharepoint-online sharepoint-clientobject


    【解决方案1】:

    嗯,我发现了问题。

    这确实是我在哪里“分割”路径的问题。

    我不得不进一步使用网站 URL:

    using (ClientContext ctx = new ClientContext("https://*******.sharepoint.com/sites/cms/doc/projects"))
    

    并从文件夹路径中删除该部分:

    var folder = web.GetFolderByServerRelativeUrl("C07725");
    

    现在它可以正常工作了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 2023-01-13
      • 1970-01-01
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多