sharepoint - Microsoft.SharePoint.Client.ServerUnauthorizedAccessException:访问被拒绝
问题描述
使用c#代码在sharepoint的指定目录创建子目录时遇到异常。
异常消息:Microsoft.SharePoint.Client.ServerUnauthorizedAccessException:访问被拒绝。您无权执行此操作或访问此资源。
任何人都可以帮助我吗?谢谢!
以下是参数:
- 文件:D:\Repos\helpfilesync\ArtefactUploader\bin\Release\ArtefactUploader.exe
- 文件名:ArtefactUploader.exe
- 上传路径:/sites/Platform/Shared Documents/dailybuild/helpfilesync/
子文件夹路径:v0.1.0/
public void Upload() { using (ClientContext clientContext = new ClientContext("*****")) { SecureString pass = new SecureString(); foreach (char ch in password) { pass.AppendChar(ch); } clientContext.Credentials = new SharePointOnlineCredentials(user, pass); Web web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); if (!string.IsNullOrWhiteSpace(this.subFolderPath)) { CreateFolder(clientContext.Web, uploadPath, subFolderPath); } using (FileStream fs = new FileStream(file, FileMode.Open)) { Microsoft.SharePoint.Client.File.SaveBinaryDirect (clientContext, $"{this.uploadPath}{this.subFolderPath}/{fileName}", fs, true); } Console.WriteLine("Uploaded File Successfully"); } } public void CreateFolder(Web web, string relativePath, string fullFolderPath) { if (web == null) { throw new ArgumentNullException(nameof(web)); } if (string.IsNullOrWhiteSpace(relativePath)) { throw new ArgumentNullException(nameof(relativePath)); } if (string.IsNullOrWhiteSpace(fullFolderPath)) { throw new ArgumentNullException(fullFolderPath); } Folder relativeFolder = web.GetFolderByServerRelativeUrl(relativePath); CreateFolderInternal(web, relativeFolder, fullFolderPath); } public static Folder CreateFolderInternal(Web web, Folder parentFolder, string fullFolderPath) { var folderUrls = fullFolderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string folderUrl = folderUrls[0]; var curFolder = parentFolder.Folders.Add(folderUrl); //web.Context.Load(curFolder); try { web.Context.ExecuteQuery(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } if (folderUrls.Length > 1) { var folderPath = string.Join("/", folderUrls, 1, folderUrls.Length - 1); return CreateFolderInternal(web, curFolder, folderPath); } return curFolder; }
Microsoft.SharePoint.Client.ServerUnauthorizedAccessException:访问被拒绝。您无权执行此操作或访问此资源。在 Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream) 在 Microsoft.SharePoint.Client.ClientRequest.ProcessResponse() 在 Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() 在 ArtefactUploader.SharepointUploader.CreateFolderInternal(Web web, 文件夹 parentFolder , String fullFolderPath) 在 D:\Repos\helpfilesync\ArtefactUploader\SharepointUploader.cs:line 96
解决方案
测试了你的代码,工作正常。确保用户/密码正确。
class Program
{
const string user = "user@teanat.onmicrosoft.com";
const string password = "password";
public static void Upload()
{
using (ClientContext clientContext = new ClientContext("https://tenant.sharepoint.com/sites/lee"))
{
SecureString pass = new SecureString();
foreach (char ch in password)
{
pass.AppendChar(ch);
}
clientContext.Credentials = new SharePointOnlineCredentials(user, pass);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
if (!string.IsNullOrWhiteSpace("a"))
{
CreateFolder(clientContext.Web, "/sites/lee/mydoc2", "childA");
}
//using (FileStream fs = new FileStream(file, FileMode.Open))
//{
// Microsoft.SharePoint.Client.File.SaveBinaryDirect
// (clientContext, $"{this.uploadPath}{this.subFolderPath}/{fileName}", fs, true);
//}
Console.WriteLine("Uploaded File Successfully");
}
}
public static void CreateFolder(Web web, string relativePath, string fullFolderPath)
{
if (web == null)
{
throw new ArgumentNullException(nameof(web));
}
if (string.IsNullOrWhiteSpace(relativePath))
{
throw new ArgumentNullException(nameof(relativePath));
}
if (string.IsNullOrWhiteSpace(fullFolderPath))
{
throw new ArgumentNullException(fullFolderPath);
}
Folder relativeFolder = web.GetFolderByServerRelativeUrl(relativePath);
CreateFolderInternal(web, relativeFolder, fullFolderPath);
}
public static Folder CreateFolderInternal(Web web, Folder parentFolder, string fullFolderPath)
{
var folderUrls = fullFolderPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string folderUrl = folderUrls[0];
var curFolder = parentFolder.Folders.Add(folderUrl);
//web.Context.Load(curFolder);
try
{
web.Context.ExecuteQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (folderUrls.Length > 1)
{
var folderPath = string.Join("/", folderUrls, 1, folderUrls.Length - 1);
return CreateFolderInternal(web, curFolder, folderPath);
}
return curFolder;
}
static void Main(string[] args)
{
Upload();
}
}
推荐阅读
- python - UnboundLocalError:分配前引用的局部变量“压缩”:生成器错误
- c# - Umbraco 7:如何获取包含当前内容的内容
- android - 在 Android Kotlin 中查找日期之间的天数差异
- java - 地图
返回地图 > groupingBy value 之后,而不是 Map >> - lua - 在STM32F103中运行LUA抛出错误“unexpected symbol”
- c# - 如何使用 Entity Framework .NET Core 编写带有 where 子句的 SQL 更新查询
- flutter - admob firebase_admob:verifyReleaseResources 错误
- c# - 使用复杂位掩码显示串行项
- python - 当前者的路径设置为系统而后者设置为用户环境变量时,为什么传统的 python 在 cmd 中优先于 anaconda python?
- arrays - 在 awk 中循环删除数组元素:总是安全的?