问题描述
我正在尝试使用C#(Microsoft.SharePoint.Client
)中的官方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
属性而不是Folders,也会发生这种情况。
我在这里想念什么? folder
变量似乎已正确加载(调用第一个ItemsCount
之后,其中的11
属性设置为.ExecuteQuery()
),但是如果不加例外。我在这里做什么错了?
解决方法
好吧,我发现了问题。
“拆分”路径确实存在问题。
我不得不进一步介绍网站网址:
using (ClientContext ctx = new ClientContext("https://*******.sharepoint.com/sites/cms/doc/projects"))
并从文件夹路径中删除该部分:
var folder = web.GetFolderByServerRelativeUrl("C07725");
现在它可以正常工作了。