Microsoft.SharePoint.Client.ServerUnauthorizedAccessException:访问被拒绝

时间:2019-10-15 10:20:18

标签: sharepoint

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

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

有人可以帮助我吗?谢谢!

以下是参数:

  • 文件:D:\ Repos \ helpfilesync \ ArtefactUploader \ bin \ Release \ ArtefactUploader.exe
  • 文件名:ArtefactUploader.exe
  • uploadPath:/ sites / Platform / Shared Documents / dailybuild / helpfilesync /
  • subFolderPath: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(流   responseStream)在   Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()在   Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()在   ArtefactUploader.SharepointUploader.CreateFolderInternal(网络   文件夹parentFolder,字符串fullFolderPath)在   D:\ Repos \ helpfilesync \ ArtefactUploader \ SharepointUploader.cs:第96行

1 个答案:

答案 0 :(得分:0)

已对您的代码进行了测试,效果很好。确保用户名/密码正确。

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

enter image description here