以编程方式将默认文档库作为 SP16 中文件上传的目标

Programatically target default document library for file upload in SP16

我制作了一个用于将文件上传到 SharePoint 的 C# 应用程序。到目前为止,它在除默认文档库之外的所有文档库上都按预期工作。每次它抛出异常:列表 'Documents' 在 URL 'http://...' 的站点不存在 我也尝试过 "Shared Documents",但结果相同。默认库是否有一些我不知道的内部名称?

上传代码如下:

// Get the SharePoint context
            ClientContext context = new ClientContext(domain);

            // Open the web
            var web = context.Web;

            String[] files = System.IO.File.ReadAllLines(args[0]);

            foreach (String file in files)
            {
                // Create the new file  
                var newFile = new FileCreationInformation
                {
                    Content = System.IO.File.ReadAllBytes(file),
                    Url = Path.GetFileName(file),
                    Overwrite = true
                };

                // Get a reference to the document library
                var docs = web.Lists.GetByTitle(library);
                var uploadFile = docs.RootFolder.Files.Add(newFile);

                // Upload the document
                context.Load(uploadFile);
            }

首先,通过url而不是标题获取列表更安全。

using (ClientContext context = new ClientContext("https://sharepoint.domain.com"))
{
    context.Load(context.Web, w => w.ServerRelativeUrl);
    context.ExecuteQuery();

    List list = context.Web.GetList($"{context.Web.ServerRelativeUrl.TrimEnd('/')}/Shared Documents");
}

另外不要忘记处理 object context.

检查启用的备用语言(站点设置 > 站点管理 > 语言设置)。您可能有更多启用的语言,默认语言可能与您期望的不同。每种语言都有自己的列表名称。