pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
764 stars 304 forks source link

Get recent Sharepoint FIles #3159

Closed ahernandez-iusup closed 2 weeks ago

ahernandez-iusup commented 3 weeks ago

What version of PnPjs library you are using

4.x

Minor Version Number

1

Target environment

All

Additional environment details

I'm using Node.js to develop with PNP. I want to get the last 5/10 recent files that a user have seen or modified but I don't find a way to do it.

Question/Request

I have seen this: https://pnp.github.io/pnpjs/graph/files/#get-the-recent-files But when I try to use the drive id of my sharepoint it says: "code":"accessDenied","message":"The current caller is not the drive owner."

Is there a way how I can get the recent files from Sharepoint?

juliemturner commented 3 weeks ago

If you want to share your code, we can try to help but generally "Access denied" implies you don't have access. Which either means you don't have a valid access token (i.e. since you're using node, I assume you have an app registration that grants the proper scopes) or you don't have delegated access to the thing you're trying to access. I have no way of helping you without further information.

ahernandez-iusup commented 2 weeks ago
   const graph = graphfi().using(GraphDefault({
    baseUrl: 'https://graph.microsoft.com/v1.0',
    msal: {
        config: configuration,
        scopes: ['https://graph.microsoft.com/.default']
    }
   }));

   graphMe.on.auth(async function (url, init) {

    init.headers["Authorization"] = `Bearer ${token}`;

    return [url, init];
    });

    //const files1 = await graph3.drives.getById(idDriveSharepoint).recent.orderBy("lastModifiedDateTime", false).top(5)()
    const drives= await graphMe.drives()
    const files = await graphMe.drives.getById(drives[0].id).recent.orderBy("lastModifiedDateTime", false).top(5)()

Files1 returns this, "code":"accessDenied","message":"The current caller is not the drive owner."

Files returns the top 5

Is there any way to get the recent files from sharepoint?

bcameron1231 commented 2 weeks ago

Hi. your code is not full and is unclear.

You've instantiated the graphfi object called graph, then you are using a graphMe object. Then on files1, you're using an object called graph3

Where did graphMe and graph3 come from and how are they instantiated? Where did that token come from that you are assigning to it in that behavior? Can you share what permissions you've granted to your application in Azure?

patrick-rodgers commented 2 weeks ago

Recent by design only works with a signed in user. The collection represents the set of recent files for that user, there isn't a concept of recent files in the drive. You could sort on last modified and use $top

ahernandez-iusup commented 2 weeks ago

Thanks for your help. As I understand it, using Recent I can't access a SharePoint drive. Is there a way to get recent files from a SharePoint library?

juliemturner commented 2 weeks ago

In graph a drive is a SharePoint library or OneDrive... there is no difference.

patrick-rodgers commented 2 weeks ago

/ Closing this issue as answered. If you have additional questions or we did not answer your question, please open a new issue, ref this issue, and provide any additional details available. Thank you!

github-actions[bot] commented 1 week ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.