首页 > 解决方案 > Microsoft.SharePoint.Client.ServerUnauthorizedAccessException:访问被拒绝

问题描述

使用c#代码在sharepoint的指定目录创建子目录时遇到异常。

异常消息:Microsoft.SharePoint.Client.ServerUnauthorizedAccessException:访问被拒绝。您无权执行此操作或访问此资源。

任何人都可以帮助我吗?谢谢!

以下是参数:

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

标签: sharepoint

解决方案


测试了你的代码,工作正常。确保用户/密码正确。

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();
        }
    }

在此处输入图像描述


推荐阅读