C#上传文件至SharePoint文档库遇SP命名空间引用错误求助
问题解决:SP.xxx未定义编译错误
核心问题分析
你代码里的SP.前缀是错误用法,还引用了针对本地SharePoint的Microsoft.SharePoint命名空间,这和SharePoint Online的CSOM库完全不兼容。再加上可能装错了NuGet包,才导致所有SP相关标识提示未定义。
解决步骤
清理错误引用
- 删除代码里的
using Microsoft.SharePoint;,这个是给本地SharePoint服务器用的,和Online无关。 - 把所有代码里的
SP.前缀全部删掉,Microsoft.SharePoint.Client命名空间下的类直接用类名即可。
- 删除代码里的
安装正确的NuGet包
在NuGet包管理器中安装Microsoft.SharePointOnline.CSOM,选择兼容.NET 4.8的稳定版本(比如16.0.24121.12000及以上)。修正代码逻辑错误
- 原代码里的文件路径
C: estupload.pdf转义错误,是制表符,要改成@"C:\test\upload.pdf"或者"C:\\test\\upload.pdf"。 - 直接调用
Folders.Add会导致文件夹已存在时报错,建议先检查文件夹是否存在,不存在再创建。
- 原代码里的文件路径
修正后的完整代码
using System; using System.Security; using System.IO; using Microsoft.SharePoint.Client; namespace UploadToSharePoint_Net48 { internal class Program { static void Main(string[] args) { string SiteUrl = "https://You.sharepoint.com/sites/Upload"; string DocumentLibrary = "UploadLibrary"; string FileName = @"C:\test\upload.pdf"; string CustomerFolder = "1564_dsfgsst"; string UserName = "samir.daoudi@******.co.uk"; string Password = "*****"; UploadFileToSharePoint(SiteUrl, DocumentLibrary, CustomerFolder, FileName, UserName, Password); } private static void UploadFileToSharePoint(string siteUrl, string docLibraryTitle, string clientSubFolder, string localFilePath, string login, string password) { try { // 构建安全密码 var securePassword = new SecureString(); foreach (char c in password) { securePassword.AppendChar(c); } var onlineCredentials = new SharePointOnlineCredentials(login, securePassword); using (var clientContext = new ClientContext(siteUrl)) { clientContext.Credentials = onlineCredentials; Web web = clientContext.Web; // 获取文档库 List docLibrary = web.Lists.GetByTitle(docLibraryTitle); clientContext.Load(docLibrary, l => l.RootFolder); clientContext.ExecuteQuery(); // 检查目标文件夹是否存在,不存在则创建 Folder targetFolder = null; try { targetFolder = web.GetFolderByServerRelativeUrl($"{docLibrary.RootFolder.ServerRelativeUrl}/{clientSubFolder}"); clientContext.Load(targetFolder); clientContext.ExecuteQuery(); } catch (ServerException ex) { // 文件夹不存在时创建 if (ex.ServerErrorCode == -2147024894) { targetFolder = docLibrary.RootFolder.Folders.Add(clientSubFolder); clientContext.Load(targetFolder); clientContext.ExecuteQuery(); } else { throw; } } // 准备文件上传信息 byte[] fileContent = File.ReadAllBytes(localFilePath); FileCreationInformation fileInfo = new FileCreationInformation { ContentStream = new MemoryStream(fileContent), Url = Path.GetFileName(localFilePath), Overwrite = true // 如果文件已存在则覆盖,可按需调整 }; // 上传文件 Microsoft.SharePoint.Client.File uploadedFile = targetFolder.Files.Add(fileInfo); clientContext.Load(uploadedFile); clientContext.ExecuteQuery(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"文件上传成功\n文件地址:{siteUrl}/{docLibraryTitle}/{clientSubFolder}/{Path.GetFileName(localFilePath)}"); } } catch (Exception exp) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"错误信息:{exp.Message}\n堆栈跟踪:{exp.StackTrace}"); } finally { Console.ReadLine(); } } } }
关键修改说明
- 移除了冗余的
SP.前缀和错误的Microsoft.SharePoint引用,只保留Microsoft.SharePoint.Client。 - 增加了文件夹存在性检查,避免重复创建导致的异常。
- 修正了文件路径的转义错误。
- 增加了
Overwrite = true配置,可根据需求调整是否覆盖已存在的文件。
内容的提问来源于stack exchange,提问作者Colin Slater
相关产品推荐
相关产品推荐

