使用SharePoint CSOM获取子文件夹

时间:2018-10-16 15:57:31

标签: c# sharepoint csom

我想使用CSOM在SharePoint网站上的“共享文档”中获得一个文件夹,以便向其中添加文件。 这是我当前的代码:

        // Get the folder we want to add images to
        var context = new ClientContext(sharePointSiteUrl)
        context.Credentials = new SharePointOnlineCredentials(username, password);
        Web web = context.Web;
        Folder targetFolder = web.GetFolderByServerRelativeUrl("/Shared%20Documents/" + nameOfTargerFolder);
        context.ExecuteQuery();

我收到此错误:“服务器相对URL必须以SPWeb.ServerRelativeUrl开头”

非常感谢!

1 个答案:

答案 0 :(得分:0)

该Web对象具有serverrelativeurl属性:     https://docs.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.spweb.serverrelativeurl?view=sharepoint-server

尝试一下:

    // Get the folder we want to add images to
    var context = new ClientContext(sharePointSiteUrl)
    context.Credentials = new SharePointOnlineCredentials(username, password);
    Web web = context.Web;
    context.Load(web, w => w.ServerRelativeUrl);
    context.ExecuteQuery();
    Folder targetFolder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/Shared%20Documents/" + nameOfTargerFolder);
    context.ExecuteQuery();