none
System.UnauthorizedAccessException in respoint api endpoints RRS feed

  • Question

  • hi, I'm using share point api endpoints to access folders and file (either via folder GUID or server relative path).  I have two folders: folder #1:/OrgReports/Forms/AllItems.aspx (GUID 3DA0B055-968D-4689-9579-3B415221F5BB) and folder #2: /sites/ISO/regulation/Forms/AllItems.aspx (GUID C5D0D7C6-5147-4D26-8C5F-B5B347B5E28C) 
    I'm trying to read all files and sub folders inside these folders. the problem is only one them returns the expected results and the other one (folder #2) returns System.UnauthorizedAccessException even though the user has access to that folder (in web view). 

    I already used these endpoints with the same results:

    1. /_api/web/GetFolderById('folderGUID')

    2. /_api/web/GetFolderByServerRelativeUrl('/sites/ISO/regulation/')

    3. /_api/web/Lists/GetByTitle('folderTitle')/rootFolder/Folders

    and these are the results for folder #2:

    {
        "error": {
            "code": "-2147024894, System.IO.FileNotFoundException",
            "message": {
                "lang": "en-EN",
                "value": "File Not Found."
            }
        }
    }
    {
        "error": {
            "code": "-2147024891, System.UnauthorizedAccessException",
            "message": {
                "lang": "en-EN",
                "value": "Access denied. You do not have permission to perform this action or access this resource."
            }
        }
    }

    Note that I can view the second folder in the web with the same credentials (username and password) and use CURLAUTH_NTLM in my curl requests.

    So here is my question: is there another way to get all files and folders using share point rest api? if not what is the mistake in my approaches? 

    Wednesday, August 21, 2019 12:01 PM

All replies

  • Hi MehrdadEP,

    I replied a same thread in StackOverflow 2 days. Let me explain furthur.

    You can firtsly get the all the folder Guid with the endpoint below, note, this is passing LibraryTitle not folder Title, in this case, Library Title should be regulation, while in my Test capture below, I'm using the default library "Shared Dcouments" which Title is "Documents":

    /_api/web/Lists/GetByTitle('Documents')/rootFolder

     

    Then you could use this folder UniqueId in GetFolderById endpoint:

    /_api/web/getFolderById('d9b5be78-2fd9-4d48-8a2d-da773ca9e09c')?$expand=Folders,Files

    For UnauthorizedAccessException issue, try to check the permission for the library from Web UI, make sure in sub folders there is no unique permission.

    Another way to list folders and files in the library is using this endpoint:

    /_api/web/Lists/getbyTitle('Documents')/Items?$select=FileLeafRef,FileRef,Id

    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.


    Thursday, August 22, 2019 2:49 AM
  • Thank you, the problem was I didn't know that there was different endpoints for site collections.

    I used http://sitename/sites/collection_name/_api/web ... and the problem is fixed now.

    Sunday, August 25, 2019 6:46 AM
  • Hi MehrdadEP,

    Thanks for your sharing, you could Mark your reply as answer so that it could help others who stuck in the same issue in the forum.

    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, August 26, 2019 6:02 AM