question

Sachin-9234 avatar image
0 Votes"
Sachin-9234 asked MichaelHan-MSFT edited

How to fetch files from a document library in one site collection to display in a web part in another site collection in SPO with or without APIs

I have a project documents library in one site collection (projects) and I need to fetch and display these files in another site collection (HR) in SharePoint online , but the users from HR do not have permissions on project documents and also we do not want to mirror the document library in HR site collection.

I tried to fetch the documents using REST API :

GET method -

var requestUri = _spPageContextInfo.webAbsoluteUrl +"/_api/web/lists/getByTitle('Test Project Documents')/items?$select=EncodedAbsUrl,*,File/Name&$expand=File";

var requestHeaders = "accept": "application/json;odata=verbose"


But I got the response code of 404 as Project Documents Library was not present in HR site collection.

Reference: https://www.enjoysharepoint.com/get-documents-from-document-library-sharepoint-rest-api/


Also i tried looking into cross domain but it is not supported in SharePoint Online.

Reference : https://social.technet.microsoft.com/Forums/en-US/dda6463a-2f6a-4243-9660-15e837632464/accessing-list-items-from-a-different-site-collection-in-top-nav?forum=SP2016

Any leads will be so helpful.

Regards,
Sachin

office-sharepoint-onlinesharepoint-dev
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

MichaelHan-MSFT avatar image
0 Votes"
MichaelHan-MSFT answered MichaelHan-MSFT commented

Hi @Sachin-9234

In your code, the requestUri is not right. Try to change it to the below:

 var requestUri ="https://xxx.sharepoint.com/sites/<projects site>/_api/web/lists/getByTitle('Test Project Documents')/items?$select=EncodedAbsUrl,*,File/Name&$expand=File";


Note: users need to have at lease read permission on the library to see the file. If users donot have permissions, you would get access denied in reponse.


If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Michael Han,

Thank you so much for the answer, yes it solves my question regarding not mirroring the project documents in HR.

However regarding the read permissions for the library, I have an issue with it.

The test project documents contains information from different banks and we do not want to give permissions to everyone as they would have access to different bank documents as well.

The requirement was to display information in a web part in different pages for different banks without giving permissions to the project documents.

Please suggest how to proceed further regarding this.

Regards,
Sachin

0 Votes 0 ·

@Sachin-9234

Glad that my solution works for you. You could accept my answer via "Accept Answer" button to close this thread.

If you have several issues, we recommend you open up a new thread for each problem so that we can help you with them specifically in a timely manner

0 Votes 0 ·