1

I'm very VERY new to SharePoint and am currently working on a project with needs a react SPFx Web Part to be developed which can be put on any site/page to read data from Document Libraries. However, before I get to that, I just want to be able to see data coming back in Postman.

I've authenticated using App Permissions so I have my token and that is working as far as I can see. I am just having issues forming the URL to get the data.

I know this works:

https://tennantname.sharepoint.com/_api/web/lists?$filter=Hidden eq false

As I get data back.

But when I try and target a specific site to get data from, I am unable to perform such a task(I'm not even sure if I should be able to).

The URL I see in my browser when I click on the site and the Document Library is:

https://tennant.sharepoint.com/sites/BR-DemoBidderRoom/Shared%20Documents/Forms/Default%20View.aspx

So I thought that in Postman, I should be able to do:

https://tennant.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/BR-DemoBidderRoom/SitePages/Documents/')?$expand=Folders,Files

But I get this back:

"odata.error": {
        "code": "-2147024894, System.IO.FileNotFoundException",
        "message": {
            "lang": "en-US",
            "value": "File Not Found."
        }

There are two folders and a file in the root of Shared Documents.

I seem to get File not found no matter what I put in the URL. And I sometimes get unauthorized errors depending on what I put in the URL (i.e. putting tennant.sharepoint.com/sites/_api/web etc.). Nothing seems to work!

Am I using this correctly, or are these actions prohibited?

Hope you can help.

Thanks

2 Answers 2

0

If you take a look here you will see that the endpoint used by the example is the below:

    GET https://{site_url}/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')
    Authorization: "Bearer " + accessToken
    Accept: "application/json;odata=verbose"

{site_url} is the URL of the Site and in the GetFolderByServerRelativeUrl method you should target the Document Library that you wish to get data from.

I believe that in your case, since "There are two folders and a file in the root of Shared Documents." It should be https://tennant.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')?$expand=Folders,Files meaning that you should be targetting the Shared Documents Library in the Site.

You may have given yourself App Prmissions, but have you given them to the correct Site Collection? https://tennant.sharepoint.com and https://tennant.sharepoint.com/sites/BR-DemoBidderRoom/ are two different sites and the Permissions that you might have given yourself to the Top Level Site will not be inherited in other Site Collections (This can only be achieved by Azure AD Authentication).

9
  • Still getting the File Not Found error. Which I can see why when I'm using postman, as surely I'm not providing enough infomation if I just use /SitePages/Documents/ ?
    – Bentura
    Mar 4, 2022 at 15:42
  • I think that you are searching the wrong Document Library. You did say "There are two folders and a file in the root of Shared Documents." so i have editted my answer in order to help out.
    – jimas13
    Mar 4, 2022 at 15:47
  • Pretty sure I set the app permissions to Scope="sharepoint/content/sitecollection" Right="FullControl"
    – Bentura
    Mar 4, 2022 at 16:06
  • If I do this : tenant.sharepoint.com/sites/BR-DemoBidderRoom/_api/web/… I get the following error : "odata.error": { "code": "-2147024891, System.UnauthorizedAccessException", "message": { "lang": "en-US", "value": "Attempted to perform an unauthorized operation." } } I'm not sure if that is a legit error about an unathorised operation, or it's just a repsonse to a malformed URL.
    – Bentura
    Mar 4, 2022 at 16:14
  • On which site have you set the above Scope ?
    – jimas13
    Mar 4, 2022 at 19:05
1

Try using endpoint like:

https://tenant.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/SitePages/Documents')?$expand=Folders,Files

Note: Add / before library name and remove / after last folder name in server relative URL in endpoint.

Similar thread: Unable to read sub folders inside document library with REST

Microsoft official documentation: Working with folders and files with REST


Updated from comments:

Above URL is targeting the root SharePoint site. To target specific site, try this:

https://tenant.sharepoint.com/sites/BR-DemoBidderRoom/_api/web/GetFolderByServerRelativeUrl('/SitePages/Documents')?$expand=Folders,Files

Make sure you have a document library with name SitePages & folder with name Documents inside SitePages library.


If you are trying to query the default SharePoint library (Documents) on SharePoint site, use endpoint in below format:

https://tenant.sharepoint.com/sites/BR-DemoBidderRoom/_api/web/GetFolderByServerRelativeUrl('/Shared Documents')?$expand=Folders,Files
2
  • That still brings back no file found. Also, how am I targeting a specific document library for a specific site if I am just using /SitePage/Documents ?
    – Bentura
    Mar 4, 2022 at 16:08
  • Above URL is targeting the root SharePoint site. To target specific site, check updated answer. Mar 4, 2022 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.