node.jssharepointpnp-js

Hot to retrieve the Sparepoint Guid by given ItemId


I am wondering whether there is way to retrieve the Guid (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) from an given ItemId (eg. ItemID=1969 as in query params in https://xyz.sharepoint.com/path/to/documents/image.jpg?ItemID=1969&ItemVersion=5.0) with pnpjs under nodejs. The docs do not describe how to do that but there might be a way to use a "back-door" to achieve this. I would like to do something similar like:

sp.web.
  .getById(1969)
  .get()
  .then((file) => {
     doSomethingWith(file.UniqueId); //UniqueId is supposed to be the guid (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
  })
...

Any suggestions are welcome. Cheers


Solution

  • You could use the below way to retrieve the file uniqur id

      sp.web
        .lists
        .getByTitle("Documents")
        .items
        .getById(80)
        .file
        .get()
        .then((file)=>{
          console.log(file.UniqueId);
        })