Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Microsoft.SharePoint.Client.ServerUnauthorizedAccess异常:访问被拒绝_Sharepoint - Fatal编程技术网

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

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

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

有人能帮我吗?谢谢

以下是参数:

  • 文件: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.ServerUnauthorizedAccess异常:访问 否认。您没有执行此操作或访问的权限 这个资源。在 Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(流 响应(团队)在 Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()位于 Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()位于 ArtefactUploader.SharepointUploader.CreateFolderInternal(Web, 文件夹parentFolder,字符串fullFolderPath)位于 D:\Repos\helpfilesync\ArtefactUploader\SharepointUploader.cs:第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();
        }
    }

此问题是由重建程序在创建clientcontext对象时传入SharePoint基址引起的,该问题通过传入SharePoint网站地址来解决。感谢Lee_MSFT-Before:clientcontext clientcontext=new clientcontext(“https://**.SharePoint.com”)Before:clientcontext clientcontext clientcontext=new clientcontext(“https://***.sharepoint.com/sites/***”)