none
C# Error Access denied. You do not have permission to perform this action or access this resource. RRS feed

  • Question

  • Hi, i try to read all folders on my sharepoint site, instead of returning the list with any folders im getting this error :

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

    Using browser i can do anything i want , upload, download and other thins . Just get this error if i try to do any of this actions using c#. 

    This is what i'm tryng to do :

    string userName = Console.ReadLine();

    string pswd = Console.ReadLine();
                SecureString password = new NetworkCredential("", pswd).SecurePassword;

    using (var context = new ClientContext(webUrl)) { context.Credentials = new SharePointOnlineCredentials(userName, password); Web Upload = context.Web; FolderCollection collFolder = Upload.Folders; context.Load(collFolder); context.ExecuteQuery(); Console.WriteLine("The current site contains the following folders:\n\n"); foreach (Folder myFolder in collFolder) Console.WriteLine(myFolder.Name);}


    Thanks.


    Rodrigo S. Nascimento


    Saturday, November 2, 2019 11:10 AM

All replies

  • Do you use SharePoint Online?

    If so refer below link.

    https://www.c-sharpcorner.com/blogs/office365-how-to-connect-sharepoint-online-site-using-csom


    Justin Liu Office Apps & Services MVP, MCSE
    Senior Software Engineer
    Learn Microsoft 365 from Microsoft DOCs now!
    Please Vote and Mark as Answer if it helps you.

    Monday, November 4, 2019 1:38 AM
  • Hi Rodrigo,

    If you are using SharePoiont Online, please covert password to secure string like the code demo below, rather than use NetworkCredential:

    	    string password = "*******";
                string account = "jerryzy@tenant.onmicrosoft.com";
                var secret = new SecureString();
                foreach (char c in password)
                {
                    secret.AppendChar(c);
                }
                using (ClientContext ctx = new ClientContext("https://tenant.sharepoint.com/sites/dev/"))
                {
    
                    ctx.Credentials = new SharePointOnlineCredentials(account, secret);
                    ctx.Load(ctx.Web);
                    ctx.ExecuteQuery();
                    FolderCollection collFolder = ctx.Web.Folders;
                    ctx.Load(collFolder);
                    ctx.ExecuteQuery();
                    foreach (Folder myFolder in collFolder)
                    {
                        Console.WriteLine(myFolder.Name);
                    }
    			}

    And please make sure the user account you are using have at least Contribute Permission in the site collection like below so that they have permission to access the web folder with CSOM:

    Because Contribue Permission level will have "Browser Directories" permission by default which could access web folders:

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.







    Monday, November 4, 2019 4:00 AM
  • Hi Rodrigo,

    Would you please provide a update of this question ? If the reply above is helpful to this question, I would suggest you can Mark as answer so that it could also help others in the forum who have the similiar question.

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, November 13, 2019 9:12 AM