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

Sachin 21 Reputation points
2021-07-08T14:32:05.987+00:00

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

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,702 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,977 questions
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,031 Reputation points
    2021-07-09T03:23:01.58+00:00

    Hi @Sachin

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.