Microsoft.SharePoint.Client.ServerUnauthorizedAccess异常:访问被拒绝
我在使用c#代码在sharepoint的指定目录中创建子目录时遇到了一个异常 异常消息: Microsoft.SharePoint.Client.ServerUnauthorizedAccess异常:访问被拒绝。您没有执行此操作或访问此资源的权限 有人能帮我吗?谢谢 以下是参数:Microsoft.SharePoint.Client.ServerUnauthorizedAccess异常:访问被拒绝,sharepoint,Sharepoint,我在使用c#代码在sharepoint的指定目录中创建子目录时遇到了一个异常 异常消息: Microsoft.SharePoint.Client.ServerUnauthorizedAccess异常:访问被拒绝。您没有执行此操作或访问此资源的权限 有人能帮我吗?谢谢 以下是参数: 文件:D:\Repos\helpfilesync\ArtefactUploader\bin\Release\ArtefactUploader.exe 文件名:ArtefactUploader.exe 上载路径:/si
- 文件: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; }
对代码进行了测试,工作正常。确保用户/密码正确
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();
}
}
此问题是由重建程序在创建clientcontext对象时传入SharePoint基址引起的,该问题通过传入SharePoint网站地址来解决。感谢Lee_MSFT-Before:clientcontext clientcontext=new clientcontext(“https://**.SharePoint.com”)Before:clientcontext clientcontext clientcontext=new clientcontext(“https://***.sharepoint.com/sites/***”)