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

Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
Bentura
  • 49
  • 7

2 Answers2

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
Ganesh Sanap - MVP
  • 44,918
  • 21
  • 30
  • 61
  • 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 04 '22 at 16:08
  • Above URL is targeting the root SharePoint site. To target specific site, check updated answer. – Ganesh Sanap - MVP Mar 04 '22 at 16:32
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).

jimas13
  • 318
  • 2
  • 13
  • 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 04 '22 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 04 '22 at 15:47
  • Pretty sure I set the app permissions to

    Scope="http://sharepoint/content/sitecollection" Right="FullControl"

    – Bentura Mar 04 '22 at 16:06
  • If I do this :

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

    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 04 '22 at 16:14
  • On which site have you set the above Scope ? – jimas13 Mar 04 '22 at 19:05
  • Hi, sorry what do you mean by 'which site'? – Bentura Mar 07 '22 at 09:12
  • I see what you mean. I did it via AD and got another admin to give global permissions. Ill double check that though. – Bentura Mar 07 '22 at 11:06
  • nice :) let me know if the issue is solved. – jimas13 Mar 07 '22 at 18:55
  • So I discovered that the Title colum hasn't been populated with any data so that's why the getbytitle wasn't working. I'm now using GetserverbyrelativeUrl and have managed to get everything working :) – Bentura Mar 29 '22 at 13:52