0
votes

I encountered an exception when I used c# code to create subdirectories in sharepoint's specified directory.

Exception message: Microsoft.SharePoint.Client.ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource.

Anybody can help me? thanks!

The following is parameters:

  • file: D:\Repos\helpfilesync\ArtefactUploader\bin\Release\ArtefactUploader.exe
  • fileName: 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: Access denied. You do not have permission to perform this action or access this resource. at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream) at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse() at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery() at ArtefactUploader.SharepointUploader.CreateFolderInternal(Web web, Folder parentFolder, String fullFolderPath) in D:\Repos\helpfilesync\ArtefactUploader\SharepointUploader.cs:line 96

1

1 Answers

0
votes

Did test of your code, works fine. Make sure the user/password is correct.

class Program
    {

        const string user = "[email protected]";
        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